sudsy1970 Posted November 17, 2008 Share Posted November 17, 2008 hi newbie still trying to figure out this php form validation. I have managed to get the form validated and will output the errors on the form if I put some deliberate errors in. however if i don't put any errors in i get the following message: Notice: Undefined variable: errors in /home/stud/0/0274148/public_html/Assignment/registerdetails.php on line 67 but it does connect and find that i have a duplicate entry, but doesn't output the error message as can be seen below. connected to the databaseDuplicate entry 'sudsy' for key 1 Why is this please guys. Here is the code <? session_start(); $Firstname = $_POST['Firstname']; $Surname = $_POST['Surname']; $Email = $_POST['Email']; $Dob = $_POST['Dob']; $Password1 = $_POST['Password1']; $Password2 = $_POST['Password2']; $User = $_POST['User']; if(empty($Firstname)) { $errors[]='Please enter your Firstname'; } if(empty($Surname)) { $errors[]='Please enter your Surname'; } if(empty($User)) { $errors[]='You must select a Username'; } if(empty($Dob)) { $errors[]='Date of Birth'; } /*elseif(!is_numeric($Dob)) { $errors[]='Please enter your Date of Birth using numbers...I.E 15/09/2000<br>'; }*/ if(empty($Email)) { $errors[]='Email address'; } // Email verification amended from http://www.plus2net.com/php_tutorial/php_email_validation.php if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $Email)) { $errors[]='This is not a valid email address'; } if(strlen($Password1) < 6) { $errors[]="longer"; } if(empty($Password1)) { $errors[]='No Password'; } if($Password1 != $Password2) { $errors[]='Do not Match'; } if (count($errors)==0) { // Connect to mysql $dbServer=mysql_connect("localhost","0274148","8lgn62"); if (!$dbServer) {echo "Failed to connect to MySQL"; exit; } else { echo"connected to the database"; } //Checks the database for existing records before writing.Username is primary key and Email must be unique. mysql_select_db("db0274148",$dbServer); $sql=("SELECT * from users WHERE name= '$User' AND name= '$Email'"); $queryResult=mysql_query($sql); if ($queryResult==0) { // Inserts the data from the register page into the database mysql_select_db("db0274148",$dbServer); mysql_query("INSERT into users (Firstname, Surname, Email, Dob, Password, Username) VALUES ('$Firstname', '$Surname', '$Email', '$Dob', '$Password1', '$User') ") or die(mysql_error()); } else { echo"Sorry that username has already been chosen"; // Return to the form } // will show if there has been an error and tell what error it is if (mysql_error()) { print ("There has been an error<BR>".mysql_error()); exit; } } else { echo'The following errors were found:<ul>'; foreach($errors as $error) { echo "<li>$error</li><br>"; } echo'</ul>'; include("registerform.html"); } ?> Quote Link to comment Share on other sites More sharing options...
ratcateme Posted November 17, 2008 Share Posted November 17, 2008 change or die(mysql_error()); to or die(mysql_error()."<br>{$query}"); and also add $errors = array(); somewhere near the top. Scott. Quote Link to comment Share on other sites More sharing options...
sudsy1970 Posted November 17, 2008 Author Share Posted November 17, 2008 have tried that Scott but now get another error Notice: Undefined variable: query in /home/stud/0/0274148/public_html/Assignment/registerdetails.php on line 95 which seems to relate to the first bit of code. Is this an array and would require defining ?? Cheers Sudsy Quote Link to comment Share on other sites More sharing options...
ratcateme Posted November 17, 2008 Share Posted November 17, 2008 sorry to use to my own code replace mysql_query("INSERT into users (Firstname, Surname, Email, Dob, Password, Username) VALUES ('$Firstname', '$Surname', '$Email', '$Dob', '$Password1', '$User') ") or die(mysql_error()."<br>{$query}"); with $sql="INSERT into users (Firstname, Surname, Email, Dob, Password, Username) VALUES ('$Firstname', '$Surname', '$Email', '$Dob', '$Password1', '$User') "; mysql_query($sql) or die(mysql_error()."<br>{$sql}") Scott. Quote Link to comment Share on other sites More sharing options...
sudsy1970 Posted November 17, 2008 Author Share Posted November 17, 2008 ok that seems to have rid of most of the errors, sorry to be a pest but now how else do i call this sql statement if this isn't correct ? I have also tried naming them the records that they would be stored under in the database i.e User and Email but no change. Unknown column 'name' in 'where clause' SELECT * from users WHERE name="$User" AND name="$Email" Quote Link to comment Share on other sites More sharing options...
ratcateme Posted November 17, 2008 Share Posted November 17, 2008 can you post your full code now something strange is happening. Scott. Quote Link to comment Share on other sites More sharing options...
sudsy1970 Posted November 17, 2008 Author Share Posted November 17, 2008 here you go mate i am lost now lol, am i right in thinking that should be a valid sql query ? <? session_start(); $Firstname = $_POST['Firstname']; $Surname = $_POST['Surname']; $Email = $_POST['Email']; $Dob = $_POST['Dob']; $Password1 = $_POST['Password1']; $Password2 = $_POST['Password2']; $User = $_POST['User']; $errors = array(); if(empty($Firstname)) { $errors[]='Please enter your Firstname'; } if(empty($Surname)) { $errors[]='Please enter your Surname'; } if(empty($User)) { $errors[]='You must select a Username'; } if(empty($Dob)) { $errors[]='Date of Birth'; } /*elseif(!is_numeric($Dob)) { $errors[]='Please enter your Date of Birth using numbers...I.E 15/09/2000<br>'; }*/ if(empty($Email)) { $errors[]='Email address'; } // Email verification amended from http://www.plus2net.com/php_tutorial/php_email_validation.php if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $Email)) { $errors[]='This is not a valid email address'; } if(strlen($Password1) < 6) { $errors[]="longer"; } if(empty($Password1)) { $errors[]='No Password'; } if($Password1 != $Password2) { $errors[]='Do not Match'; } if (count($errors)==0) { // Connect to mysql $dbServer=mysql_connect("localhost","0274148","8lgn62"); if (!$dbServer) {echo "Failed to connect to MySQL"; exit; } else { echo"connected to the database"; } //Checks the database for existing records before writing.Username is primary key and Email must be unique. mysql_select_db("db0274148",$dbServer); $sql=("SELECT * from users WHERE name='$User' AND name='$Email'"); $queryResult=mysql_query($sql); if ($queryResult==0) { // Inserts the data from the register page into the database mysql_select_db("db0274148",$dbServer); mysql_query("INSERT into users (Firstname, Surname, Email, Dob, Password, Username) VALUES ('$Firstname', '$Surname', '$Email', '$Dob', '$Password1', '$User') "); mysql_query($sql) or die(mysql_error()."<br>{$sql}"); } else { $errors[]='Sorry that username has already been chosen'; // Return to the form } // will show if there has been an error and tell what error it is if (mysql_error()) { print ("There has been an error<BR>".mysql_error()); exit; } } else { echo'The following errors were found:<ul>'; foreach($errors as $error) { echo "<li>$error</li><br>"; } echo'</ul>'; include("registerform.html"); } ?> Quote Link to comment Share on other sites More sharing options...
ratcateme Posted November 17, 2008 Share Posted November 17, 2008 ok i made some changes and you only need to select the db once and what i meant before was to replace the whole query with my code but i have done that for you. <?php session_start(); $Firstname = $_POST['Firstname']; $Surname = $_POST['Surname']; $Email = $_POST['Email']; $Dob = $_POST['Dob']; $Password1 = $_POST['Password1']; $Password2 = $_POST['Password2']; $User = $_POST['User']; $errors = array(); if (empty($Firstname)) { $errors[] = 'Please enter your Firstname'; } if (empty($Surname)) { $errors[] = 'Please enter your Surname'; } if (empty($User)) { $errors[] = 'You must select a Username'; } if (empty($Dob)) { $errors[] = 'Date of Birth'; } /*elseif(!is_numeric($Dob)) { $errors[]='Please enter your Date of Birth using numbers...I.E 15/09/2000<br>'; }*/ if (empty($Email)) { $errors[] = 'Email address'; } // Email verification amended from http://www.plus2net.com/php_tutorial/php_email_validation.php if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $Email)) { $errors[] = 'This is not a valid email address'; } if (strlen($Password1) < 6) { $errors[] = "longer"; } if (empty($Password1)) { $errors[] = 'No Password'; } if ($Password1 != $Password2) { $errors[] = 'Do not Match'; } if (count($errors) == 0) { // Connect to mysql $dbServer = mysql_connect("localhost", "0274148", "8lgn62"); mysql_select_db("db0274148", $dbServer); if (!$dbServer) { echo "Failed to connect to MySQL"; exit; } else { echo "connected to the database"; } //Checks the database for existing records before writing.Username is primary key and Email must be unique. $sql = ("SELECT * from users WHERE name='$User' AND name='$Email'"); $queryResult = mysql_query($sql) or die(mysql_error() . "<br>{$sql}"); if ($queryResult == 0) { // Inserts the data from the register page into the database $sql = "INSERT into users (Firstname, Surname, Email, Dob, Password, Username) VALUES ('$Firstname', '$Surname', '$Email', '$Dob', '$Password1', '$User') "; mysql_query($sql) or die(mysql_error() . "<br>{$sql}"); } else { $errors[] = 'Sorry that username has already been chosen'; // Return to the form } // will show if there has been an error and tell what error it is if (mysql_error()) { print ("There has been an error<BR>" . mysql_error()); exit; } } else { echo 'The following errors were found:<ul>'; foreach ($errors as $error) { echo "<li>$error</li><br>"; } echo '</ul>'; include ("registerform.html"); } ?> Scott. Quote Link to comment Share on other sites More sharing options...
sudsy1970 Posted November 17, 2008 Author Share Posted November 17, 2008 Am still getting the error code. have cut and pasted your code directly into the file connected to the databaseUnknown column 'name' in 'where clause' SELECT * from users WHERE name='sudsy' AND name='A.Suddes@wlv.ac.uk' Quote Link to comment Share on other sites More sharing options...
ratcateme Posted November 17, 2008 Share Posted November 17, 2008 in the first query where you try to check for existing username it is saying there is no column called name and taking a closer look at the query it will fail because you are saying you want `name` to equal $user and $email at the same time which is imposable SELECT * from users WHERE name='$User' AND name='$Email' should this look like SELECT * from users WHERE name='$User' because other wise you could get people with the same username but different emails Scott. Quote Link to comment Share on other sites More sharing options...
sudsy1970 Posted November 18, 2008 Author Share Posted November 18, 2008 Scott, the problem i am getting here is the name part of the sql statement SELECT * from users WHERE name='$User' the error code i think isn't accepting this as i always get Unknown column 'name' in 'where clause' SELECT * from users WHERE name='A.Suddes@wlv.ac.uk' or Unknown column 'name' in 'where clause' SELECT * from users WHERE name='sudsy' Quote Link to comment Share on other sites More sharing options...
sudsy1970 Posted November 18, 2008 Author Share Posted November 18, 2008 In a moment of stupidity i have now found the answer it should have been SELECT * from users WHERE Username='sudsy' Thanks for all your help Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.