0

Hello i was here yesterday with this problem, i don't really know how to use this site well as i am new so i reposted. but I'm getting an error with this block of code and i think its the Update query which contains a syntax error.

// Updates if player's record already exists

    $result = mysql_query("UPDATE PlayerStat SET Position='$POS', Number='$NUM', Name='$PlyrName', Status='$Status', TDS='$TDS', INT='$INT', YDS='$YDS', RTG='$RTG', Team='$Team' WHERE Name='$PlyrName'");
    echo mysql_error();
    if (mysql_affected_rows()==0){
        // Populates table if record is non existent
        $result = mysql_query("INSERT INTO PlayerStat(`Position`, `Number`, `Name`, `Status`, `TDS`, `INT`, `YDS`, `RTG`, `Team`) VALUES ('$POS','$NUM','$PlyrName','$Status','$TDS','$INT','$YDS','$RTG','$Team')");
        echo mysql_error();
    }

The Error message

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INT='1', YDS='86', RTG='52.5', Team='ARI' WHERE Name='Bartel, Richard'' at line 1

3 Answers 3

2

INT is a keyword in mysql (declares and integer), if it's your column name you should surround it backticks (`) like so: `INT`.

It's good practice to put these in even though they're not necessary in all cases

UPDATE
    PlayerStat
SET
    `Position` =  '$POS',
    `Number` = '$NUM',
    `Name` = '$PlyrName',
    `Status` = '$Status',
    `TDS` = '$TDS',
    `INT` = '$INT',
    `YDS` = '$YDS',
    `RTG` = '$RTG',
    `Team` = '$Team'
WHERE
    `Name` = '$PlyrName'
Sign up to request clarification or add additional context in comments.

2 Comments

Hey yea that has definitely helped. but it isn't updating the old records in the DB, but just remakes them :(
Your method inserts instead of updates? If that's the case it seems you may have further logic problem or perhaps you don't have the data set up like you think you do in your database. I would start by trying to execute this in your dbs UI (PHPMyAdmin or whatever) and see what you get for results. Better yet, start by converting this to a SELECT statement and executing it there.
1

Two things:

  1. Check the manual for INSERT ... ON DUPLICATE KEY UPDATE which should do this in one statement.

  2. I suggest you take a hard look at mysql_real_escape_string() or similar in PHP to escape your data and prevent SQL Injections.

If you don't know what SQL Injections are, then google it and spend a bit of time reading NOW before it's too late and some cracker or script kitty has your database.

Hope this helps!

Comments

0

You may want to check these websites. http://www.w3schools.com/php/php_mysql_update.asp http://www.tizag.com/mysqlTutorial/mysqlupdate.php

And you might also want to check your spelling mistake or the single quote or double quote. Other than that, check your database namings and data type.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.