Jump to content

Php Error, Help!


bcrider83

Recommended Posts

I am trying to write a registration code so people can register for my web site (login code to come once I get this done), I have gotten it to work and register the person but then thought of the proplem of duplicate usernames and am trying to correct this possiblity but keep getting an error. Any help would be appreciated.code used:<?php$con = mysql_connect("localhost","myusername","mypassword");if (!$con) { die('Could not Connect: ' . mysql_error()); }mysql_select_db("mydatabase", $con);$email = mysql_query("SELECT * FROM email WHERE (email='$email')"); if (mysql_num_rows($user) > 0) { showheader("Email Already Registered !"); die('We are sorry to inform you that this email address is already registered'); } else$user = mysql_query("SELECT * FROM username WHERE (username='$username')"); if (mysql_num_rows($user) > 0) { showheader("User Name Taken !"); die('We are sorry to inform you that this username is already Taken'); } else $sql="INSERT INTO users (firstname, lastname, yearborn, email, username, password)Values('$_POST[firstname]','$_POST[lastname]','$_POST[yearborn]','$_POST','$_POST[username]','$_POST[password]')";if (!mysql_query($sql,$con)) { die('Error: '. mysql_error()); }echo 'You are now registered, please login';mysql_close($con);?>The above code keeps returning this error:Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/myprecio/public_html/login/register.php on line 11You are now registered, please loginThank everyone in advance for any help provided ( I am new to PHP, only about 3 days)

Link to comment
Share on other sites

$email = mysql_query("SELECT * FROM email WHERE (email='$email')");if (mysql_num_rows($user) > 0)
Should you be passing $email to mysql_num_rows? Or return the value of the first mysql_query into $user?
Link to comment
Share on other sites

Should you be passing $email to mysql_num_rows? Or return the value of the first mysql_query into $user?
sorry that should have been $email = mysql_query("SELECT * FROM email WHERE (email='$email')"); if (mysql_num_rows($email) > 0) { showheader("Email Already Registered !"); die('We are sorry to inform you that this email address is already registered'); }and I have changed that in the script, but it still gives me that same error, it seems that it doesn't recognize the mysql_num_rows.I am trying to make it so if someone tries to register with an e-mail address or username that has already been used it gives them that error.
Link to comment
Share on other sites

If PHP recognizes mysql_query, it will recognize mysql_num_rows. Most likely your query is returning false. You should check for that before passing the return value to mysql_num_rows. You should also check the return value of mysql_select_db.Please post the exact text of the error statement.

Link to comment
Share on other sites

If PHP recognizes mysql_query, it will recognize mysql_num_rows. Most likely your query is returning false. You should check for that before passing the return value to mysql_num_rows. You should also check the return value of mysql_select_db.Please post the exact text of the error statement.
here is the exact error textWarning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/myprecio/public_html/login/register.php on line 11Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/myprecio/public_html/login/register.php on line 19You are now registered, please loginwouldn't it c
Link to comment
Share on other sites

Exactly. The function is fine. The argument is not valid. (The argument is the variable you pass to the function in the parentheses.) As I said, something about your sql query is not working. Make sure you have selected a database, first.If you have, start testing things. Where does the value of $email get assigned? Have you examined it to make sure it contains what you think it contains? Are you getting it from $_POST data? If so, have you examined that array to make sure it contains what you think it does?Do you really have a table called "email" with a field called "email" ?

Link to comment
Share on other sites

I do not have a table called "email" I have a table called "users" with a colum called "email" would that be entered in this form?$email = mysql_query("SELECT * FROM users WHERE (email='$email')"); if (mysql_num_rows($email) > 0){ showheader("Email Already Registered !"); die('We are sorry to inform you that this email address is already registered');}again, thank you for all your help, it is much appreciated

Link to comment
Share on other sites

I'm not making promises, but that could be your whole problem. If not, it is certainly a big part of the problem. So, yes, try that, if you haven't already done so.
well that did make it so it wasn't giving me errors anymore, however it is not returning that this e-mail address or username is already registered either....any suggestions? the script posted above is the entire code for the registration and is simply taking information from a form to put into the database.how can I retrieve all information in the database to be sure that it actually is going in?
Link to comment
Share on other sites

in this spot:$email = mysql_query("SELECT * FROM users WHERE (email='$email')");try:$email = mysql_query("SELECT * FROM users WHERE email = '$email'");also here:$user = mysql_query("SELECT * FROM username WHERE (username='$username')");to:$user = mysql_query("SELECT * FROM username WHERE username = '$username'");and if that dosen't work make sure all capitalization and spelling is correct

Link to comment
Share on other sites

If mysql_num_rows doesn't work you can always try checking an array:

<?php// Check if e-mail is being used$result = mysql_query ('SELECT email FROM users WHERE email=\''.$email.'\' LIMIT 1') or die(mysql_error());$row = mysql_fetch_array ($result);if (!empty($row['email'])) {   echo 'Sorry, this e-mail is taken';   exit;}// Check if username already exists$result = mysql_query ('SELECT username FROM users WHERE username=\''.$username.'\' LIMIT 1') or die(mysql_error());$row = mysql_fetch_array ($result);if (!empty($row['username'])) {   echo 'Sorry, this username is taken.';   exit;}?>

Hope it helps!

Link to comment
Share on other sites

ok, thank you to everyone that helped, posted below is the code that ended up working for me$username = mysql_real_escape_string($_POST['username']);$email = mysql_real_escape_string($_POST['email']);$sql = mysql_query("SELECT email FROM users WHERE email = '$email'"); if (mysql_num_rows($sql)>0){ die('We are sorry to inform you that this email address is already registered');}else$sql = mysql_query("SELECT username FROM users WHERE username = '$username'"); if (mysql_num_rows($sql)>0){ die('We are sorry to inform you that this username is already registered');}else$query = "INSERT INTO users (user_id, firstname, lastname, yearborn, email, username, password) VALUES (0, '{$_POST['firstname']}', '{$_POST['lastname']}', '{$_POST['yearborn']}', '{$_POST['email']}', '{$_POST['username']}', '{$_POST['password']}')";if (@mysql_query ($query)) { print '<p>You are now registered<br /> You will be redirected back to the main page in 5 seconds.</p>';} else { print "<p>Could not add the entry because: <b>" . mysql_error() . "</b>. The query was $query.</p>";}

Link to comment
Share on other sites

I never use parentheses myself like we saw in the original code, but I tested it on the School's "try-it" page, and it worked okay. I'm not sure if the school uses mysql, though.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...