Frostbite Posted August 22, 2013 Share Posted August 22, 2013 Hello, I am fairly new at php and mysql but finding the book very useful. I am having trouble with trying to register an account I fill out the registration form but when I click submit all I get is "Please enter your username" I am not getting any error appearing so I can not find out what the problem is. Here is my code I hope it is easy enough to read. I am also using a test server on 000webhost ("http://testwebserver.comli.com/register.php") I also have a localhost. <?php # register.php require('includes/config.inc.php'); $page_title = 'Register'; if ($_SERVER['REQUEST_METHOD'] == 'POST') { require (MYSQL); $trimmed = array_map('trim', $_POST); $un = $e = $p = FALSE; if (preg_match ('/^[A-z \' .-]{4,20} $/i', $trimmed['user_name'])) { $un = mysqli_real_escape_string ($dbc, $$trimmed['user_name']); } else { echo '<p class="error">Please enter your username! </p>'; } if (filter_var($trimmed['email'], FILTER_VALIDATE_EMAIL)) { $e = mysqli_real_escape_string ($dbc, $trimmed['email']); } else { echo '<p class="error>Please enter a valid email address! </p>'; } 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 { '<p class="error">Please enter a vaild password!</p>'; } if ($un && $e && $p) { $q = "SELECT user_id From users WHERE user_name='$un', 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) { $a = md5(uniqid(rand() , true)); $q = "INSERT INTO users (user_name, email, pass, active, reg_date) VALUES ('$e', '$un', SHA1('$p'), '$a', NOW())"; $r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MYSQL Error:". mysqli_error($dbc)); if (mysqli_affected_rows($dbc) == 1) { $body = "Thank you for reistering at PowerPlay. To activate your account, please click on this link:\n\n"; $body .= BASE_URL . 'activate.php?x='.urencode($e) . "&y=$a"; mail($trimmed['email'], 'Registration Confirmation', $body, 'From alexp2510@hotmail.com'); echo '<h3>Thank you for registering! A confirmation email has been sent to your email address. Please click on the link in order to activate your account.</h3>'; exit(); } 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 { echo '<p class="error">That email address or username is already in use. If you have forgotten your password use the link at the right to have your password sent to you.</p>'; } } else { echo '<p class="error">Please try again.</p>'; } mysqli_close($dbc); } ?> <h1>Register</h1> <form action="register.php" method="POST"> <fieldset> <p><b>Username:</b> <input type="text" name="user_name" size="20" maxlength="20" value="<?php if (isset($trimmed['user_name'])) echo $_POST['user_name'];?>" /> </p> <p><b>Email Address:</b> <input type="text" name="email" size="30" maxlength="60" value="<?php if (isset($trimmed['email'])) echo $trimmed ['email']; ?>" /> </p> <p><b>Password:</b> <input type="password" name="password1" size="20" maxlength="20" value="<?php if (isset($trimmed['password1'])) echo $trimmed ['password1']; ?>" /> <small> Use only letters, numbers, and the underscore. <p>Must be between 4 and 20 characters long.</p></small></p> <p><b>Confirm Password:</b> <input type="password" name="password2" size="20" maxlength="20" value="<?php if (isset($trimmed['password2'])) echo $trimmed['password2']; ?>" /> </p> </fieldset> <div align="center"> <input type="submit" name="submit" value="Register" /></div> </form> Any help would be great. Thanks Alex Link to comment Share on other sites More sharing options...
Larry Posted August 23, 2013 Share Posted August 23, 2013 You've got a space in your regular expression for the username after the initial characters and before the $. This means that the username must end with a space, but you've trimmed the username, so it'll never end with a space. Link to comment Share on other sites More sharing options...
Frostbite Posted August 23, 2013 Author Share Posted August 23, 2013 I removed the spaces and it fixed the "Enter username" but I am now getting "Please try again" Thanks Alex Link to comment Share on other sites More sharing options...
Larry Posted August 24, 2013 Share Posted August 24, 2013 That would imply that this conditional is false: if ($un && $e && $p) To confirm that, print out the values of $un, $e, and $p. Link to comment Share on other sites More sharing options...
Frostbite Posted August 24, 2013 Author Share Posted August 24, 2013 Sorry to be a pain but could you explain what you mean by print or refer to a chapter in your book. Also I tired to change the condition from false to true $un && $e && $p = TRUE; I was given an error: #0 my_error_handler(1024, Query: SELECT user_id From users WHERE user_name='Frostbite', email='example@thisaddress.com' Thanks Alex Link to comment Share on other sites More sharing options...
webrawa Posted August 25, 2013 Share Posted August 25, 2013 What Larry means is confirm that your $un, $e and $p variables are actually being assigned values from the form by using "echo" or "print" to output these variable values to the screen .... (i.e echo $un ) to dispay the value assigned to the $un variable .... As far as your last error I believe you may be missing the "AND" in the WHERE clause of the sql statement: SELECT user_id FROM users WHERE username='Frostbite' AND email='example@thisaddress.com' -Randy Link to comment Share on other sites More sharing options...
Frostbite Posted August 25, 2013 Author Share Posted August 25, 2013 I managed to fix the problems with the SQL statement so thank you for your help on that. I am still just getting "please try again". I have tried to place the echo or print in 3-4 different places and no error appears echo $un; echo $p; echo $e; require('includes/config.inc.php'); $page_title = 'Register'; if ($_SERVER['REQUEST_METHOD'] == 'POST') { require (MYSQL); $trimmed = array_map('trim', $_POST); $un = $e = $p = false; require('includes/config.inc.php'); $page_title = 'Register'; if ($_SERVER['REQUEST_METHOD'] == 'POST') { require (MYSQL); $trimmed = array_map('trim', $_POST); $un = $e = $p = false; echo $un; echo $p; echo $e; if ($un && $e && $p) echo $un; echo $p; echo $e; { After I place the echo here I get an error on line 172: (Parse error: syntax error, unexpected T_ELSE in /home/a2687587/public_html/register.php on line 172) } else // Line 172 { echo '<p class="error">Please try again.</p>'; } mysqli_close($dbc); } I also placed echo also under all of the variables and it should the username and also the email but not the password. if (preg_match ('/^[A-z\'.-]{4,20}$/i',$trimmed['user_name'])) { $un = mysqli_real_escape_string ($dbc, $trimmed['user_name']); echo $un; // Variable 1 } else { echo '<p class="error">Please enter your username! </p>'; } if (filter_var($trimmed['email'], FILTER_VALIDATE_EMAIL)) { $e = mysqli_real_escape_string ($dbc, $trimmed['email']); echo $e; // Variable 2 } else { echo '<p class="error>Please enter a valid email address! </p>'; } if (preg_match ('/^\w{4-20}$/',$trimmed['password1'])) { if ($trimmed['password1'] == $trimmed['password2']) { $p = mysqli_real_escape_string ($dbc, $trimmed ['password1']); echo $p; // Variable 3 } else { echo '<p class="error">Your password did not match the confirmed password!</p>'; } } else { '<p class="error">Please enter a vaild password!</p>'; } Thanks Alex Link to comment Share on other sites More sharing options...
Larry Posted August 26, 2013 Share Posted August 26, 2013 Do this right before the if ($un && $e && $p) conditional (it doesn't make sense anywhere else): echo "<p>un: $un</p>"; Do that for all three variables to confirm their values. Note that in the one situation, you put your echo statements between the closing parenthesis of the conditional and the opening curly bracket. Hence the error. Link to comment Share on other sites More sharing options...
Frostbite Posted August 26, 2013 Author Share Posted August 26, 2013 I ran the echo's and all of the variable appeared un: Frostbite e: alexp2510@hotmail.com p: testpass An error has occurred in the script '/home/a2687587/public_html/register.php' on line : Query: INSERT INTO users (user_name, email, pass, active, reg_date) VALUES ('alexp2510@hotmail.com', 'Frostbite', SHA1('testpass'), '873ec23e8b761496dd2990ba20204075', NOW()) MYSQL Error:Duplicate entry 'alexp2510@hotmail.co' for key 'user_name' Date/Time: 8-26-13 15:08:53 #0 my_error_handler(1024, Query: INSERT INTO users (user_name, email, pass, active, reg_date) VALUES ('alexp2510@hotmail.com', 'Frostbite', SHA1('testpass'), '873ec23e8b761496dd2990ba20204075', NOW()) I then was given this error. I am unsure what it means duplicate entry Thanks Alex Link to comment Share on other sites More sharing options...
Larry Posted August 26, 2013 Share Posted August 26, 2013 The database index won't allow duplicate email addresses or usernames to be registered. You shouldn't see this error. The reason you are is that the SELECT query needs to be WHERE username OR email. You have AND. You can compare this with the code in the book. Link to comment Share on other sites More sharing options...
Frostbite Posted August 26, 2013 Author Share Posted August 26, 2013 That seemed to fix it. I also cleared the users database and tried to register again and it worked. Thank you Larry and also Webrawa for all your help on my problem. Thanks again Alex Link to comment Share on other sites More sharing options...
Recommended Posts