繁体   English   中英

如果我的两个表之一中存在php-sql删除值

[英]php-sql delete value if exists in one of my two tables

我创建了一个聊天系统,学生和学术人员可以在它们之间进行交流。 我已经开发了一种表单,可以在对话存在的情况下获取俱乐部的名称,以及出于其他原因而获得staff_id的值。

问题是我无法从3个表中删除记录。 我知道,首先我必须检查staff_tablestu_table是否存在特定的ID,但是我无法弄清楚必须使用的查询。

第一个表是staff_message (列: staff_id, m_id ),第二个表是stu_message (列: stu_id, mid ),第三个表是message表(列: id, text, date, type )。

在这里您可以看到我的代码:

    <?php
        include_once('connect.php');

        $name=mysqli_real_escape_string($con,$_POST['clubname']);
        $staff_id=mysqli_real_escape_string($con,$_POST['staff_id']);

        if(empty($name)) {
            header("Location: ../options.php?index==empty");
            exit(); 
        }
        else {
            $quer = mysqli_query($con,"SELECT id FROM message WHERE type='$name'");
            while($row = mysqli_fetch_array($quer)) {   
                $message_id = $row['id'];
            }

            $query4 = mysqli_query($con,"SELECT * FROM staff_message WHERE m_id='$message_id'");

            if(mysqli_num_rows($query4) >= 1)
            {
                $query5 = mysqli_query($con,"DELETE FROM staff_message WHERE m_id='$message_id'");
            } else {
                    echo "not here staff"; //see if finds the value   
            }

            $query8 = mysqli_query($con,"SELECT * FROM stu_message WHERE mid='$message_id'");   

            if(mysqli_num_rows($query8) >= 1)
            {
                 $query5 = mysqli_query($con,"DELETE FROM stu_message WHERE mid='$message_id'");
            } else {
                    echo "not here stu";//see if finds the value   
            }


    $query= mysqli_query($con,"DELETE FROM message WHERE type = '$name'");  

    header("Location: ../options.php?delete==success");

        }
        ?>

先感谢您!

编辑:我分别测试了每个代码,我看到它们都可以工作。 我的意思是,当只有一行学生消息时,该消息以及参考表中的行( stu_message )都会被删除。 这也适用于教职员工表。

可能您有3个表1.Staff_Message 2.Stu_Message 3.Message

您能告诉我哪个是主要ID,哪些ID在表格中反映,然后我可以根据您的要求进行查询

根据您的图像,您的基表是消息,然后中间引用两个表(stu_message和staff_message)---对吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM