cluce Posted May 9, 2007 Share Posted May 9, 2007 I recieve these errors when checking the database for a username that already exists. Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\wamp\www\insert.php on line 20 Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\insert.php:20) in C:\wamp\www\insert.php on line 33 Can someone tell me what I am doing wrong? This is all I have left to my registration page. I would appreciate it. <?php //checks for identical passwords if($_POST["password"]!==$_POST["confirmpassword"]){ header("Location: registration.html"); } else { $mysqli = mysqli_connect("localhost", "root", "", "test"); } if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } // checks if the username is in use if (!get_magic_quotes_gpc()) { $_POST['username'] = addslashes($_POST['username']); } $usercheck = $_POST['username']; $check = ("SELECT username FROM auth_users WHERE username = '$usercheck'"); $check2 = mysql_num_rows($check); //if the name exists it gives an error if ($check2 != 0) { echo ('Sorry, the username '.$_POST['username'].' is already in use.'); } else { $sql = "INSERT INTO auth_users (username, password, ConfirmPassword, f_name, L_name, address, city, state, zip, phoneNumber, AltphoneNumber, email, company) VALUES ('".$_POST['username']."',PASSWORD('".$_POST["password"]."'),PASSWORD('".$_POST["confirmpassword"]."'),'".$_POST['firstname']."','".$_POST['lastname']."','".$_POST['address']."','".$_POST['city']."','".$_POST['state']."','".$_POST['zip']."','".$_POST['phone']."','".$_POST['alternate']."','".$_POST['email']."','".$_POST['company']."')"; $res = mysqli_query($mysqli, $sql); } if ($res === TRUE) { header("Location: registered.html"); } else { printf("Could not insert record: %s\n", mysqli_error($mysqli)); } mysqli_close($mysqli); ?> Link to comment https://forums.phpfreaks.com/topic/50717-solved-checking-the-database-for-a-username-that-already-exists/ Share on other sites More sharing options...
per1os Posted May 9, 2007 Share Posted May 9, 2007 <?php $usercheck = $_POST['username']; $check = mysql_query("SELECT username FROM auth_users WHERE username = '$usercheck'"); // needed mysql_query here $check2 = mysql_num_rows($check); ?> You needed the mysql_query to get a valid resource. Link to comment https://forums.phpfreaks.com/topic/50717-solved-checking-the-database-for-a-username-that-already-exists/#findComment-249333 Share on other sites More sharing options...
jim.davidson Posted May 9, 2007 Share Posted May 9, 2007 Try this if($check2) { echo ('Sorry, the username '.$_POST['username'].' is already in use.'); } instead of this if ($check2 != 0) { echo ('Sorry, the username '.$_POST['username'].' is already in use.'); } Link to comment https://forums.phpfreaks.com/topic/50717-solved-checking-the-database-for-a-username-that-already-exists/#findComment-249337 Share on other sites More sharing options...
cluce Posted May 9, 2007 Author Share Posted May 9, 2007 OK added the mySQL but now I get database. I guess I am not connected to the database but I cant see why? Here are some new errors. Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\wamp\www\insert.php on line 19 Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in C:\wamp\www\insert.php on line 19 Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\wamp\www\insert.php on line 20 Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\insert.php:19) in C:\wamp\www\insert.php on line 32 Link to comment https://forums.phpfreaks.com/topic/50717-solved-checking-the-database-for-a-username-that-already-exists/#findComment-249343 Share on other sites More sharing options...
per1os Posted May 9, 2007 Share Posted May 9, 2007 Sorry about that change all the mysql_query and mysql_num_rows to mysqli_query and mysqli_num_rows you have to be consistent. Link to comment https://forums.phpfreaks.com/topic/50717-solved-checking-the-database-for-a-username-that-already-exists/#findComment-249347 Share on other sites More sharing options...
cluce Posted May 9, 2007 Author Share Posted May 9, 2007 OK I simplyfied my code a bit but I still have the warnings........ Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\wamp\www\insert.php on line 14 Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in C:\wamp\www\insert.php on line 14 Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\wamp\www\insert.php on line 16 Sorry, the username jdoe is already in use.Could not insert record: here is the new code <?php //checks for identical passwords if($_POST["password"]!==$_POST["confirmpassword"]){ header("Location: registration.html"); } else { $mysqli = mysqli_connect("localhost", "root", "", "test"); } if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } // checks if the username exists $result = mysql_query("SELECT username FROM auth_users WHERE username = '$username'"); if(mysql_num_rows($result) !== 0){ echo ('Sorry, the username '.$_POST['username'].' is already in use.'); } else { $sql = "INSERT INTO auth_users (username, password, ConfirmPassword, f_name, L_name, address, city, state, zip, phoneNumber, AltphoneNumber, email, company) VALUES ('".$_POST['username']."',PASSWORD('".$_POST["password"]."'),PASSWORD('".$_POST["confirmpassword"]."'),'".$_POST['firstname']."','".$_POST['lastname']."','".$_POST['address']."','".$_POST['city']."','".$_POST['state']."','".$_POST['zip']."','".$_POST['phone']."','".$_POST['alternate']."','".$_POST['email']."','".$_POST['company']."')"; $res = mysqli_query($mysqli, $sql); } if ($res === TRUE) { header("Location: registered.html"); } else { printf("Could not insert record: %s\n", mysqli_error($mysqli)); } mysqli_close($mysqli); ?> Link to comment https://forums.phpfreaks.com/topic/50717-solved-checking-the-database-for-a-username-that-already-exists/#findComment-249349 Share on other sites More sharing options...
per1os Posted May 9, 2007 Share Posted May 9, 2007 Read my post above. Link to comment https://forums.phpfreaks.com/topic/50717-solved-checking-the-database-for-a-username-that-already-exists/#findComment-249350 Share on other sites More sharing options...
cluce Posted May 9, 2007 Author Share Posted May 9, 2007 OK I changed the mysql to mysqli to be consistent but I still recieve two warnings.. ALthough I see the proper message too Warning: mysqli_query() expects at least 2 parameters, 1 given in C:\wamp\www\insert.php on line 14 Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in C:\wamp\www\insert.php on line 16 Sorry, the username jdoe is already in use.Could not insert record <?php //checks for identical passwords if($_POST["password"]!==$_POST["confirmpassword"]){ header("Location: registration.html"); } else { $mysqli = mysqli_connect("localhost", "root", "", "test"); } if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } // checks if the username exists $result = mysqli_query("SELECT username FROM auth_users WHERE username = '$username'"); if(mysqli_num_rows($result) !== 0){ //$_SESSION['userExists'] echo ('Sorry, the username '.$_POST['username'].' is already in use.'); //header ("Location: registration.php"); } else { $sql = "INSERT INTO auth_users (username, password, ConfirmPassword, f_name, L_name, address, city, state, zip, phoneNumber, AltphoneNumber, email, company) VALUES ('".$_POST['username']."',PASSWORD('".$_POST["password"]."'),PASSWORD('".$_POST["confirmpassword"]."'),'".$_POST['firstname']."','".$_POST['lastname']."','".$_POST['address']."','".$_POST['city']."','".$_POST['state']."','".$_POST['zip']."','".$_POST['phone']."','".$_POST['alternate']."','".$_POST['email']."','".$_POST['company']."')"; $res = mysqli_query($mysqli, $sql); } if ($res === TRUE) { header("Location: registered.html"); } else { printf("Could not insert record: %s\n", mysqli_error($mysqli)); } mysqli_close($mysqli); ?> Link to comment https://forums.phpfreaks.com/topic/50717-solved-checking-the-database-for-a-username-that-already-exists/#findComment-249359 Share on other sites More sharing options...
per1os Posted May 9, 2007 Share Posted May 9, 2007 mysqli_query($mysqli, "SELECT username FROM auth_users WHERE username = '$username'"); You have to remember to follow the function guide lines. www.php.net/mysqli_query It requires a connection string first and statement after. Link to comment https://forums.phpfreaks.com/topic/50717-solved-checking-the-database-for-a-username-that-already-exists/#findComment-249367 Share on other sites More sharing options...
cluce Posted May 9, 2007 Author Share Posted May 9, 2007 Cool. that was it. Thanks. I never had to use a connection string their before. I thought I was connected from my first connection string. php is quite different from asp.net but I like it. Link to comment https://forums.phpfreaks.com/topic/50717-solved-checking-the-database-for-a-username-that-already-exists/#findComment-249373 Share on other sites More sharing options...
per1os Posted May 9, 2007 Share Posted May 9, 2007 mysql functions do not require the connection string mysqli functions require the connection as the first parameter. mysqli is the "new and improved" version of the mysql functions. Either way, php is way better than ASP. =) Link to comment https://forums.phpfreaks.com/topic/50717-solved-checking-the-database-for-a-username-that-already-exists/#findComment-249376 Share on other sites More sharing options...
cluce Posted May 9, 2007 Author Share Posted May 9, 2007 Ahh did not know that. I know you helped me alot already but if you could. Could you tell me why this code is telling me every username exists even the ones that dont exist. Link to comment https://forums.phpfreaks.com/topic/50717-solved-checking-the-database-for-a-username-that-already-exists/#findComment-249377 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.