PHP MYSQL使用数组中的值更新多行 [英] PHP MYSQL updating multiple rows using values from an array
问题描述
我已经在这个问题上解决了一段时间,并且已经停留了几天.我拥有的是基本的博客系统,现在我只能尝试从列表中删除/隐藏帖子.
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;?>">
现在,当我运行上述脚本并检查帖子10、8和4并按"hideBtn"按钮时,我得到:
Now when I run the above script and check off post 10, 8 and 4 and press the "hideBtn" button I get:
数组([0] => 10 [1] => 8 [1] => 4)
Array ( [0] => 10 [1] => 8 [1] => 4 )
这就是我被困住的地方.我正在尝试采用此数组的值,并在MSQL查询中使用它们来查找要隐藏的post_id:
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:
数组([0] => 10 [1] => 8 [1] => 4)
Array ( [0] => 10 [1] => 8 [1] => 4 )
数据库没有任何变化.
这是数据库表,hide_post默认为0:
Here's the database table, hide_post defaults to 0:
post
post_id user_id标题正文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);
?>
从我所读到的有关错误的信息中可以看出:命令不同步;您现在不能运行此命令
,我想我需要使用 mutli_query
或 $ stmt-> store_result()
,但这是我第一次使用php和mysql,我不知道该怎么做.
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.
好的,我找到了另一种解决方法,我所做的就是将查询隐藏起来的查询移动到 while()
下面,该查询用于实际获取帖子信息.我希望我早点意识到这一点.
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.
推荐答案
事实证明,我的问题不是由查询本身引起的,而是由我将其放置在何处引起的.
It turns out that my problem was not caused by the query itself, but where I had placed it.
我为避免该错误所做的所有工作:命令不同步;您现在不能运行此命令
是要执行第二个查询以隐藏/删除用户选择的帖子,并将查询放在 while()
循环之后,每个帖子 fetch()
.
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.
这篇关于PHP MYSQL使用数组中的值更新多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!