Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php pdo codeigniter未执行更新查询_Php_Codeigniter - Fatal编程技术网

Php pdo codeigniter未执行更新查询

Php pdo codeigniter未执行更新查询,php,codeigniter,Php,Codeigniter,我正在尝试使用此查询更新表,但似乎不起作用 $query="UPDATE product SET qty = (qty - '$qty') WHERE barcode = '$barcode'"; $result = $this->db->conn_id->prepare($query); $result->execute(); 我尝试将查询放置在try-catch块中,但它不会抛出任何错误。问题在于CodeIgniter中的实现,因为此查询在Cod

我正在尝试使用此查询更新表,但似乎不起作用

  $query="UPDATE product SET qty = (qty - '$qty') WHERE barcode = '$barcode'";  
  $result = $this->db->conn_id->prepare($query);
  $result->execute();  

我尝试将查询放置在try-catch块中,但它不会抛出任何错误。问题在于CodeIgniter中的实现,因为此查询在CodeIgniter外部执行时正在工作

看看你的代码,有几件事

您没有正确使用准备好的语句。使用prepared语句的好处是使用不同的函数传递所需的变量,以便正确地转义它们。考虑以下事项:

$query="UPDATE product SET qty = (qty - ':qty') WHERE barcode = ':barcode'";  

$stmt = $this->db->conn_id->prepare($query);

$stmt->bindParam(':qty', $qty, PDO::PARAM_STR);
$stmt->bindParam(':barcode', $barcode, PDO::PARAM_STR);

$stmt->execute();  

echo "Rows affected: " . $stmt->rowCount();

在这里,我们进行查询并在中设置参数。然后我们将变量绑定到语句,以便正确转义它们。之后,我们可以执行该语句,然后使用
fetch()
函数获取响应。传入的枚举将以关联数组的形式返回结果

如果受影响的行为零,我想查询是正确的,请查看数据库中是否存在条形码变量的值

引用的是什么?我没有在中的示例中看到这一点。此答案的部分引用了此感谢更正,但它仍然没有更新我的表格。更新了我的答案。查询的行数是多少?受影响的行数:0,我尝试为数量设置静态值,但仍然无效。当您执行
var\u dump($this->db->conn\u id)
时会发生什么。您确定要更新的数据库中存在该记录吗?