duanbo6871 2018-11-24 09:51
浏览 31

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

I have created a chat system where students and academic staff can communicate between them. I have developed a form were I can get the name of the club were the conversation exists, and the value of the staff_id for another reason.

The problem is that I can not delete the records from 3 tables. I know that first I have to check if a specific id exists in staff_table or stu_table but I cannot figure out the query that I have to use.

The first table is the staff_message (columns: staff_id, m_id), the second is the stu_message (columns: stu_id, mid) and the third is the message table (columns: id, text, date, type).

Here you can see my code:

    <?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");

        }
        ?>

Thank you in advance!

EDIT: I tested each code separately and I saw that they both work. What I mean is that when there is only one row of student message then the message is deleted as well as the row in reference table (stu_message). That also applies for the academic staff table.

  • 写回答

2条回答 默认 最新

  • duanduji2986 2018-11-24 11:36
    关注

    May Be You Have 3 Tables 1.Staff_Message 2.Stu_Message 3.Message

    can you tell me which is the main id and which id refrence in tables then i can make query according your requirement

    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改