Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
<?php 
	include('connection.php');

	$error = "";

	if (isset($_POST['register']))
	{
		$name = $_POST['name'];
		$email = $_POST['email'];
		$password = $_POST['password'];

echo $name;
echo $email;
echo $password;
		
			$query = "INSERT INTO users(name, email, password) VALUES('$name','$email','$password')";
		



			$results = mysqli_query($conn,$query);

			if ($results)
			{
				$error ="you are registered";
				header('location: login.php');
			}
			else
			{
				$error ="sorry not registered";
				mysqli_error($conn = mysqli_connect(SERVER, USER,PASSWORD, DATABASE));
			}
		
	
	}

?>

<!DOCTYPE html>
<html>
<head>
	<title>Register</title>
</head>
<body>
	<form action="" method="post">
		
		<p style="color: red">
			<?php echo $error; ?></p>

	Name:<input type="text" name="name" required=""><br>

	Email:<input type="email" name="email" required=""><br>

	Password:<input type="password" name="password" required=""><br>

	<input type="submit" name="register">

	<a href="login.php"> login here...</a>
	</form>
</body>
</html>


What I have tried:

<?php 
	include('connection.php');

	$error = "";

	if (isset($_POST['register']))
	{
		$name = $_POST['name'];
		$email = $_POST['email'];
		$password = $_POST['password'];

echo $name;
echo $email;
echo $password;
		
			$query = "INSERT INTO users(name, email, password) VALUES('$name','$email','$password')";
		



			$results = mysqli_query($conn,$query);

			if ($results)
			{
				$error ="you are registered";
				header('location: login.php');
			}
			else
			{
				$error ="sorry not registered";
				mysqli_error($conn = mysqli_connect(SERVER, USER,PASSWORD, DATABASE));
			}
		
	
	}

?>

<!DOCTYPE html>
<html>
<head>
	<title>Register</title>
</head>
<body>
	<form action="" method="post">
		
		<p style="color: red">
			<?php echo $error; ?></p>

	Name:<input type="text" name="name" required=""><br>

	Email:<input type="email" name="email" required=""><br>

	Password:<input type="password" name="password" required=""><br>

	<input type="submit" name="register">

	<a href="login.php"> login here...</a>
	</form>
</body>
</html>
Posted
Updated 10-Jan-20 0:09am
Comments
chrisnah 10-Jan-20 5:11am    
its not inserting data into the database
Richard MacCutchan 10-Jan-20 5:24am    
You need to explain exactly what is happening when this code runs. What is the result of the INSERT statement?

Before you even start looking at why it's not working, you need to fix the whole app, or pretty soon, you won't have any database to worry about, or any money left to fix it with ...

Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?
Fix that throughout your app, and you DB should be safe.

Now for your money ... Never store passwords in clear text - it is a major security risk. There is some information on how to do it here: Password Storage: How to do it.[^] - the code is in C#, but it's pretty obvious, and teh principle is exactly the same in PHP.

And remember: as this is web based, if you have any European Union users then GDPR applies and that means you need to handle passwords as sensitive data and store them in a safe and secure manner. Text is neither of those and the fines can be .... um ... outstanding. In December 2018 a German company received a relatively low fine of €20,000 for just that.

Fix all that, and the chances are the problem you have found will go away at the same time ...
 
Share this answer
 
PHP
$query = "INSERT INTO users(name, email, password) VALUES('$name','$email','$password')";

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900