[英]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;?>">
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_postpost_id user_id标题正文hide_post
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
从我得到的有关错误的信息中可以看到:
Commands out of sync; you can't run this command now
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. Commands out of sync; you can't run this command now
我想我需要使用mutli_query
或$stmt->store_result()
但这是我第一次使用php和mysql,我不知道该如何处理。
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. 好了,我找到了另一种解决方法,我所做的就是将查询隐藏起来,将查询隐藏到运行实际获取信息的
while()
下面。 I wish I realized this sooner heh. 我希望我早点意识到这一点。
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错误。
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
为避免该错误,我所要做的就是:
Commands out of sync; you can't run this command now
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. Commands out of sync; you can't run this command now
是执行第二个查询以隐藏/删除用户选择的帖子,并将查询放在while()
循环之后,该循环将fetch()
每个帖子。
Basically I was trying to make a second query before the first query was finished. 基本上,我试图在第一个查询完成之前进行第二个查询。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.