PHP MYSQL使用数组中的值更新多行

时间:2021-05-08 13:20:38

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:

现在当我运行上面的脚本并检查10,8和4后,然后按“hideBtn”按钮,我得到:

Array ( [0] => 10 [1] => 8 [1] => 4 )

数组([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:

这是我被卡住的地方。我正在尝试获取此数组的值并在MSQL查询中使用它们来查找我要隐藏的post_id:

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 )

数组([0] => 10 [1] => 8 [1] => 4)

and there's no change in the database.

并且数据库没有变化。

Here's the database table, hide_post defaults to 0:

这是数据库表,hide_post默认为0:

post
post_id user_id title body hide_post

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.

从我读到的关于我得到的错误:命令不同步;你现在不能运行这个命令我想我需要使用mutli_query或$ stmt-> store_result()但这是我第一次使用php和mysql而且我不知道如何去做。

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.

好吧,我找到了另一种解决方法,我所做的就是将隐藏帖子的查询移动到while()下面,该while()运行实际获取帖子信息。我希望我早点意识到这一点。

2 个解决方案

#1


0  

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.

在phpmyadmin中运行相同的查询并检查相同的查询是否返回ant错误。

#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.

我只需要做的就是避免错误:命令不同步;你不能运行这个命令现在是我的第二个查询隐藏/删除用户选择的帖子,并将查询放在while()循环之后,它将获取()每个帖子。

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

基本上我试图在第一个查询完成之前进行第二次查询。

#1


0  

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.

在phpmyadmin中运行相同的查询并检查相同的查询是否返回ant错误。

#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.

我只需要做的就是避免错误:命令不同步;你不能运行这个命令现在是我的第二个查询隐藏/删除用户选择的帖子,并将查询放在while()循环之后,它将获取()每个帖子。

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

基本上我试图在第一个查询完成之前进行第二次查询。