簡體   English   中英

php mySQL語法錯誤

[英]php mySQL error of syntax

我不是PHP的新手,但是我遇到了一個[看似]簡單的問題,無法解決。

MySQL拋出語法錯誤的錯誤。

我的聲明是這樣的:

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

然后$words_amount是一個整數, $sn_id也是一個整數。 他們被仔細檢查。

在執行之前打印該語句如下:

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

但是,單詞value( '250' )也使用整數數據類型進行了測試,但是沒有發生變化並且錯誤持續存在。

引發的錯誤是:

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

如果我理解您的問題(並且preuploads表格),那么

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

應該

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

或者,甚至更好地准備和使用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);

我發現了兩個錯誤:

首先,不應該封裝數據,因此:

$words_count應該保留原樣,不要用'

並且表和字段名稱應使用反引號封裝

我認為您的表名有問題。 更新查詢的語法是

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM