简体   繁体   English

MySQLi 在 PHP 中准备更新语句

[英]MySQLi prepared update statement in PHP

How do you write a prepared update statement?您如何编写准备好的更新语句? Reference: mysqli::prepare参考: mysqli::prepare

I've tried writing it as described:我已经尝试按照描述编写它:

  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);
    }

Error:错误:

Prep statment failed: You have an error in your SQL syntax; Prep 语句失败:您的 SQL 语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'description = ?检查与您的 MySQL 服务器版本相对应的手册,以在 'description = ? WHERE uid = ?'哪里 uid = ? at line 1 Edited row.在第 1 行编辑的行。

You're just missing a comma between the set columns:您只是在设置的列之间缺少一个逗号:

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

When MySQL reports an error the likes of check the manual for syntax to use near 'something , look most often to the character immediately preceding the 'something , as that is where your error occurs.当 MySQL 报告错误时,例如检查手册以了解在 'something 附近使用的语法,最常查看紧接在'something之前的字符,因为那是您的错误发生的地方。

$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 = ?"

You are binding the parameters before assigning them to variables:您在将参数分配给变量之前绑定参数:

$title=$_POST['title'];
$desc=$_POST['description'];
$uid2=$_GET['uid']; 

$stmt->bind_param('sss', $title, $desc, $uid2);

edit : scratch that, it doesn't appear to make a difference whether or not the parameters are bound before or after you have defined the variables (you learn something new everyday!), but like Michael said, logically it makes sense to define them first.编辑:从头开始,在定义变量之前或之后是否绑定参数似乎没有区别(您每天都会学到新东西!),但就像迈克尔说的那样,从逻辑上讲,定义它们是有意义的第一的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM