mykdel Posted April 9, 2011 Share Posted April 9, 2011 Hi All- Im new to PHP (and posting to forums) but really need some assistance w/ this code. This is a 3rd party code and i'm trying to create a registration page on my site. The problem is that I keep getting the following error: **** An error occurred in script '/homepages/11/d123456789/htdocs/mywebsite/login/register.php' on line 58: mysql_num_rows(): supplied argument is not a valid MySQL result resource **** I've been dealing w/ other line errors in the code but this one is giving me issues. If I need to post additional info, please let me know. Thanks in advance!!!! <?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: $fn = $ln = $e = $p = FALSE; // Check for a first name: if (preg_match ('/^[A-Z \'.-]{2,20}$/i', $trimmed['first_name'])) { $fn = mysql_real_escape_string($trimmed['first_name'], $dbc); } 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 = mysql_real_escape_string($trimmed['last_name'], $dbc); } 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 = mysql_real_escape_string($trimmed['email'], $dbc); } 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 = mysql_real_escape_string($trimmed['password1'], $dbc); } 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 ($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 = mysql_query ($q, $dbc); if (mysql_num_rows($r) == 0) { // Available.1 // Create the activation code: $a = md5(uniqid(rand(), true)); // Add the user to the database: $q = "INSERT INTO users (email, pass, first_name, last_name, active, registration_date) VALUES ('$e', SHA1('$p'), '$fn', '$ln', '$a', NOW() )"; $r = mysql_query ($q, $dbc); if (mysql_affected_rows($dbc) == 1) { // If it ran OK. // Send the email: $body = "Thank you for registering at <Website>. 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: you@email.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>'; } mysql_close(); } // 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> </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 https://forums.phpfreaks.com/topic/233214-newb-in-need-of-help-w-registration-form-pls/ Share on other sites More sharing options...
Pikachu2000 Posted April 9, 2011 Share Posted April 9, 2011 That error is an indication the query has failed. What are the other errors you're getting? Quote Link to comment https://forums.phpfreaks.com/topic/233214-newb-in-need-of-help-w-registration-form-pls/#findComment-1199342 Share on other sites More sharing options...
mykdel Posted April 9, 2011 Author Share Posted April 9, 2011 That is the only error that im getting... the prior errors have been resolved. Quote Link to comment https://forums.phpfreaks.com/topic/233214-newb-in-need-of-help-w-registration-form-pls/#findComment-1199345 Share on other sites More sharing options...
Pikachu2000 Posted April 9, 2011 Share Posted April 9, 2011 For the time being, make the changes below to report the actual mysql error, and echo the query string. // Make sure the email address is available: $q = "SELECT user_id FROM users WHERE email='$e'"; $r = mysql_query($q, $dbc) or die( "<br>Query: $q<br>Failed with error: " . mysql_error() ); // <---- HERE. Quote Link to comment https://forums.phpfreaks.com/topic/233214-newb-in-need-of-help-w-registration-form-pls/#findComment-1199348 Share on other sites More sharing options...
mykdel Posted April 9, 2011 Author Share Posted April 9, 2011 recv'd the following: Query: SELECT user_id FROM users WHERE email='mike@superiortel.net' Failed with error: No database selected And here is the connect info: // Set the database access information as constants: DEFINE ('DB_USER', 'dboXXXXXXXXX'); DEFINE ('DB_PASSWORD', 'XXXX'); DEFINE ('DB_HOST', 'XXXXXXXX'); DEFINE ('DB_NAME', 'XXXXXXX'); // Make the connection: $dbc = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); Quote Link to comment https://forums.phpfreaks.com/topic/233214-newb-in-need-of-help-w-registration-form-pls/#findComment-1199353 Share on other sites More sharing options...
Pikachu2000 Posted April 9, 2011 Share Posted April 9, 2011 mysql_connect doesn't take the database name as an argument. The db needs to be selected with mysql_select_db, so you'd need to remove the DB_NAME from mysql_connect then on the next line, add; mysql_select_db(DB_NAME); Quote Link to comment https://forums.phpfreaks.com/topic/233214-newb-in-need-of-help-w-registration-form-pls/#findComment-1199356 Share on other sites More sharing options...
mykdel Posted April 9, 2011 Author Share Posted April 9, 2011 THAT DID THE TRICK!!!!!! I got a couple more errors but was able get those fixed... All is working as designed. I am very happy that i found phpfreaks !! :hail_freaks: Quote Link to comment https://forums.phpfreaks.com/topic/233214-newb-in-need-of-help-w-registration-form-pls/#findComment-1199374 Share on other sites More sharing options...
llorax387 Posted June 10, 2011 Share Posted June 10, 2011 I am using the exact same code but I am getting this error message An error occurred in script '/home/anne/domains/umtpg.org/public_html/login/register.php' on line 30: mysql_real_escape_string() expects parameter 1 to be string, resource given Any help would be appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/233214-newb-in-need-of-help-w-registration-form-pls/#findComment-1227881 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.