Amal Posted May 11, 2010 Share Posted May 11, 2010 Guys i am trying to store a score on the table , for the first time users score should be inserted however if the same user plays again their score must not be inserted again but the score should be updated where the new score is added to the old score. this is the code i am using and it only inserted and not doing the else statement. i am also getting a error message code <?php $userid = $_SESSION['userID']; //did you use session_start() yet? $CheckSQL = mysql_query("Select * FROM score WHERE userID ='$userid'"); if (mysql_num_rows($CheckSQL) == 0){ // use mysql_num_rows() to see if there are rows //store the the score and member id retrieved using session into the quiz table $sqlinsert = "INSERT INTO Score (Scores, userID) VALUES(" .$scores. "," . $_SESSION['userID'] . " )"; $sql = mysql_query($sqlinsert) or die (mysql_error()); echo "Score Inserted "; }else{ $update="UPDATE Score SET Scores='$scores' WHERE userID='$userid'"; $SQL=mysql_query($update) or die(mysql_error()); echo "Score Updated "; } ?> error message Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in Can someone help me to get it solve please thank you all Quote Link to comment https://forums.phpfreaks.com/topic/201355-phpmysql-insert-and-update-problem/ Share on other sites More sharing options...
PFMaBiSmAd Posted May 11, 2010 Share Posted May 11, 2010 The error means that your query failed to execute and returned a FALSE value. If you echo mysql_error() on the next line after the mysql_query() statement, it will tell you what error occurred. I'm going to guess that your table is named Score and not score and that you are doing this on an operating system that is case-sensitive. Is there some reason you are not using an INSERT ... ON DUPLICATE KEY UPDATE ... query - http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html Quote Link to comment https://forums.phpfreaks.com/topic/201355-phpmysql-insert-and-update-problem/#findComment-1056404 Share on other sites More sharing options...
sharp.mac Posted May 11, 2010 Share Posted May 11, 2010 check your database structure. It sounds to me that your colum count or structure is off. INSERT INTO Score (Scores, userID) VALUES(" .$scores. "," . $_SESSION['userID'] . " ) Does your table "Score" have only 2 columns? If so something more streamline would work. INSERT INTO `Score` VALUES (`$scores`,`$_SESSION['userID']`); Quote Link to comment https://forums.phpfreaks.com/topic/201355-phpmysql-insert-and-update-problem/#findComment-1056405 Share on other sites More sharing options...
Amal Posted May 11, 2010 Author Share Posted May 11, 2010 I have three fields in this table the id, score and userid too Quote Link to comment https://forums.phpfreaks.com/topic/201355-phpmysql-insert-and-update-problem/#findComment-1056415 Share on other sites More sharing options...
bulrush Posted May 11, 2010 Share Posted May 11, 2010 sharp.mac is correct. Watch those quotes when constructing your SQL statement. I'm a beginning PHP programmer and the quotes fooled me a lot early on too. Your statement should be: $update="UPDATE Score SET Scores='".$scores."' WHERE userID='".$userid."'"; A PHP highlighting text editor will help you a lot. See how the forum highlights code inside and outside of quotes? See how the forum changes the font inside of the php tag, so different quotes are recognizable? Quote Link to comment https://forums.phpfreaks.com/topic/201355-phpmysql-insert-and-update-problem/#findComment-1056428 Share on other sites More sharing options...
Amal Posted May 11, 2010 Author Share Posted May 11, 2010 The problem now is not the update statement but it is the myqsl error and i don't now how to solve it Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in Quote Link to comment https://forums.phpfreaks.com/topic/201355-phpmysql-insert-and-update-problem/#findComment-1056442 Share on other sites More sharing options...
PFMaBiSmAd Posted May 11, 2010 Share Posted May 11, 2010 You were already told what is causing the error and how to troubleshoot it - The error means that your query failed to execute and returned a FALSE value. If you echo mysql_error() on the next line after the mysql_query() statement, it will tell you what error occurred. Quote Link to comment https://forums.phpfreaks.com/topic/201355-phpmysql-insert-and-update-problem/#findComment-1056465 Share on other sites More sharing options...
Amal Posted May 11, 2010 Author Share Posted May 11, 2010 That wasn't the problem and i got it solved anyways thank u all for the help Quote Link to comment https://forums.phpfreaks.com/topic/201355-phpmysql-insert-and-update-problem/#findComment-1056518 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.