PHP MYSQL updating multiple rows using values from an array

本秂侑毒 提交于 2020-07-10 11:48:01

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/15513353/php-mysql-updating-multiple-rows-using-values-from-an-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!