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





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















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.










share|improve this question































    0















    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.










    share|improve this question



























      0












      0








      0








      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.










      share|improve this question
















      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.







      php sql database






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 2 '18 at 20:50









      marc_s

      586k13011261272




      586k13011261272










      asked Nov 24 '18 at 9:51









      EdwardCooperEdwardCooper

      206




      206
























          2 Answers
          2






          active

          oldest

          votes


















          0














          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






          share|improve this answer
























          • First of all thank you @Dilip Shekhawat about your answer. Here is a picture of my tables: imgur.com/9GwhZsr If there is any problem inform me

            – EdwardCooper
            Nov 24 '18 at 11:47





















          0














          According To Your Image Your Base Table Is Message Then Mid Refer In Two Table(stu_message and staff_message)--- Right?






          share|improve this answer
























          • yes the main table is the message and the mid ref the other two tables

            – EdwardCooper
            Nov 24 '18 at 12:03











          • Ok i Think You Can Change Serial Of Record Delete 1.You Can Find Id From Name Based And Store Your ID In Variable Then You Delete Record In Message Table Then You Can Delete Record From Both Table According Assign Variable Id

            – Dilip Shekhawat
            Nov 24 '18 at 12:06













          • I can't delete the record from message table. I have to delete the record from the ref. table first (staff_message, stu_message)

            – EdwardCooper
            Nov 24 '18 at 12:10













          • $quer = mysqli_query($con,"SELECT id FROM message WHERE type='$name'"); while($row = mysqli_fetch_array($quer)){ $message_id = $row['id']; } When You Putting This Code Then You Get Correct Id

            – Dilip Shekhawat
            Nov 24 '18 at 12:16











          • I get the correct id for every record that the type equals with the name of the club

            – EdwardCooper
            Nov 24 '18 at 12:20












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53456994%2fphp-sql-delete-value-if-exists-in-one-of-my-two-tables%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          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






          share|improve this answer
























          • First of all thank you @Dilip Shekhawat about your answer. Here is a picture of my tables: imgur.com/9GwhZsr If there is any problem inform me

            – EdwardCooper
            Nov 24 '18 at 11:47


















          0














          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






          share|improve this answer
























          • First of all thank you @Dilip Shekhawat about your answer. Here is a picture of my tables: imgur.com/9GwhZsr If there is any problem inform me

            – EdwardCooper
            Nov 24 '18 at 11:47
















          0












          0








          0







          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






          share|improve this answer













          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







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 24 '18 at 11:36









          Dilip ShekhawatDilip Shekhawat

          399




          399













          • First of all thank you @Dilip Shekhawat about your answer. Here is a picture of my tables: imgur.com/9GwhZsr If there is any problem inform me

            – EdwardCooper
            Nov 24 '18 at 11:47





















          • First of all thank you @Dilip Shekhawat about your answer. Here is a picture of my tables: imgur.com/9GwhZsr If there is any problem inform me

            – EdwardCooper
            Nov 24 '18 at 11:47



















          First of all thank you @Dilip Shekhawat about your answer. Here is a picture of my tables: imgur.com/9GwhZsr If there is any problem inform me

          – EdwardCooper
          Nov 24 '18 at 11:47







          First of all thank you @Dilip Shekhawat about your answer. Here is a picture of my tables: imgur.com/9GwhZsr If there is any problem inform me

          – EdwardCooper
          Nov 24 '18 at 11:47















          0














          According To Your Image Your Base Table Is Message Then Mid Refer In Two Table(stu_message and staff_message)--- Right?






          share|improve this answer
























          • yes the main table is the message and the mid ref the other two tables

            – EdwardCooper
            Nov 24 '18 at 12:03











          • Ok i Think You Can Change Serial Of Record Delete 1.You Can Find Id From Name Based And Store Your ID In Variable Then You Delete Record In Message Table Then You Can Delete Record From Both Table According Assign Variable Id

            – Dilip Shekhawat
            Nov 24 '18 at 12:06













          • I can't delete the record from message table. I have to delete the record from the ref. table first (staff_message, stu_message)

            – EdwardCooper
            Nov 24 '18 at 12:10













          • $quer = mysqli_query($con,"SELECT id FROM message WHERE type='$name'"); while($row = mysqli_fetch_array($quer)){ $message_id = $row['id']; } When You Putting This Code Then You Get Correct Id

            – Dilip Shekhawat
            Nov 24 '18 at 12:16











          • I get the correct id for every record that the type equals with the name of the club

            – EdwardCooper
            Nov 24 '18 at 12:20
















          0














          According To Your Image Your Base Table Is Message Then Mid Refer In Two Table(stu_message and staff_message)--- Right?






          share|improve this answer
























          • yes the main table is the message and the mid ref the other two tables

            – EdwardCooper
            Nov 24 '18 at 12:03











          • Ok i Think You Can Change Serial Of Record Delete 1.You Can Find Id From Name Based And Store Your ID In Variable Then You Delete Record In Message Table Then You Can Delete Record From Both Table According Assign Variable Id

            – Dilip Shekhawat
            Nov 24 '18 at 12:06













          • I can't delete the record from message table. I have to delete the record from the ref. table first (staff_message, stu_message)

            – EdwardCooper
            Nov 24 '18 at 12:10













          • $quer = mysqli_query($con,"SELECT id FROM message WHERE type='$name'"); while($row = mysqli_fetch_array($quer)){ $message_id = $row['id']; } When You Putting This Code Then You Get Correct Id

            – Dilip Shekhawat
            Nov 24 '18 at 12:16











          • I get the correct id for every record that the type equals with the name of the club

            – EdwardCooper
            Nov 24 '18 at 12:20














          0












          0








          0







          According To Your Image Your Base Table Is Message Then Mid Refer In Two Table(stu_message and staff_message)--- Right?






          share|improve this answer













          According To Your Image Your Base Table Is Message Then Mid Refer In Two Table(stu_message and staff_message)--- Right?







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 24 '18 at 12:01









          Dilip ShekhawatDilip Shekhawat

          399




          399













          • yes the main table is the message and the mid ref the other two tables

            – EdwardCooper
            Nov 24 '18 at 12:03











          • Ok i Think You Can Change Serial Of Record Delete 1.You Can Find Id From Name Based And Store Your ID In Variable Then You Delete Record In Message Table Then You Can Delete Record From Both Table According Assign Variable Id

            – Dilip Shekhawat
            Nov 24 '18 at 12:06













          • I can't delete the record from message table. I have to delete the record from the ref. table first (staff_message, stu_message)

            – EdwardCooper
            Nov 24 '18 at 12:10













          • $quer = mysqli_query($con,"SELECT id FROM message WHERE type='$name'"); while($row = mysqli_fetch_array($quer)){ $message_id = $row['id']; } When You Putting This Code Then You Get Correct Id

            – Dilip Shekhawat
            Nov 24 '18 at 12:16











          • I get the correct id for every record that the type equals with the name of the club

            – EdwardCooper
            Nov 24 '18 at 12:20



















          • yes the main table is the message and the mid ref the other two tables

            – EdwardCooper
            Nov 24 '18 at 12:03











          • Ok i Think You Can Change Serial Of Record Delete 1.You Can Find Id From Name Based And Store Your ID In Variable Then You Delete Record In Message Table Then You Can Delete Record From Both Table According Assign Variable Id

            – Dilip Shekhawat
            Nov 24 '18 at 12:06













          • I can't delete the record from message table. I have to delete the record from the ref. table first (staff_message, stu_message)

            – EdwardCooper
            Nov 24 '18 at 12:10













          • $quer = mysqli_query($con,"SELECT id FROM message WHERE type='$name'"); while($row = mysqli_fetch_array($quer)){ $message_id = $row['id']; } When You Putting This Code Then You Get Correct Id

            – Dilip Shekhawat
            Nov 24 '18 at 12:16











          • I get the correct id for every record that the type equals with the name of the club

            – EdwardCooper
            Nov 24 '18 at 12:20

















          yes the main table is the message and the mid ref the other two tables

          – EdwardCooper
          Nov 24 '18 at 12:03





          yes the main table is the message and the mid ref the other two tables

          – EdwardCooper
          Nov 24 '18 at 12:03













          Ok i Think You Can Change Serial Of Record Delete 1.You Can Find Id From Name Based And Store Your ID In Variable Then You Delete Record In Message Table Then You Can Delete Record From Both Table According Assign Variable Id

          – Dilip Shekhawat
          Nov 24 '18 at 12:06







          Ok i Think You Can Change Serial Of Record Delete 1.You Can Find Id From Name Based And Store Your ID In Variable Then You Delete Record In Message Table Then You Can Delete Record From Both Table According Assign Variable Id

          – Dilip Shekhawat
          Nov 24 '18 at 12:06















          I can't delete the record from message table. I have to delete the record from the ref. table first (staff_message, stu_message)

          – EdwardCooper
          Nov 24 '18 at 12:10







          I can't delete the record from message table. I have to delete the record from the ref. table first (staff_message, stu_message)

          – EdwardCooper
          Nov 24 '18 at 12:10















          $quer = mysqli_query($con,"SELECT id FROM message WHERE type='$name'"); while($row = mysqli_fetch_array($quer)){ $message_id = $row['id']; } When You Putting This Code Then You Get Correct Id

          – Dilip Shekhawat
          Nov 24 '18 at 12:16





          $quer = mysqli_query($con,"SELECT id FROM message WHERE type='$name'"); while($row = mysqli_fetch_array($quer)){ $message_id = $row['id']; } When You Putting This Code Then You Get Correct Id

          – Dilip Shekhawat
          Nov 24 '18 at 12:16













          I get the correct id for every record that the type equals with the name of the club

          – EdwardCooper
          Nov 24 '18 at 12:20





          I get the correct id for every record that the type equals with the name of the club

          – EdwardCooper
          Nov 24 '18 at 12:20


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53456994%2fphp-sql-delete-value-if-exists-in-one-of-my-two-tables%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Academy of Television Arts & Sciences

          L'Équipe

          1995 France bombings