【发布时间】:2015-01-28 19:06:26
【问题描述】:
环顾四周,这让我发疯,尝试使用 PDO 对 PHP 进行基本更新,使用可变大小的数组,这是我的代码:
function Database_Update($table,$set,$where) {
$con = DB_PDO_Connect();
//Create bind array that picks up values as they have places made for them
$bind = array();
//Write SET part of statement, with ? as variable places
$prep = "UPDATE $table SET ";
foreach ($set as $key => $value){
$prep .= $key."=?, ";
$bind[] = $value;
}
$prep = rtrim($prep, " ,") . " ";
//Write WHERE part of statment, with ? as variable places
$prep .= "WHERE ";
foreach ($where as $key => $value){
$prep .= $key . "=?, ";
$bind[] = $value;
}
$prep = rtrim($prep, " ,");
var_dump($prep);
echo('<br>');
var_dump($bind);
echo('<br>');
var_dump($table);
try {
$stmt = $con->prepare($prep);
$stmt->execute($bind);
echo $affected_rows = $stmt->rowCount();
//$a_data = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch(PDOException $e) {
trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $e->getMessage(), E_USER_ERROR);
}
$con = null;
}
在代码中,$prep 的输出如下所示:
string(138) "UPDATE Test SET Group=?, PartName=?, PartNum=?, NumInstock=?, Shelf=?, NumUsed=?, Distributor=?, DistributorPartNum=?, Cost=? WHERE DBid=?"
$bind 变量如下所示:
array(10) { [0]=> string(0) "" [1]=> string(24) "Bearing C Spherical" [2]=> string(5) "Hello" [3]=> string(1) "5" [4]=> string(27) "Black Bearing Box 2 shelf 3" [5]=> string(1) "0" [6]=> string(3) "FKS" [7]=> string(6) "GE 8 C" [8]=> string(0) "" [9]=> int(6) }
除了 BDid 列是 int 之外,所有列都是 TEXT 格式。我已经使用未准备好的语句使代码顺利运行,但我想我会更新它,使用相同的数据和相同的表。
不返回错误,但不影响行。
【问题讨论】:
-
您是否尝试过使用
ini_set("display_errors", "on")和error_reporting(E_ALL)来尝试去除其中的一些错误? -
好的,我已经把这些放在了,
error_reporting(E_ALL)在脚本的开头,然后echo ini_get('display_errors');在它通过之后仍然没有得到任何东西......那是如何实现你的说? @Vanitas -
尝试在数据库中运行转储的 SQL 字符串,将所有
?占位符替换为一个值,然后查看是否运行。 -
这行得通:
UPDATE Test SETGroup`='hi', ``PartName``='hi' WHERE ``DBid``=2,我将尝试将 ` 放入查询中并查看结果,但我想我已经尝试过了