mattd8752 Posted January 21, 2007 Share Posted January 21, 2007 I get the following error:[quote]Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/mattd/public_html/f1/register.php on line 29Sorry, that username is already taken.[/quote]From the following code:[code]<?phpinclude "dbconnect.php"; if(!isset($_POST['register']) || !isset($_POST['regkey']) || !isset($_POST['username']) || !isset($_POST['password']) || !isset($_POST['email'])){?><html><head><title>Registration Form</title></head><body><form method="POST" action="register.php">Username:<input type="text" name="username" size="20"><br>Password:<input type="password" name="password" size="20"><br>Email:<input type="text" name="email" size="20">Registration Key:<input type="text" name="regkey" size="20"> (you need to get a registration key to sign up)<br><input type="submit" value="Submit" name="register"></form></body></html><?php }else{//reg them$result=mysql_query("select * from users where username='$username'"); if(mysql_num_rows($result) != "1"){ echo "Sorry, that username is already taken."; }else{ echo "Username is available...<br>"; $pass = md5($_POST['password']); mysql_query("INSERT INTO users (username, password, regkey) VALUES($name, $hp, $username ) "); echo "Your account should be registered. Try logging in! <a href=\"login.php\">Login Page</a>"; } }?>[/code]Please help with this one if you can, I am not quite sure what is causing the error. Also, that user name is available. It should say your account has been registered with the error if it were to be working properly how I want it to as well (but the error has to go anyway :D ) anyways, if you know what the problem is please reply.Thanks in advanced -- Matt Quote Link to comment Share on other sites More sharing options...
smc Posted January 21, 2007 Share Posted January 21, 2007 Now I'm not exactly sure what causes the problem but I had this difficulty earlier today and this is what was suggested to me to change it to in order to make it more efficent[code]$found = mysql_result(mysql_query('SELECT count(0) FROM `users` WHERE `username` = "'.$username.'" LIMIT 1'), 0, 0);if ( $found > 0 ){ echo ("Sorry, that username is already taken."); die;}else{echo "Username is available...<br>"; $pass = md5($_POST['password']); mysql_query("INSERT INTO users (username, password, regkey) VALUES($name, $hp, $username ) "); echo "Your account should be registered. Try logging in! <a href=\"login.php\">Login Page</a>"; } }?>[/code]Some variables might need to be changed to suit your needs Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted January 21, 2007 Share Posted January 21, 2007 You forgot to transfer your $_POST variable.Try this:[code]<?phpinclude "dbconnect.php"; if(!isset($_POST['register']) || !isset($_POST['regkey']) || !isset($_POST['username']) || !isset($_POST['password']) || !isset($_POST['email'])){?><html><head><title>Registration Form</title></head><body><form method="POST" action="register.php">Username:<input type="text" name="username" size="20"><br>Password:<input type="password" name="password" size="20"><br>Email:<input type="text" name="email" size="20">Registration Key:<input type="text" name="regkey" size="20"> (you need to get a registration key to sign up)<br><input type="submit" value="Submit" name="register"></form></body></html><?php }else{//reg them$username= $_POST['username'];$result=mysql_query("select * from users where username='$username'"); if(mysql_num_rows($result) != "1"){ echo "Sorry, that username is already taken."; }else{ echo "Username is available...<br>"; $pass = md5($_POST['password']); mysql_query("INSERT INTO users (username, password, regkey) VALUES($name, $hp, $username ) "); echo "Your account should be registered. Try logging in! <a href=\"login.php\">Login Page</a>"; } }?>[/code] Quote Link to comment Share on other sites More sharing options...
mattd8752 Posted January 21, 2007 Author Share Posted January 21, 2007 [quote author=smc link=topic=123423.msg510098#msg510098 date=1169423647]Now I'm not exactly sure what causes the problem but I had this difficulty earlier today and this is what was suggested to me to change it to in order to make it more efficent[code]$found = mysql_result(mysql_query('SELECT count(0) FROM `users` WHERE `username` = "'.$username.'" LIMIT 1'), 0, 0);if ( $found > 0 ){ echo ("Sorry, that username is already taken."); die;}else{echo "Username is available...<br>"; $pass = md5($_POST['password']); mysql_query("INSERT INTO users (username, password, regkey) VALUES($name, $hp, $username ) "); echo "Your account should be registered. Try logging in! <a href=\"login.php\">Login Page</a>"; } }?>[/code]Some variables might need to be changed to suit your needs[/quote]Now what is the use of count(0), what does it do? I don't quite get that part. Quote Link to comment Share on other sites More sharing options...
trochia Posted January 22, 2007 Share Posted January 22, 2007 http://www.php.net/manual/en/function.count.php Quote Link to comment Share on other sites More sharing options...
mattd8752 Posted January 22, 2007 Author Share Posted January 22, 2007 Ok, I've got my second version of the script here, and its got some MAJOR bugs.I have no idea how I've gotten to these, but:Register.php:[code]<?phpinclude "dbconnect.php"; if(!isset($_POST['register']) || !isset($_POST['regkey']) || !isset($_POST['username']) || !isset($_POST['password']) || !isset($_POST['email'])){?><html><head><title>Registration Form</title></head><body><form method="POST" action="register.php">Username:<input type="text" name="username" size="20"><br>Password:<input type="password" name="password" size="20"><br>Email:<input type="text" name="email" size="20"><br>Registration Key:<input type="text" name="regkey" size="20"> (you need to get a registration key to sign up)<br><input type="submit" value="Submit" name="register"></form></body></html><?php }else{//reg them$username = $_POST['username'];$key = $_POST['regkey'];$pass = $_POST['password'];$pass = md5($pass);$email = $_POST['email'];$found = mysql_result(mysql_query('SELECT count(0) FROM `users` WHERE `username` = "'.$username.'" LIMIT 1'), 0, 0); if ( $found > 0 ){ echo ("Sorry, that username is already taken."); die; }else{echo "Username is available...<br>"; $pass = md5($_POST['password']); mysql_query("INSERT INTO users (username, password, regkey, email) VALUES($username, $pass, $key, $email) "); echo "Your account should be registered. Try logging in! <a href=\"login.php\">Login Page</a>"; } }?>[/code]It looks like its working on that page, but nothing is added to the DB. Also the following error:[quote]Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/mattd/public_html/f1/register.php on line 33[/quote]But wait, thats not it, I've got a problem on my next page (yeah... it sucks).I have the login page allowing me to login as anyone (doesn't that rock... if only I wasn't trying to fix it).[code]<?ob_start();session_start();session_destroy();$message="";$Login=$_POST['Login'];if(isset($Login)){$username=$_POST['username'];$md5=md5($_POST['password']);// Connect databaseinclude "dbconnect.php";// Check matching of username and password.$result=mysql_query("select * from users where username='$username'");if(mysql_num_rows($result)!='0'){ // If match.session_register("username");header("location:main.php");exit;}else{ // If not match.$message="The username or password you have entered is incorrect.<br>";}}?><html><head><title>Login</title></head><body><? echo $message; ?><form id="form1" name="form1" method="post" action="<? echo $PHP_SELF; ?>"><table><tr><td>User : </td><td><input name="username" type="text" id="username" /></td></tr><tr><td>Password : </td><td><input name="password" type="password" id="password" /></td></tr></table><input name="Login" type="submit" id="Login" value="Login" /></form></body></html> [/code]This code outputs the form first, then no errors if I make up a username and password, but if I use my username and password I get the exact same thing. The problem is the database isn't being searched. The problem might be in my dbconnect script, so here it goes (I hope Im not boring you to death)[code]<?php$host="localhost"; // Host name.$db_user="mattd_racing"; // MySQL username.$db_password="HIDDEN"; // MySQL password.$database="mattd_racingdb"; // Database name.mysql_connect($host,$db_user,$db_password);mysql_select_db($database);?>[/code]There was no error from this reported. I am hoping you can help me, if you have any suggestions please post them. And thanks trochia, I didn't think of using the manual. Quote Link to comment Share on other sites More sharing options...
trochia Posted January 22, 2007 Share Posted January 22, 2007 First place to look..<lol> Are you sure you are addressing all your tables and fields properly ? <snytax>...and do they exit? Have you been connected to this db before?Here's a D-n-D ( down and dirty ) quick table viewer snip..<? /* D-n-D ( down and dirty ) quick table viewer */$sql = "show tables"; $result = connect($sql); while ($rows = mysql_fetch_array($result)) { echo $rows[0]."<br>"; } function connect($sql) { $username = "uname"; // User Defined $pwd = "pwd"; // User Defined $host = "localhost"; // User Defined possibly if remote $dbname = "dbname"; // User Defined //echo "commencing connection to local db<br>"; if (!($conn=mysql_connect($host, $username, $pwd))) { printf("error connecting to DB"); exit(); } $db2=mysql_select_db($dbname,$conn); $result = mysql_query($sql); if ($result){ return $result; }else{ $error = mysql_error(); echo "Can't complete query because $error"; die(); } } ?> Quote Link to comment Share on other sites More sharing options...
trochia Posted January 22, 2007 Share Posted January 22, 2007 [quote author=trochia link=topic=123423.msg510156#msg510156 date=1169426321]First place to look..<lol> Are you sure you are addressing all your tables and fields properly ? <snytax>...and do they exist? Have you been connected to this db before?Here's a D-n-D ( down and dirty ) quick table viewer snip..<? /* D-n-D ( down and dirty ) quick table viewer */$sql = "show tables"; $result = connect($sql); while ($rows = mysql_fetch_array($result)) { echo $rows[0]."<br>"; } function connect($sql) { $username = "uname"; // User Defined $pwd = "pwd"; // User Defined $host = "localhost"; // User Defined possibly if remote $dbname = "dbname"; // User Defined //echo "commencing connection to local db<br>"; if (!($conn=mysql_connect($host, $username, $pwd))) { printf("error connecting to DB"); exit(); } $db2=mysql_select_db($dbname,$conn); $result = mysql_query($sql); if ($result){ return $result; }else{ $error = mysql_error(); echo "Can't complete query because $error"; die(); } } ?>[/quote] Quote Link to comment Share on other sites More sharing options...
mattd8752 Posted January 22, 2007 Author Share Posted January 22, 2007 That fixed about half of the problem, kinda makes me feel dumb, I forgot to link the user to the database. The D n' D way works now. But registration doesn't want to add them to the database. I don't see why this isn't working. If you can find the error please let me know. Login doesn't let me go as anyone, it does let me login if I create the data in the database. It also can read if the username is taken, So my guess is I am having the problem with:[code] mysql_query("INSERT INTO users (username, password, regkey, email) VALUES($username, $pass, $key, $email) ");[/code] 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.