我目前正在用一些不错的材料设计构建一个轻量级的博客平台,但我遇到了一个问题。我有一个表与ID的,我想改变我的表中的公共的值,这样你就可以隐藏文章从博客,这样我做了一个循环,但它只适用于第一个ID和其他ID的。下面是我的代码:
try {
if (isset($_POST['submit'])) {
$stmt = $db->query('SELECT postID FROM blog_posts ORDER BY postID DESC');
while ($row = $stmt->fetch()) {
// set public based on the submitted value from your form
$public = empty($_POST['public'][$row['postID']]) ? 0 : 1;
// do the update
$stmt = $db->prepare('UPDATE blog_posts SET public = ? WHERE postID = ?');
$stmt->execute(array($public, $row['postID']));
header('Location: index.php');
}
}
} catch (PDOException $e) {
echo $e->getMessage();
}
如果有任何用处,这里是我的PHP完整页面,我的完整页面可以在这里找到
请将header('Location: index.php');
放在while循环之外,不要覆盖$stmt
,而是使用另一个:
//.......
//.......
while ($row = $stmt->fetch()) {
// set public based on the submitted value from your form
$public = empty($_POST['public'][$row['postID']]) ? 0 : 1;
// do the update
//Create another statement
$stmt2 = $db->prepare('UPDATE blog_posts SET public = ? WHERE postID = ?');
$stmt2->execute(array(
$public,
$row['postID']
));
}
header('Location: index.php');
//.......
//.......