PHP& MySQL函数从数据库问题中删除未经检查的类别 [英] PHP & MySQL function delete unchecked categories from database problem

查看:77
本文介绍了PHP& MySQL函数从数据库问题中删除未经检查的类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PHP函数,允许用户选择帖子中应显示的类别.我希望能够在用户取消选中复选框时从数据库中删除用户选择的类别.但是我尝试的所有操作似乎都删除了所有用户类别,或者根本无法正常工作.有人可以帮我解决这个问题吗?

I have a PHP function that allows users to pick which categories there post should be displayed in. I want to be able to delete a users picked category from the database when they unchecked a check box. But everything I tried seemed to delete all the users categories or not work at all. Can someone help me with this problem?

这是我的PHP函数的一部分,应该删除未选中的类别.

Here is part of my PHP function that should delete unchecked categories.

for ($x = 0; $x < count($query_cat_id); $x++){
    if($query_cat_id[$x] == $cat['id']){
        echo 'checked="checked"';
    } else if(!isset($query_cat_id[$x])) {
        $mysqli = mysqli_connect("localhost", "root", "", "sitename");
        $delete_id = mysqli_query($mysqli,"DELETE FROM posts_categories WHERE post_id = '" . $post_id . "'");
        if (!$delete_id) {
            print mysqli_error($mysqli);
        }
    }
}

推荐答案

此查询正在删除您帖子中的所有类别,因为您是在与where子句一起告诉您的.从本质上讲,您要从表posts_categories中删除所有帖子ID = $ post_id的记录.您需要添加到where子句中以使其更加具体.

This query is deleting all categories from your post because you are telling it to with you where clause. You are essentially saying, remove every record from the table posts_categories that has a post id = $post_id. You need to add to your where clause to make it more specific.

$delete_id = mysqli_query($mysqli,"DELETE FROM posts_categories WHERE post_id = '" . $post_id . "' AND categoryID = '". $query_cat_id[$x] ."'");

这现在说出与您所做的相同的事情,但增加了一个额外的限定词.仅从表中删除post_id为$ post_id且类别ID为(未选中复选框的值)的行.

This now says the same thing you did but adds an extra qualifier. Only delete rows from the table where the post_id is $post_id and the ID of the category is (value of your unchecked check box).

免责声明:我正在猜测您将类别ID存储在函数中的位置($ query_cat_id [$ x]).

这篇关于PHP&amp; MySQL函数从数据库问题中删除未经检查的类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆