2017-08-10 29 views
0
Subject Update Failed!!You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1 
I am stuck here can anyone help me what I am missing in this code.The error is in Update Query. 

一切正常,當我寫的代碼(我用的是Dreamviwer代碼編輯器軟件,我沒有得到任何語法錯誤。然而,當我運行它,我得到這個錯誤: //處理形式主題更新失敗Mysql的

$id= $current_subject["Id"]; 
$name=mysql_prep($_POST["Name"]); 
$position=(int)$_POST["Position"]; 
$visible=(int)$_POST["Visible"]; 

$query="UPDATE subjects SET Name='{$name}',Position=$position,Visible=$visible WHERE Id={$id}"; 

$result= mysqli_query($conn, $query); 
if($result && mysqli_affected_rows($conn)==1){ 
    //success 
    $_SESSION["message"]="Subject updated."; 
    redirect_to("manage_content.php"); 

}else{ 

    //Failure 
    $message="Subject Update Failed" . $conn->error; 

    } 
+0

什麼是mysql_prep?我認爲你需要刪除UPDATE查詢 –

+0

'echo $ query'中的{}。當你發現語法錯誤時,收起你的尾巴。 –

回答

1

最有可能輸入錯誤的參數名稱Еcho您的參數第一

並使用準備好的語句,防止SQL注入:。

$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass); 
$query="UPDATE subjects SET Name = ? ,Position = ?,Visible = ? WHERE Id = ?"; 
$stmt = $dbh->prepare($query); 
$stmt->bindParam(1, $name); 
$stmt->bindParam(2, $position); 
$stmt->bindParam(3, $visible); 
$stmt->bindParam(4, $id); 
$stmt->execute(); 
$stmt->fetchAll(); 

延伸閱讀:PDO