sudsy1970 Posted November 24, 2008 Share Posted November 24, 2008 Hi all, Just having some issues with mysql messages. I have the code below which seems to work connecting to my database and inserting data. If i do put in duplicate data it comes back and tells me that there is an error (duplicate entry) but i cannot seem to get my echo message to appear. Similarly if the data is written to the database i dont get the congrats message and the link to appear. Am a newbie to this and not that sure of the code but was wondering if it was to do with the or die(mysql) statement. Any ideas at all ?? cheers sudsy 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 Email must be unique. $sql = ("SELECT * from users WHERE Username='$User'"); $queryResult = mysql_query($sql) or die(mysql_error() . "<br>{$sql}"); if ($queryResult == 1) { echo "sorry that username has been taken. Please try again"; } 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) or die(mysql_error() . "<br>{$sql}"); echo "Congratulations you have been registered <br>"; echo " please return to the homepage to log in <br>"; echo ("<a href=\"homepage.php\">Back to homepage</a>"); } // will show if there has been an error and tell what error it is if (mysql_error()) { echo ("<a href=\"register.php\">Back to Register</a>"); mysql_error(); } Quote Link to comment https://forums.phpfreaks.com/topic/134109-solved-help-with-posting-messages/ Share on other sites More sharing options...
revraz Posted November 24, 2008 Share Posted November 24, 2008 Post the entire script. Quote Link to comment https://forums.phpfreaks.com/topic/134109-solved-help-with-posting-messages/#findComment-698082 Share on other sites More sharing options...
premiso Posted November 24, 2008 Share Posted November 24, 2008 // will show if there has been an error and tell what error it is if (mysql_error()) { echo ("<a href=\"register.php\">Back to Register</a>"); mysql_error(); } That will never get executed. After each mysql_query you DIE the script. So that check is pointless unless you remove the die's after each mysql_query. As for why it is not echoing, no clue. Also I would not check $queryResult == 1 I would check $queryResult > 0 just to be safe. Quote Link to comment https://forums.phpfreaks.com/topic/134109-solved-help-with-posting-messages/#findComment-698089 Share on other sites More sharing options...
sudsy1970 Posted November 24, 2008 Author Share Posted November 24, 2008 Entire script <?php session_start(); $Firstname = $_POST['Firstname']; $Surname = $_POST['Surname']; $Email = $_POST['Email']; $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($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 Email must be unique. $sql = ("SELECT * from users WHERE Username='$User'"); $queryResult = mysql_query($sql) or die(mysql_error() . "<br>{$sql}"); if ($queryResult == 1) { echo "sorry that username has been taken. Please try again"; } 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) or die(mysql_error() . "<br>{$sql}"); echo "Congratulations you have been registered <br>"; echo " please return to the homepage to log in <br>"; echo ("<a href=\"homepage.php\">Back to homepage</a>"); } // will show if there has been an error and tell what error it is if (mysql_error()) { echo ("<a href=\"register.php\">Back to Register</a>"); mysql_error(); } } 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 https://forums.phpfreaks.com/topic/134109-solved-help-with-posting-messages/#findComment-698096 Share on other sites More sharing options...
premiso Posted November 24, 2008 Share Posted November 24, 2008 You have a syntax error here: if (mysql_error()) { echo ("<a href=\"register.php\">Back to Register</a>"); mysql_error(); } } // extra paran else { echo 'The following errors were found:<ul>'; foreach ($errors as $error) { echo "<li>$error</li><br>"; echo '</ul>'; } include ("registerform.html"); } ?> I would suggest turning on the following to show errors while developing. error_reporting(E_ALL); ini_set('display_errors', 1); Also see above about the mysql_error reporting. It will never be reached if there is an error because of the die statements. Quote Link to comment https://forums.phpfreaks.com/topic/134109-solved-help-with-posting-messages/#findComment-698101 Share on other sites More sharing options...
sudsy1970 Posted November 24, 2008 Author Share Posted November 24, 2008 ok thanks, do i just put those two error reporting statements at the top of the page ? and if i changed $queryResult = mysql_query($sql) or die(mysql_error() . "<br>{$sql}"); to just $queryResult = mysql_query($sql); would that be correct ? Quote Link to comment https://forums.phpfreaks.com/topic/134109-solved-help-with-posting-messages/#findComment-698108 Share on other sites More sharing options...
premiso Posted November 24, 2008 Share Posted November 24, 2008 ok thanks, do i just put those two error reporting statements at the top of the page ? and if i changed $queryResult = mysql_query($sql) or die(mysql_error() . "<br>{$sql}"); to just $queryResult = mysql_query($sql); would that be correct ? Either $queryResult is correct. I was just stating that since you have that die portion, there is no need to do an if check cause it would never get called upon. It would be like setting up an if statement like this: <?php if ($i == 1) { echo "$i = 1"; }else { if ($i == 1) { echo 'This will never be true and this statement will never get executed.'; } } ?> Yes you would just put them at the top of the page and remove them when you are done testing. Quote Link to comment https://forums.phpfreaks.com/topic/134109-solved-help-with-posting-messages/#findComment-698128 Share on other sites More sharing options...
sudsy1970 Posted November 24, 2008 Author Share Posted November 24, 2008 ok i see what you are saying. i have removed the die statement now as i want the echo's to be displayed. i now get an error of Parse error: syntax error, unexpected $end in registerdetails.php on line 120 yet i do no have that many lines. I can't see any missing ; or { } either. <?php error_reporting(E_ALL); ini_set('display_errors', 1); session_start(); $Firstname = $_POST['Firstname']; $Surname = $_POST['Surname']; $Email = $_POST['Email']; $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($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 Email must be unique. $sql = ("SELECT * from users WHERE Username='$User'"); $queryResult = mysql_query($sql); if ($queryResult > 0) { echo "sorry that username has been taken. Please try again"; } 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); echo "Congratulations you have been registered <br>"; echo " please return to the homepage to log in <br>"; echo ("<a href=\"homepage.php\">Back to homepage</a>"); } // will show if there has been an error and tell what error it is if (mysql_error()) { echo ("<a href=\"register.php\">Back to Register</a>"); mysql_error(); } 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 https://forums.phpfreaks.com/topic/134109-solved-help-with-posting-messages/#findComment-698144 Share on other sites More sharing options...
revraz Posted November 24, 2008 Share Posted November 24, 2008 Maybe missing a } here for the first IF if (count($errors) == 0) { // Connect to mysql $dbServer = mysql_connect("localhost", "0274148", "8lgn62"); mysql_select_db("db0274148", $dbServer); if (!$dbServer) { Quote Link to comment https://forums.phpfreaks.com/topic/134109-solved-help-with-posting-messages/#findComment-698153 Share on other sites More sharing options...
sudsy1970 Posted November 24, 2008 Author Share Posted November 24, 2008 absolutely awesome, well spotted and it even works. I thought that the die statement might have caused the problems, at least i understand better now. Thanks for the advice only a few more problems to go and i might even get a working website lol. Quote Link to comment https://forums.phpfreaks.com/topic/134109-solved-help-with-posting-messages/#findComment-698183 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.