Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/201355-phpmysql-insert-and-update-problem/
Share on other sites

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

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']`);

 

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?

 

 

 

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.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.