I have concern with my written code here. The connection is ok when I checked but when my if-statement here runs it goes expectedly from the password=cpassword up to the second if-statement for checking the user if exist and goes it into the else because it is false, but when it reached the third if-statement i don't know why in if($query_run) going to be false.
Therefore, the result the data hasn't added to my database and giving me an alert of ERROR which I declared in that else-statement.
<?php
if(isset($_POST['submit_btn']))
{
$username = $_POST['username'];
$password = $_POST['password'];
$cpassword = $_POST['cpassword'];
if($password==$cpassword)
{
$query= "select * from user WHERE username ='$username'";
$query_run = mysqli_query($con,$query);
if(mysqli_num_rows($query_run)>0)
{
// there is already a user with the same username
echo '<script type="text/javascript"> alert("user already exist.. Try another username")</script>';
}
else
{
$query = "insert into user values('$username','$password')";
$query_run = mysqli_query($con,$query);
if($query_run)
{
echo '<script type="text/javascript"> alert("User Registered Go to Login page to Log in")</script>';
}
else
{
echo '<script type="text/javascript"> alert("Error..")</script>';
}
}
}
else
{
echo '<script type="text/javascript"> alert("Password does not match!")</script>';
}
}
?>