0

Here is the full Error

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\Installations\htdocs\finished\register.php on line 35 Username already exists Query was empty

Here is my code:

    <?php
//Checks to make sure the submit button is pressed
if (isset($_POST['submit'])){
    $errors = array();
    $valid = true;

    //Checks to make sure the password meets the rules of validation
    if(isset($_POST['Password'])) {
        if(!preg_match('/^(?=^.{8,}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$/',$_POST['Password'])) {
            $errors ['Password'] = "Your password must have the following criteria - contain at least (1) upper case letter - contain at least (1) lower case letter - contain at least (1) number or special character - contain at least (8) characters in length";
            echo $errors ['Password'];
            $valid = false;
        }
    }
    //Checks to make sure the email meets the rules of validation
    if(isset($_POST['Email'])) {
        if(!preg_match('/^([a-zA-Z0-9._-])+@[a-zA-Z0-9._-]+.([a-zA-Z]{2,4})$/',$_POST['Email'])) {
            $errors['Email']  = "Must be valid email address";
            echo $errors['Email'];
            $valid = false;
        }
    }

if(empty($errors)) {
        //This creates the connection to the database
        $con = mysql_connect("localhost", "Peter", "password");
        if(!$con) {
            die("Can not connect:" . mysql_error());
        }
        //Selects the database I wish to add the table into
        mysql_select_db("deepseadiving",$con);

        //Passes the entered information into the table called user
        $mysql=mysql_query("SELECT FROM User (Username) WHERE Username = '$_POST[Username]'");
        if(mysql_num_rows($mysql)>=0)
        {
        echo"Username already exists";
        }
        else
        {
        $sql = "INSERT INTO User (Username,Password,FirstName,LastName,Email,Address,ContactNumber) VALUES('$_POST[Username]','$_POST[Password]','$_POST[FirstName]','$_POST[LastName]','$_POST[Email]','$_POST[Address]','$_POST[ContactNumber]')";
        header("location:register.php");
        }       
        mysql_query($mysql,$con) or die(mysql_error($con));

        mysql_close($con);
    }
}
?>

1 Answers1

1

error is there

what happen that your $mysql is putting wrong query ro the db so it return FALSE it is not wrong then mysql_query will return resource to the table...

$mysql=mysql_query("SELECT Username FROM user WHERE Username='".$_POST['Username']."');

or for example

$mysql=mysql_query("SELECT * FROM table_name WHERE Username='".$_POST['Username']."');

Your insert query is also wrong..

 $sql = "INSERT INTO User (Username,Password,FirstName,LastName,Email,Address,ContactNumber) VALUES('".$_POST['Username']."','".$_POST['Password']."','".$_POST['FirstName']."','".$_POST['LastName']."','".$_POST['Email']."','".$_POST['Address']."','".$_POST['ContactNumber']."')";
HaRsH
  • 323
  • 3
  • 15