gpt4 book ai didi

php - 使用 PHP 更新 sql 表中的字段

转载 作者:行者123 更新时间:2023-11-29 09:49:24 25 4
gpt4 key购买 nike

我的数据库中有一个表,其中包含字段 id、state、number 和 date。

我需要的是更新与作为参数出现的 id 相匹配的记录状态。

我正在执行以下操作,但没有结果:

static public function mdlUpdateField($table, $id){

$stmt = Conection::conect()->prepare("UPDATE $table SET state = :state WHERE id = :id");

$stmt->bindParam(":state", "OK", PDO::PARAM_STR);

if($stmt -> execute()){

return "ok";

}else{

return "error";

}

$stmt -> close();

$stmt = null;

}

当我这样做时,我收到以下错误:

Fatal error: Uncaught Error: Cannot pass parameter 2 by reference

最佳答案

$stmt->bindParam(":state", "OK", PDO::PARAM_STR);
$stmt->bindParam(":id", $id, PDO::PARAM_INT);

为了传递故事名称,您需要动态 SQL

("UPDATE " . $table . " SET state = :state WHERE id = :id");

这样你就有被sqlinjection的风险..

关于php - 使用 PHP 更新 sql 表中的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55081024/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com