I am currently building a lightweight blogging platform with some nice Material Design but i have run into a problem. I have a table with ID's and I want to change the value of public in my table so that you can hide articles from the blog, to do so I made a loop but it only works on the first id and none of the other id's. Here is my code:
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();
}
If it is of any use here is my full page of PHP, also my fully loaded page can be found here
Thanks in advance.
Please put this header('Location: index.php');
outside the while-loop and don't override the $stmt
instead use another one :
//.......
//.......
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');
//.......
//.......