ostig Posted September 10, 2008 Share Posted September 10, 2008 I am attempting to write a script to add or delete users from a database. I am very new to this so apologies if I missed something very basic, but I have been looking/recoding for hours... Here is the code at present of the part that is at issue. ---------------------- if ($switch == "add") { $query="INSERT INTO subscribers (userid, username, userpass)VALUES ('NULL','".$user1."', '".$pass1."')"; mysql_query($query) or die ('Error updating database'); echo "Database Updated With: " .$user1. " " .$pass1 ; } elseif ($switch == "del") { $query="SELECT FROM subscribers WHERE (username=".$user1." AND userpass=".$pass1.")"; mysql_query($query); if (mysql_num_rows($query) == FALSE) { echo "User / Password combination does not exist"; } else { $query="DELETE FROM subscribers WHERE (username='".$user1."' AND userpass='".$pass1."')"; mysql_query($query) or die ('Error deleting from database'); echo "User " .$user1. "deleted"; } } else { echo "You did not specify to add or delete..."; } --------------------------------------------- The part about adding works fine, if I put in a new userid and password and select add, the data gets added to the database. However, if I put in a userid and password and select "del", I get errors when I am trying to use the mysql_num_rows function. What I am trying to do is: IF the username / password combo does not exist, just print out the message. IF the userid / password combo does exist, delete the userid from the table. On both of the above conditions, I get: Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/cheshire/public_html/test/update.php on line 36 ( LINE 36 is (mysql_num_rows($query) != FALSE) { ) If the userid / password combo does not exist, I also get the appropriate message. If the userid / password combo does exist, it is successfully deleted. No matter how I change line 36 I still get this error. I would be more than willing to not use mysql_num_rows if there is another way to get this done, I was earlier advised to use this function to accomplish what I need to do. Any help/advice appreciated. Kevin Link to comment https://forums.phpfreaks.com/topic/123623-having-trouble-with-mysql_num_rows/ Share on other sites More sharing options...
lanmonkey Posted September 10, 2008 Share Posted September 10, 2008 do this: if ($switch == "add") { $query="INSERT INTO subscribers (userid, username, userpass)VALUES ('NULL','".$user1."', '".$pass1."')"; mysql_query($query) or die ('Error updating database'); echo "Database Updated With: " .$user1. " " .$pass1 ; } elseif ($switch == "del") { $query="SELECT FROM subscribers WHERE (username=".$user1." AND userpass=".$pass1.")"; $result = mysql_query($query); if (0 == mysql_num_rows($result)) { echo "User / Password combination does not exist"; } else { $query="DELETE FROM subscribers WHERE (username='".$user1."' AND userpass='".$pass1."')"; mysql_query($query) or die ('Error deleting from database'); echo "User " .$user1. "deleted"; } } else { echo "You did not specify to add or delete..."; } Link to comment https://forums.phpfreaks.com/topic/123623-having-trouble-with-mysql_num_rows/#findComment-638394 Share on other sites More sharing options...
ostig Posted September 10, 2008 Author Share Posted September 10, 2008 Edited code. Result with mismatched userid/password: version = 1.01 Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/cheshire/public_html/test/update.php on line 38 User / Password combination does not exist --------------------- echo "version = 1.01"; if ($switch == "add") { $query="INSERT INTO subscribers (userid, username, userpass)VALUES ('NULL','".$user1."', '".$pass1."')"; mysql_query($query) or die ('Error updating database'); echo "Database Updated With: " .$user1. " " .$pass1 ; } elseif ($switch == "del") { $query="SELECT FROM subscribers WHERE (username=".$user1." AND userpass=".$pass1.")"; $result = mysql_query($query); if (0 == mysql_num_rows($result)) { echo "User / Password combination does not exist"; } else { $query="DELETE FROM subscribers WHERE (username='".$user1."' AND userpass='".$pass1."')"; mysql_query($query) or die ('Error deleting from database'); echo "User " .$user1. "deleted"; } } else { echo "You did not specify to add or delete..."; } Link to comment https://forums.phpfreaks.com/topic/123623-having-trouble-with-mysql_num_rows/#findComment-638401 Share on other sites More sharing options...
lanmonkey Posted September 10, 2008 Share Posted September 10, 2008 you might also have a problem with your sql query: try putting single quotes around the variables: $query="SELECT FROM subscribers WHERE (username='".$user1."' AND userpass='".$pass1."')"; more info: http://www.w3schools.com/php/php_mysql_where.asp Link to comment https://forums.phpfreaks.com/topic/123623-having-trouble-with-mysql_num_rows/#findComment-638421 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.