PHP ID 循环仅适用于第一个 ID
PHP While loop of ID's only works with first id
我目前正在构建一个具有一些不错 Material 设计的轻量级博客平台,但我遇到了 运行 问题。我有一个带 ID 的 table,我想在我的 table 中更改 public 的值,以便您可以隐藏博客中的文章,为此我做了一个循环,但它只是适用于其他 ID 的第一个 ID 和 none。这是我的代码:
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();
}
如果有用的话here is my full page of PHP, also my fully loaded page can be found here
提前致谢。
请将此 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');
//.......
//.......
我目前正在构建一个具有一些不错 Material 设计的轻量级博客平台,但我遇到了 运行 问题。我有一个带 ID 的 table,我想在我的 table 中更改 public 的值,以便您可以隐藏博客中的文章,为此我做了一个循环,但它只是适用于其他 ID 的第一个 ID 和 none。这是我的代码:
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();
}
如果有用的话here is my full page of PHP, also my fully loaded page can be found here
提前致谢。
请将此 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');
//.......
//.......