简体   繁体   中英

php mySQL error of syntax

I'm not a newbie to PHP but I have encountered a [seemingly] simple problem which I cannot figure out how to resolve.

MySQL throws error that the syntax is wrong.

My Statement is this:

if($value){
        $query = "UPDATE ".$preuploads." SET words = '$words_amount' WHERE id= $sn_id";
        $db->sql_query( $query ) or die( mysql_error() );
    }

And then $words_amount is an integer, $sn_id is also an integer. They are double checked.

The statement when printed before execution is as follows:

UPDATE  SET uploads words = '250' WHERE id= 8081
// edited, with the name of table added since the problem primarily was 
// with the encapsulation and the name of table just was dropped in this question
// and not in the app

however words value ( '250' ) is tested with integer data-type as well, but no change occurs and the error lingers on.

And the error thrown is:

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 'SET words = '250' WHERE id= 8081' at line 1

If I understand your question (and preuploads is a table), then

$query = "UPDATE ".$preuploads." SET words = '$words_amount' WHERE id= $sn_id";

should be

$query = "UPDATE ".$preuploads." SET words = '".$words_amount."' WHERE id=".$sn_id;

Or, even better prepare and use bind_param ,

$stmt = $mysqli->prepare("UPDATE ? SET words=? WHERE id=?");
$stmt->bind_param($preuploads, $words_amount, $snd_id);
$stmt->execute();

检查您的字符串($words_amount)是否有单引号'如果使用php $words_amount=string_replace("'","/'",$your_string_variable);将其删除,则将其删除$words_amount=string_replace("'","/'",$your_string_variable);

I have found two errors:

First, not encapsulation of the data should occur, thus:

$words_count should be left as is, not to be encapsulated with '

And the table and fields name should be encapsulated with backtick

I think your having problem with name of table. The syntax for update query is

UPDATE table_name SET words = '250' WHERE id= 8081

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