【发布时间】:2013-12-11 22:52:02
【问题描述】:
我从forum 中选择了一个小 sn-p,它允许我使用 PDO 更新多行。该示例仅允许单列,但我希望它可以启用按列更新。
我稍微修改了sn-p,问题是,row(url)中的单个更改将更改row(url)中的所有条目
如果有人有敏锐的眼光,看看问题出在哪里:
if (isset($_POST['submit'])) {
$stmt = $db->prepare("UPDATE `$tbl_name` SET `url`=:url, `country`=:country WHERE id=:id");
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->bindParam(':url', $url, PDO::PARAM_STR);
$stmt->bindParam(':country', $country, PDO::PARAM_STR);
foreach ($_POST['url'] as $id => $url) {
$stmt->execute();
}
foreach ($_POST['country'] as $id => $country) {
$stmt->execute();
}
echo '<h1>Updated the records.</h1>';
}
// Print the form.
echo '<form action="' . htmlspecialchars($_SERVER['PHP_SELF']) . '" method="post">';
foreach ($db->query("SELECT * FROM `$tbl_name` ORDER BY `id`") as $row) {
echo '<input type="text" name="url[' . (int)$row['id'] . ']" value="'
. htmlspecialchars($row['url']) . '" /><input type="text" name="country[' . (int)$row['id'] . ']" value="'
. htmlspecialchars($row['country']) . '" /><br />';
}
echo '<input type="submit" name="submit" value="Update" /></form>';
【问题讨论】: