0

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.

5
  • 2
    Could you print $hide_post to see if the implode is done correctly? Commented Mar 20, 2013 at 0:57
  • Ensure your error reporting is on for SQL queries (using mysql_error() or mysqli_error($link), a table name/the order might be incorrect. Commented Mar 20, 2013 at 1:06
  • the implode looks find to me, i checked post 9 and 7 and it gave me: UPDATE post SET hide_post=1 WHERE post_id IN (9,7) Commented Mar 20, 2013 at 1:53
  • @Titanium ok so when I run $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 Commented Mar 20, 2013 at 2:21
  • 1
    @Brian - pleased you fixed it. If you solve it yourself, it's better to create your own answer and tick it - so it removes the question from the unanswered lists, and so the data API separates 'question' from 'answer' correctly. If you get a mo, you can use Rollback to do that on this question - thanks. Commented Mar 20, 2013 at 8:34

2 Answers 2

0

It turns out that my problem was not caused by the query itself, but where I had placed it.

All I had to do to avoid the error: Commands out of sync; you can't run this command now was to take my second query that hides/deletes posts, that are selected by the user, and put the query after the while() loop that would fetch() each post.

Basically I was trying to make a second query before the first query was finished.

-2

Using this code

$checked = array ( 0 => 10, 1 => 8, 2 => 4 );
//print_r($checked);
$hide_post = 'UPDATE post SET hide_post=1 
WHERE post_id IN (' . implode(',', array_map('intval', $checked)) . ')';

echo $hide_post; 

your query becomes

UPDATE post SET hide_post=1 WHERE post_id IN (10,8,4) 

Run the same query in phpmyadmin and check whether the same query returns ant error or not.

3
  • I ran UPDATE post SET hide_post=1 WHERE post_id IN (10,8,4) in phpmyadmin and there were no errors Commented Mar 20, 2013 at 1:48
  • @BrianBaker Check your database connection and also check you included necessary database file in your php file Commented Mar 20, 2013 at 2:00
  • The db is included at the start of the file as $db. The posts are pulled from the database using the same connection, so I'd imagine updating the database would work as well. Commented Mar 20, 2013 at 2:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.