I've working on this problem for a while, and I've been stuck for a few days. What I have is a basic blog system, and right now I'm stuck on trying to delete/hide posts from a list.
if(isset($_POST['hideBtn']) && isset($_POST['hidePost'])){
$checked = $_POST['hidePost'];
print_r($checked);
} elseif(isset($_POST['hideBtn']) && !isset($_POST['hidePost'])){
echo "Nothing Selected";
}
There is a checkbox for each post that shows up in the list.
<input type="checkbox" name="hidePost[]" value="<?php echo $post_id;?>">
Now when I run the above script and check off post 10, 8 and 4 and press the "hideBtn" button I get:
Array ( [0] => 10 [1] => 8 [1] => 4 )
This is where I get stuck. I'm trying to take the values of this array and use them in a MSQL query to find the post_id's that I want to hide:
if(isset($_POST['hideBtn']) && isset($_POST['hidePost'])){
$checked = $_POST['hidePost'];
print_r($checked);
$hide_post = 'UPDATE post SET hide_post=1
WHERE post_id IN (' . implode(',', array_map('intval', $checked)) . ')';
$db->query($hide_post);
} elseif(isset($_POST['hideBtn']) && !isset($_POST['hidePost'])){
echo "Nothing Selected";
}
This gives me the same result as before:
Array ( [0] => 10 [1] => 8 [1] => 4 )
and there's no change in the database.
Here's the database table, hide_post defaults to 0:
post
post_id user_id title body hide_post
EDIT
Alright it seems my issue was due to too many database queries on one connection.
This is what I have included at the top of my page:
<?php
//get record count
$record_count = $db->query("SELECT * FROM post");
//number of posts per page
$per_page = 4;
//number of pages
$pages = ceil($record_count->num_rows/$per_page);
//get page number
if(isset($_GET['p']) && is_numeric($_GET['p'])){
$page = $_GET['p'];
}else{
$page = 1;
}
if($page<=0){
$start = 0;
}else{
$start = $page * $per_page - $per_page;
}
$prev = $page - 1;
$next = $page + 1;
//get post information from database
$query = $db->prepare("SELECT post_id, title, LEFT(body, 100) AS body, posted, category, user_name FROM post
INNER JOIN categories ON categories.category_id=post.category_id
INNER JOIN user ON user.user_id=post.user_id
WHERE hide_post = 0
order by post_id desc limit $start, $per_page");
$query->execute();
$query->bind_result($post_id, $title, $body, $posted, $category, $user_name);
?>
From what I've read about the error I was getting: Commands out of sync; you can't run this command now
I think I need to use mutli_query
or $stmt->store_result()
but this is my first time using php and mysql and I have no idea how to go about it.
EDIT 2
Alright, I found another way to fix it, all I did was move the query that hides the post to below the while()
that runs the actual fetching of post information. I wish I realized this sooner heh.
$hide_post
to see if the implode is done correctly? – fedorqui Mar 20 '13 at 0:57mysql_error()
ormysqli_error($link)
, a table name/the order might be incorrect. – Titanium Mar 20 '13 at 1:06UPDATE post SET hide_post=1 WHERE post_id IN (9,7)
– Brian Baker Mar 20 '13 at 1:53$db->query($hide_post) or die('Could not connect: ' . mysqli_error($db));
i get this on a blank page:Could not connect: Commands out of sync; you can't run this command now
– Brian Baker Mar 20 '13 at 2:21