主题更新失败的Mysql

时间:2017-08-10 10:34:30

标签: php mysql mariadb

    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代码编辑器软件。但是,当我运行它时,我收到此错误: //处理表单

    $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;

    }

1 个答案:

答案 0 :(得分: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