简体   繁体   中英

Can't get score to update with this mysql statement

I'm guessing that I'm just a little rusty or something because it seems like this should be working. Am I missing something here...

Here is the code I am trying to use...

<?php 
echo dbConn();
$existing_time = mysql_result(mysql_query("SELECT p_time FROM scores WHERE p_uid=$uid"), 0);
$existing_category = mysql_result(mysql_query("SELECT p_cat FROM scores WHERE p_uid=$uid AND p_cat=$pieces"), 0);

if ($existing_category == "") {
mysql_query(
          "INSERT INTO scores VALUES (
          '',
          '$uid',
          '$pusername', 
          '$time',
          '$pieces'
          )");
} elseif ($existing_time <= $time) {
    echo "No Change!  Old Score Was Better (Lower)";
} elseif ($existing_time > $time) {
    mysql_query("UPDATE scores SET p_time = " . $time . " WHERE p_uid = " . $uid . " AND p_cat = " . $pieces . "");
};
?>

Now... Here is what I am trying to do...

I am collecting info from the database where the users username AND category match. If the category for that user does not exist, it inserts the latest score. (This much works.)

Then, if the category does exist but the old score is better, it just does nothing. (This part works too)...

However, what I can't seem to do is get it to update the last score, if the current score is better (lower score, since this is a time based game.) It doesn't update the score.

I am trying it this way: By updating a row in "scores" where the USERNAME and the CATEGORY match at the same time.

Please note... where it says "pieces". this is a category. Where it says "time", this is a score. The score is returned as 00:00:00 for hours minutes and seconds.

EXAMPLE: (in parentheses is the database row name)

id (ID) = just KEY id in sequencial order

user id (p_uid) = 123456789

username (p_username) = somename

score (p_time) = 00:01:03

category (p_cat) = 10

Change you update statement to:

mysql_query("UPDATE scores SET p_time = '" . $time . "' WHERE p_uid = " . $uid . " AND p_cat = " . $pieces . "");

You have missed quotes in the update statement around $time .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM