MySQLi 在 PHP 中准备了更新语句


MySQLi prepared update statement in PHP

你如何编写准备好的更新语句?参考:mysqli::p repare

我试过按照描述来写它:

  if ($stmt = $mysqli->prepare("UPDATE tblFacilityHrs SET title =? description = ? WHERE uid = ?")){
            $stmt->bind_param('sss', $title, $desc, $uid2);
            //Get params
            $title=$_POST['title'];
            $desc=$_POST['description'];
            $uid2=$_GET['uid'];     
$stmt->execute();
            $stmt->close();
    }
    else {
        //Error
        printf("Prep statment failed: %s'n", $mysqli->error);
    }

错误:

准备语句失败:SQL 语法有错误;请检查 与您的 MySQL 服务器版本相对应的正确手册 在"描述 = ?其中 uid = ?' 在第 1 行 已编辑 排。

你只是在设置的列之间缺少一个逗号:

UPDATE tblFacilityHrs SET title = ?, description = ? WHERE uid = ?
                                ^^^^^^

当MySQL报告错误时,例如检查手册中的语法以在"某物"附近使用,最常查看"某物">前面的字符,因为那是错误发生的地方。

$sql = "UPDATE tblFacilityHrs SET title = ?, description = ? WHERE uid = ?";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param('sss', $title, $desc, $uid2);
$stmt->execute();

您可能需要添加逗号:

$stmt = $mysqli->prepare("UPDATE tblFacilityHrs SET title = ?, description = ? WHERE uid = ?"
在将

参数分配给变量之前,请先绑定参数:

$title=$_POST['title'];
$desc=$_POST['description'];
$uid2=$_GET['uid']; 
$stmt->bind_param('sss', $title, $desc, $uid2);

编辑:划伤,参数是否在定义变量之前或之后绑定似乎没有区别(你每天都会学到新东西!(,但就像迈克尔说的那样,从逻辑上讲,首先定义它们是有意义的。