Click here to Skip to main content
15,742,973 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi! This is the usersignup.php codes. So i want to confirm if both password match or not as well as i wanted to make my username a unique data. So far the codes for username works well but codes on checking if both passwords match, does not work where the php page appears blank

so the actual outcome should be an alert saying that "both passwords don't match"

Under the if statement on checking the password i have tried on other codes like
'echo "Passwords don't match!";', it works perfectly

so i guess if there is error on the codes for the alert function or on the if statement.

What I have tried:



//retreive data from register.html
$username = $_POST['username'];
$password = $_POST['password'];
$confirmpassword = $_POST['password2'];
$email = $_POST['email_address'];  
$query = "SELECT * FROM users WHERE username='$username'";
$result = mysqli_query($con, $query) or die(mysqli_error($con));
$count = mysqli_num_rows($result);

if ($password !== $confirmpassword) {
	echo "<script>window.alert('Passwords don't match!'); ";

}else if ($count>0) {
	echo "<script>alert('Sorry! Username already exist!'); ";

		$sql = "Insert into users (firstname,lastname,username,email,dob,password) 
		VALUES ('$_POST[first_name]','$_POST[last_name]','$username','$email','$_POST[dob]','".md5($password)."');";

		mysqli_query($con, $sql);

		//echo $sql
			echo "<script>alert('Unable to register ! \\nPlease Try Again!');";

			echo "<script>alert('Register Successfully!Please login now!');";
			echo "window.location.href='Login.html';</script>";			
Updated 9-Nov-18 8:41am
Richard Deeming 9-Nov-18 15:24pm    
Don't use MD5 for passwords - it's extremely insecure. Instead, use PHP's built-in methods to store and verify your passwords:

PHP: password_hash[^]
PHP: password_verify[^]

1 solution

so i guess if there is error on the codes for the alert function or on the if statement.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
phpdbg | php debugger[^]
Debugging techniques for PHP programmers[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.

$query = "SELECT * FROM users WHERE username='$username'";

$sql = "Insert into users (firstname,lastname,username,email,dob,password) 
		VALUES ('$_POST[first_name]','$_POST[last_name]','$username','$email','$_POST[dob]','".md5($password)."');";

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