berry05 Posted December 3, 2008 Share Posted December 3, 2008 i added a username field to my regestartion page and also updated it on my mySQL...i get a error saying.. Please re-enter your passwords and try again. heres the code... thxs! <?php # Script 16.6 - register.php // This is the registration page for the site. require_once ('includes/config.inc.php'); $page_title = 'Register'; include ('includes/header.html'); if (isset($_POST['submitted'])) { // Handle the form. require_once (MYSQL); // Trim all the incoming data: $trimmed = array_map('trim', $_POST); // Assume invalid values: $un = $fn = $ln = $e = $p = FALSE; // Check for a first name: if (preg_match ('/^[A-Z \'.-]{2,20}$/i', $trimmed['first_name'])) { $fn = mysqli_real_escape_string ($dbc, $trimmed['first_name']); } else { echo '<p class="error">Please enter your first name!</p>'; } // Check for a last name: if (preg_match ('/^[A-Z \'.-]{2,40}$/i', $trimmed['last_name'])) { $ln = mysqli_real_escape_string ($dbc, $trimmed['last_name']); } else { echo '<p class="error">Please enter your last name!</p>'; } // Check for an email address: if (preg_match ('/^[\w.-]+@[\w.-]+\.[A-Za-z]{2,6}$/', $trimmed['email'])) { $e = mysqli_real_escape_string ($dbc, $trimmed['email']); } else { echo '<p class="error">Please enter a valid email address!</p>'; } // Check for a password and match against the confirmed password: if (preg_match ('/^\w{4,20}$/', $trimmed['password1']) ) { if ($trimmed['password1'] == $trimmed['password2']) { $p = mysqli_real_escape_string ($dbc, $trimmed['password1']); } else { echo '<p class="error">Your password did not match the confirmed password!</p>'; } } else { echo '<p class="error">Please enter a valid password!</p>'; } if ($un && $fn && $ln && $e && $p) { // If everything's OK... // Make sure the email address is available: $q = "SELECT user_id FROM users WHERE email='$e'"; $r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc)); if (mysqli_num_rows($r) == 0) { // Available. // Check for a username: if (preg_match ('/^[A-Z \'.-]{2,40}$/i', $trimmed['username'])) { $un = mysqli_real_escape_string ($dbc, $trimmed['username']); } else { echo '<p class="error">Username taken!</p>'; } // Create the activation code: $a = md5(uniqid(rand(), true)); // Add the user to the database: $q = "INSERT INTO users (username, email, pass, first_name, last_name, active, registration_date, gold,) VALUES ('$un', '$e', SHA1('$p'), '$fn', '$ln', '$a', NOW() ,'100')"; $r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc)); if (mysqli_affected_rows($dbc) == 1) { // If it ran OK. // Send the email: $body = "Thank you for registering at <http://98.217.81.34/>. To activate your account, please click on this link:\n\n"; $body .= BASE_URL . 'activate.php?x=' . urlencode($e) . "&y=$a"; mail($trimmed['email'], 'Registration Confirmation', $body, 'From: sandshifter0592@gmail.com'); // Finish the page: echo '<h3>Thank you for registering! A confirmation email has been sent to your address. Please click on the link in that email in order to activate your account.</h3>'; include ('includes/footer.html'); // Include the HTML footer. exit(); // Stop the page. } else { // If it did not run OK. echo '<p class="error">You could not be registered due to a system error. We apologize for any inconvenience.</p>'; } } else { // The email address is not available. echo '<p class="error">That email address has already been registered. If you have forgotten your password, use the link at right to have your password sent to you.</p>'; } } else { // If one of the data tests failed. echo '<p class="error">Please re-enter your passwords and try again.</p>'; } mysqli_close($dbc); } // End of the main Submit conditional. ?> <h1>Register</h1> <form action="register.php" method="post"> <fieldset> <p><b>First Name:</b> <input type="text" name="first_name" size="20" maxlength="20" value="<?php if (isset($trimmed['first_name'])) echo $trimmed['first_name']; ?>" /></p> <p><b>Last Name:</b> <input type="text" name="last_name" size="20" maxlength="40" value="<?php if (isset($trimmed['last_name'])) echo $trimmed['last_name']; ?>" /></p> <p><b>Email Address:</b> <input type="text" name="email" size="30" maxlength="80" value="<?php if (isset($trimmed['email'])) echo $trimmed['email']; ?>" /></p> <p><b>Password:</b> <input type="password" name="password1" size="20" maxlength="20" /> <small>Use only letters, numbers, and the underscore. Must be between 4 and 20 characters long.</small></p> <p><b>Confirm Password:</b> <input type="password" name="password2" size="20" maxlength="20" /></p> <p><strong>Username:</strong> <label> <input name="username" type="text" id="username" value="<?php if (isset($trimmed['username'])) echo $trimmed['username']; ?>" /> </label> </p> <p> </p> </fieldset> <div align="center"><input type="submit" name="submit" value="Register" /></div> <input type="hidden" name="submitted" value="TRUE" /> </form> <?php // Include the HTML footer. include ('includes/footer.html'); ?> Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted December 3, 2008 Share Posted December 3, 2008 In this section: if (mysqli_affected_rows($dbc) == 1) { // If it ran OK. // Send the email: $body = "Thank you for registering at <http://98.217.81.34/>. To activate your account, please click on this link:\n\n"; $body .= BASE_URL . 'activate.php?x=' . urlencode($e) . "&y=$a"; mail($trimmed['email'], 'Registration Confirmation', $body, 'From: sandshifter0592@gmail.com'); // Finish the page: echo '<h3>Thank you for registering! A confirmation email has been sent to your address. Please click on the link in that email in order to activate your account.</h3>'; include ('includes/footer.html'); // Include the HTML footer. exit(); // Stop the page. } else { // If it did not run OK. echo '<p class="error">You could not be registered due to a system error. We apologize for any inconvenience.</p>'; } } else { // The email address is not available. echo '<p class="error">That email address has already been registered. If you have forgotten your password, use the link at right to have your password sent to you.</p>'; } } else { // If one of the data tests failed. echo '<p class="error">Please re-enter your passwords and try again.</p>'; } You have 3 else clauses. You can only have one else clause per if clause. I think you need to change the else caluses to elseif clauses except for the last. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted December 3, 2008 Share Posted December 3, 2008 you don't set the value of $un until AFTER you test the value of it. move the username test outside the big IF statement: <?php # Script 16.6 - register.php // This is the registration page for the site. require_once ('includes/config.inc.php'); $page_title = 'Register'; include ('includes/header.html'); if (isset ($_POST['submitted'])) { // Handle the form. require_once (MYSQL); // Trim all the incoming data: $trimmed = array_map('trim', $_POST); // Assume invalid values: $un = $fn = $ln = $e = $p = FALSE; // Check for a first name: if (preg_match('/^[A-Z \'.-]{2,20}$/i', $trimmed['first_name'])) { $fn = mysqli_real_escape_string($dbc, $trimmed['first_name']); } else { echo '<p class="error">Please enter your first name!</p>'; } // Check for a last name: if (preg_match('/^[A-Z \'.-]{2,40}$/i', $trimmed['last_name'])) { $ln = mysqli_real_escape_string($dbc, $trimmed['last_name']); } else { echo '<p class="error">Please enter your last name!</p>'; } // Check for an email address: if (preg_match('/^[\w.-]+@[\w.-]+\.[A-Za-z]{2,6}$/', $trimmed['email'])) { $e = mysqli_real_escape_string($dbc, $trimmed['email']); } else { echo '<p class="error">Please enter a valid email address!</p>'; } // Check for a password and match against the confirmed password: if (preg_match('/^\w{4,20}$/', $trimmed['password1'])) { if ($trimmed['password1'] == $trimmed['password2']) { $p = mysqli_real_escape_string($dbc, $trimmed['password1']); } else { echo '<p class="error">Your password did not match the confirmed password!</p>'; } } else { echo '<p class="error">Please enter a valid password!</p>'; } // Check for a username: if (preg_match('/^[A-Z \'.-]{2,40}$/i', $trimmed['username'])) { $un = mysqli_real_escape_string($dbc, $trimmed['username']); } else { echo '<p class="error">Username taken!</p>'; } if ($un && $fn && $ln && $e && $p) { // If everything's OK... // Make sure the email address is available: $q = "SELECT user_id FROM users WHERE email='$e'"; $r = mysqli_query($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc)); if (mysqli_num_rows($r) == 0) { // Available. // Create the activation code: $a = md5(uniqid(rand(), true)); // Add the user to the database: $q = "INSERT INTO users (username, email, pass, first_name, last_name, active, registration_date, gold,) VALUES ('$un', '$e', SHA1('$p'), '$fn', '$ln', '$a', NOW() ,'100')"; $r = mysqli_query($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc)); if (mysqli_affected_rows($dbc) == 1) { // If it ran OK. // Send the email: $body = "Thank you for registering at <http://98.217.81.34/>. To activate your account, please click on this link:\n\n"; $body .= BASE_URL . 'activate.php?x=' . urlencode($e) . "&y=$a"; mail($trimmed['email'], 'Registration Confirmation', $body, 'From: sandshifter0592@gmail.com'); // Finish the page: echo '<h3>Thank you for registering! A confirmation email has been sent to your address. Please click on the link in that email in order to activate your account.</h3>'; include ('includes/footer.html'); // Include the HTML footer. exit (); // Stop the page. } else { // If it did not run OK. echo '<p class="error">You could not be registered due to a system error. We apologize for any inconvenience.</p>'; } } else { // The email address is not available. echo '<p class="error">That email address has already been registered. If you have forgotten your password, use the link at right to have your password sent to you.</p>'; } } else { // If one of the data tests failed. echo '<p class="error">Please re-enter your passwords and try again.</p>'; } mysqli_close($dbc); } // End of the main Submit conditional. ?> <h1>Register</h1> <form action="register.php" method="post"> <fieldset> <p><b>First Name:</b> <input type="text" name="first_name" size="20" maxlength="20" value="<?php if (isset($trimmed['first_name'])) echo $trimmed['first_name']; ?>" /></p> <p><b>Last Name:</b> <input type="text" name="last_name" size="20" maxlength="40" value="<?php if (isset($trimmed['last_name'])) echo $trimmed['last_name']; ?>" /></p> <p><b>Email Address:</b> <input type="text" name="email" size="30" maxlength="80" value="<?php if (isset($trimmed['email'])) echo $trimmed['email']; ?>" /></p> <p><b>Password:</b> <input type="password" name="password1" size="20" maxlength="20" /> <small>Use only letters, numbers, and the underscore. Must be between 4 and 20 characters long.</small></p> <p><b>Confirm Password:</b> <input type="password" name="password2" size="20" maxlength="20" /></p> <p><strong>Username:</strong> <label> <input name="username" type="text" id="username" value="<?php if (isset($trimmed['username'])) echo $trimmed['username']; ?>" /> </label> </p> <p> </p> </fieldset> <div align="center"><input type="submit" name="submit" value="Register" /></div> <input type="hidden" name="submitted" value="TRUE" /> </form> <?php // Include the HTML footer. include ('includes/footer.html'); ?> Quote Link to comment Share on other sites More sharing options...
gathos Posted December 3, 2008 Share Posted December 3, 2008 ok i didn't actually look through all of the code, but ngreenwood6 had a good point the multiple else can cause problems. If you need these try using a switch: <?php switch ($i) { case "apple": echo "i is apple"; break; case "bar": echo "i is bar"; break; case "cake": echo "i is cake"; break; } ?> If that doesn't help check out the manual http://ca3.php.net/switch They can explain a switch better then I ever could. Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted December 3, 2008 Share Posted December 3, 2008 Yeah i just took a quick glance at it but looking at it again I was wrong. He had multiple if statements before that and it was just ending the statements. I think that rhodesa has solved the issue. Quote Link to comment Share on other sites More sharing options...
berry05 Posted December 3, 2008 Author Share Posted December 3, 2008 i tried rhodesa code and i get a system error...im trying the else statement now Quote Link to comment Share on other sites More sharing options...
berry05 Posted December 3, 2008 Author Share Posted December 3, 2008 when i change the two else to elseif i get a error saying....... Parse error: syntax error, unexpected '{', expecting '(' in C:\wamp\www\register.php on line 86 Quote Link to comment Share on other sites More sharing options...
rhodesa Posted December 3, 2008 Share Posted December 3, 2008 i tried rhodesa code and i get a system error...im trying the else statement now what is the system error. all i did was move a block of code...should haven't caused any syntax problems... Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted December 3, 2008 Share Posted December 3, 2008 What error did you get with rhodesa's code? you do not need elseif's there sorry I just quickly glanced at your code. Quote Link to comment Share on other sites More sharing options...
berry05 Posted December 3, 2008 Author Share Posted December 3, 2008 A system error occurred. We apologize for the inconvenience. You could not be registered due to a system error. We apologize for any inconvenience. Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted December 3, 2008 Share Posted December 3, 2008 All you needed to do was put this code: if (preg_match ('/^[A-Z \'.-]{2,40}$/i', $trimmed['username'])) { $un = mysqli_real_escape_string ($dbc, $trimmed['username']); } else { echo '<p class="error">Username taken!</p>'; } above this code: if ($un && $fn && $ln && $e && $p) { // If everything's OK... The reason for this is because you are checking for a variable that hadn't been set yet with your original code. Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted December 3, 2008 Share Posted December 3, 2008 Not too good with mysqli but shouldn't this line: if (mysqli_affected_rows($dbc) == 1) be this: if (mysqli_affected_rows($r) == 1) My reason for saying that is because you are checking the connection for a change instead of the results. Please let me know if I am just thinking stupidly Quote Link to comment Share on other sites More sharing options...
berry05 Posted December 3, 2008 Author Share Posted December 3, 2008 ok i get a error saying... Parse error: syntax error, unexpected $end in C:\wamp\www\register.php on line 136 heres my code now.. <?php # Script 16.6 - register.php // This is the registration page for the site. require_once ('includes/config.inc.php'); $page_title = 'Register'; include ('includes/header.html'); if (isset($_POST['submitted'])) { // Handle the form. require_once (MYSQL); // Trim all the incoming data: $trimmed = array_map('trim', $_POST); // Assume invalid values: $un = $fn = $ln = $e = $p = FALSE; // Check for a first name: if (preg_match ('/^[A-Z \'.-]{2,20}$/i', $trimmed['first_name'])) { $fn = mysqli_real_escape_string ($dbc, $trimmed['first_name']); } else { echo '<p class="error">Please enter your first name!</p>'; } // Check for a last name: if (preg_match ('/^[A-Z \'.-]{2,40}$/i', $trimmed['last_name'])) { $ln = mysqli_real_escape_string ($dbc, $trimmed['last_name']); } else { echo '<p class="error">Please enter your last name!</p>'; } // Check for an email address: if (preg_match ('/^[\w.-]+@[\w.-]+\.[A-Za-z]{2,6}$/', $trimmed['email'])) { $e = mysqli_real_escape_string ($dbc, $trimmed['email']); } else { echo '<p class="error">Please enter a valid email address!</p>'; } // Check for a password and match against the confirmed password: if (preg_match ('/^\w{4,20}$/', $trimmed['password1']) ) { if ($trimmed['password1'] == $trimmed['password2']) { $p = mysqli_real_escape_string ($dbc, $trimmed['password1']); } else { echo '<p class="error">Your password did not match the confirmed password!</p>'; } } else { echo '<p class="error">Please enter a valid password!</p>'; } if ($un && $fn && $ln && $e && $p) { // If everything's OK... // Make sure the email address is available: $q = "SELECT user_id FROM users WHERE email='$e'"; $r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc)); if (mysqli_num_rows($r) == 0) { // Available. // Check for a username: if (preg_match ('/^[A-Z \'.-]{2,40}$/i', $trimmed['username'])) { $un = mysqli_real_escape_string ($dbc, $trimmed['username']); } else { echo '<p class="error">Username taken!</p>'; } if ($un && $fn && $ln && $e && $p) { // If everything's OK... // Create the activation code: $a = md5(uniqid(rand(), true)); // Add the user to the database: $q = "INSERT INTO users (username, email, pass, first_name, last_name, active, registration_date, gold,) VALUES ('$un', '$e', SHA1('$p'), '$fn', '$ln', '$a', NOW() ,'100')"; $r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc)); if (mysqli_affected_rows($dbc) == 1) { // If it ran OK. // Send the email: $body = "Thank you for registering at <http://98.217.81.34/>. To activate your account, please click on this link:\n\n"; $body .= BASE_URL . 'activate.php?x=' . urlencode($e) . "&y=$a"; mail($trimmed['email'], 'Registration Confirmation', $body, 'From: sandshifter0592@gmail.com'); // Finish the page: echo '<h3>Thank you for registering! A confirmation email has been sent to your address. Please click on the link in that email in order to activate your account.</h3>'; include ('includes/footer.html'); // Include the HTML footer. exit(); // Stop the page. } else { // If it did not run OK. echo '<p class="error">You could not be registered due to a system error. We apologize for any inconvenience.</p>'; } } else { // The email address is not available. echo '<p class="error">That email address has already been registered. If you have forgotten your password, use the link at right to have your password sent to you.</p>'; } } else { // If one of the data tests failed. echo '<p class="error">Please re-enter your passwords and try again.</p>'; } mysqli_close($dbc); } // End of the main Submit conditional. ?> <h1>Register</h1> <form action="register.php" method="post"> <fieldset> <p><b>First Name:</b> <input type="text" name="first_name" size="20" maxlength="20" value="<?php if (isset($trimmed['first_name'])) echo $trimmed['first_name']; ?>" /></p> <p><b>Last Name:</b> <input type="text" name="last_name" size="20" maxlength="40" value="<?php if (isset($trimmed['last_name'])) echo $trimmed['last_name']; ?>" /></p> <p><b>Email Address:</b> <input type="text" name="email" size="30" maxlength="80" value="<?php if (isset($trimmed['email'])) echo $trimmed['email']; ?>" /></p> <p><b>Password:</b> <input type="password" name="password1" size="20" maxlength="20" /> <small>Use only letters, numbers, and the underscore. Must be between 4 and 20 characters long.</small></p> <p><b>Confirm Password:</b> <input type="password" name="password2" size="20" maxlength="20" /></p> <p><strong>Username:</strong> <label> <input name="username" type="text" id="username" value="<?php if (isset($trimmed['username'])) echo $trimmed['username']; ?>" /> </label> </p> <p> </p> </fieldset> <div align="center"><input type="submit" name="submit" value="Register" /></div> <input type="hidden" name="submitted" value="TRUE" /> </form> <?php // Include the HTML footer. include ('includes/footer.html'); ?> Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted December 3, 2008 Share Posted December 3, 2008 which line is 136 Quote Link to comment Share on other sites More sharing options...
rhodesa Posted December 3, 2008 Share Posted December 3, 2008 you are not copying/pasting correctly. please just use the code i posted before Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted December 3, 2008 Share Posted December 3, 2008 You need to add a } before } // End of the main Submit conditional. Quote Link to comment Share on other sites More sharing options...
berry05 Posted December 3, 2008 Author Share Posted December 3, 2008 i used the code and i got ... A system error occurred. We apologize for the inconvenience. You could not be registered due to a system error. We apologize for any inconvenience. i just tried it again Quote Link to comment Share on other sites More sharing options...
rhodesa Posted December 3, 2008 Share Posted December 3, 2008 that is ok...it is still progress... comment this line out for now: echo '<p class="error">You could not be registered due to a system error. We apologize for any inconvenience.</p>'; and below it (still inside that else) put this: print "Affected rows: ".mysqli_affected_rows($dbc);exit; and let me know what it says. also, check your database to see if it is actually inserting the data or not Quote Link to comment Share on other sites More sharing options...
berry05 Posted December 3, 2008 Author Share Posted December 3, 2008 A system error occurred. We apologize for the inconvenience. Affected rows: -1 Quote Link to comment Share on other sites More sharing options...
rhodesa Posted December 3, 2008 Share Posted December 3, 2008 k...so seems like the query is failing...do you not have error reporting on? trigger_error() is probably running, but without the error reporting on, you would never see it. the problem in the query is the comma after gold...remove it so it looks like: $q = "INSERT INTO users (username, email, pass, first_name, last_name, active, registration_date, gold) VALUES ('$un', '$e', SHA1('$p'), '$fn', '$ln', '$a', NOW() ,'100')"; Quote Link to comment Share on other sites More sharing options...
berry05 Posted December 3, 2008 Author Share Posted December 3, 2008 wow thank you so much sir....it worked! 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.