sudsy1970 Posted November 28, 2008 Share Posted November 28, 2008 hi, i have a form to register users to a website, which works as long as the data is correct. i have tried to put in incorrect data deliberately to make sure my errors work but something has gone horribly wrong and i cannot connect to the database as $dbRecord is undefined. I think this is to do with my error statement being ==0, however i have also tried using < 0 and get the same results. If i set it to <0 and put errors in it works (as i would expect. Any clues on where to start please ? <? error_reporting(E_ALL); ini_set("display_errors",true); session_start(); $Firstname = mysql_real_escape_string(trim($_POST['Firstname'])); $Surname = mysql_real_escape_string(trim($_POST['Surname'])); $Email = mysql_real_escape_string(trim($_POST['Email'])); $Password1 = mysql_real_escape_string(trim($_POST['Password1'])); $Password2 = mysql_real_escape_string(trim($_POST['Password2'])); $User = mysql_real_escape_string(trim($_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($Email)) { $errors[] = 'Please enter an 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[] = "Your password must be 6 characters"; } if (empty($Password1)) { $errors[] = 'No Password'; } if ($Password1 != $Password2) { $errors[] = 'Your passwords 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<br>"; } //Checks the database for existing records before writing.Username is primary key and must be unique. $sql = ("SELECT * from users WHERE Username='$User'"); $queryResult = mysql_query($sql); if (mysql_num_rows($queryResult)>0) { echo "Please fill in the form again<br>"; $errors[]="Sorry the username you selected is already taken by another member<br>"; } else { // Inserts the data from the register page into the database $sql = "INSERT into users (Firstname, Surname, Email, Password, Username) VALUES ('$Firstname', '$Surname', '$Email', '$Password1', '$User') "; mysql_query($sql); header("Location:homepage.php"); } // will show if there has been an error and tell what error it is if (mysql_error()) { mysql_error(); } else { echo 'The following errors were found:<ul>'; foreach ($errors as $error) { echo "<li>$error</li><br>"; echo '</ul>'; } include ("registerform.html"); } ?> Link to comment https://forums.phpfreaks.com/topic/134582-solved-problem-when-testing-registration/ Share on other sites More sharing options...
vbnullchar Posted November 28, 2008 Share Posted November 28, 2008 may be the $error array is greater than 1... try using isset($error) Link to comment https://forums.phpfreaks.com/topic/134582-solved-problem-when-testing-registration/#findComment-700755 Share on other sites More sharing options...
sudsy1970 Posted November 28, 2008 Author Share Posted November 28, 2008 thank you excellent suggestion Link to comment https://forums.phpfreaks.com/topic/134582-solved-problem-when-testing-registration/#findComment-700761 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.