Trying to create delete button to delete records in database












0















I am making a school project.



The meaning of this project is that i create a login/registration form,



where i can register, login, view the record.



But i also need to make a dropdown menu were i can see all the records (users) and below it i need to make a button and when i select a record/user and press the delete button it need to be deleted from the database. and i have no clue how i can do this.



here is my code:



<?php


include ("connectie.php");
include ("deletecode.php");



$conn = new mysqli("localhost", "student14_admin", "lol12345","student14_jordi");
mysqli_select_db($conn,'student14_jordi');

$sql = "DELETE FROM users WHERE ID='$_GET[id]'";



echo "<select name='user'>";
if ($result->num_rows>0) {
while($row = $result->fetch_assoc()) {
echo "<option value=" . $row['username'] . ">" . $row['username'] . "</option>";
}
}
echo "</select>";

?>


<form action="">
<a class="delete" href="delete.php?id=<?php echo $row['id']; ?>">Delete</a>
</form>









share|improve this question





























    0















    I am making a school project.



    The meaning of this project is that i create a login/registration form,



    where i can register, login, view the record.



    But i also need to make a dropdown menu were i can see all the records (users) and below it i need to make a button and when i select a record/user and press the delete button it need to be deleted from the database. and i have no clue how i can do this.



    here is my code:



    <?php


    include ("connectie.php");
    include ("deletecode.php");



    $conn = new mysqli("localhost", "student14_admin", "lol12345","student14_jordi");
    mysqli_select_db($conn,'student14_jordi');

    $sql = "DELETE FROM users WHERE ID='$_GET[id]'";



    echo "<select name='user'>";
    if ($result->num_rows>0) {
    while($row = $result->fetch_assoc()) {
    echo "<option value=" . $row['username'] . ">" . $row['username'] . "</option>";
    }
    }
    echo "</select>";

    ?>


    <form action="">
    <a class="delete" href="delete.php?id=<?php echo $row['id']; ?>">Delete</a>
    </form>









    share|improve this question



























      0












      0








      0








      I am making a school project.



      The meaning of this project is that i create a login/registration form,



      where i can register, login, view the record.



      But i also need to make a dropdown menu were i can see all the records (users) and below it i need to make a button and when i select a record/user and press the delete button it need to be deleted from the database. and i have no clue how i can do this.



      here is my code:



      <?php


      include ("connectie.php");
      include ("deletecode.php");



      $conn = new mysqli("localhost", "student14_admin", "lol12345","student14_jordi");
      mysqli_select_db($conn,'student14_jordi');

      $sql = "DELETE FROM users WHERE ID='$_GET[id]'";



      echo "<select name='user'>";
      if ($result->num_rows>0) {
      while($row = $result->fetch_assoc()) {
      echo "<option value=" . $row['username'] . ">" . $row['username'] . "</option>";
      }
      }
      echo "</select>";

      ?>


      <form action="">
      <a class="delete" href="delete.php?id=<?php echo $row['id']; ?>">Delete</a>
      </form>









      share|improve this question
















      I am making a school project.



      The meaning of this project is that i create a login/registration form,



      where i can register, login, view the record.



      But i also need to make a dropdown menu were i can see all the records (users) and below it i need to make a button and when i select a record/user and press the delete button it need to be deleted from the database. and i have no clue how i can do this.



      here is my code:



      <?php


      include ("connectie.php");
      include ("deletecode.php");



      $conn = new mysqli("localhost", "student14_admin", "lol12345","student14_jordi");
      mysqli_select_db($conn,'student14_jordi');

      $sql = "DELETE FROM users WHERE ID='$_GET[id]'";



      echo "<select name='user'>";
      if ($result->num_rows>0) {
      while($row = $result->fetch_assoc()) {
      echo "<option value=" . $row['username'] . ">" . $row['username'] . "</option>";
      }
      }
      echo "</select>";

      ?>


      <form action="">
      <a class="delete" href="delete.php?id=<?php echo $row['id']; ?>">Delete</a>
      </form>






      php html sql database mysqli






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 27 '18 at 12:45







      Jordi manders

















      asked Nov 27 '18 at 10:20









      Jordi mandersJordi manders

      64




      64
























          2 Answers
          2






          active

          oldest

          votes


















          1














          Create a button and name it Delete



          <a class="delete" href="delete.php?id=<?php echo $row['id']; ?>">Delete</a>


          Then make a delete.php file and have these code there.



          <?php include_once 'db_config_filename.php';
          // get id value
          $id = $_GET['id'];

          // sql to delete a record
          $sql = "DELETE FROM tablename WHERE id='$id'";

          // print_r($sql);

          if (mysqli_query($conn, $sql)) {
          echo "Record deleted successfully";
          }
          else {
          echo "Error deleting record: " . mysqli_error($conn);
          }

          //redirect here
          include 'data.php';

          ?>





          share|improve this answer
























          • im getting a 503 service error

            – Jordi manders
            Nov 27 '18 at 10:58











          • paste your codes here

            – Nayeem Parvez Chowdhury
            Nov 27 '18 at 12:37











          • i updated the code now i also got an extra file for the delete code

            – Jordi manders
            Nov 27 '18 at 12:44













          • syntax problem DELETE FROM users WHERE id =$_GET['id']; still without seeing all the codes I couldn't get you

            – Nayeem Parvez Chowdhury
            Nov 27 '18 at 15:03













          • Sorry i don't understand that so what do i need to do? because nothing is working now

            – Jordi manders
            Nov 28 '18 at 7:38



















          0














          I almost have it working. i have now this code:



          delete.php:

          <?php

          include ("connectie.php");


          $sql = "SELECT * FROM users ";
          $result = $conn->query($sql);

          echo "<select name='user'>";
          if ($result->num_rows>0) {
          while($row = $result->fetch_assoc()) {
          echo "<option value=" . $row['username'] . ">" . $row['username'] . "
          </option>";
          }
          }
          echo "</select>";

          ?>


          <form action="" method="GET">
          <input type="hidden" name="id" value=".$row['id']." />
          <input type="submit" name="delete" value="verwijderen">
          </form>

          <?php
          include ("deletecode.php");
          ?>

          Deletecode.php:

          <?php
          $servername = "localhost";
          $username = "student14_admin";
          $password = "lol12345";
          $dbname = "student14_jordi";

          // Create connection
          $conn = mysqli_connect($servername, $username, $password, $dbname);
          // Check connection
          if (!$conn) {
          die("Connection failed: " . mysqli_connect_error());
          }

          // sql to delete a record
          $sql = "DELETE FROM users WHERE id=3";

          if (mysqli_query($conn, $sql)) {
          echo "Record deleted successfully";
          } else {
          echo "Error deleting record: " . mysqli_error($conn);
          }

          mysqli_close($conn);
          ?>


          well this works for only record 3 afcourse. but now i need to have it that if i select a record from the dropdown menu and click on the delete button it needs to delete that record from my database.






          share|improve this answer



















          • 1





            I fixed it already

            – Jordi manders
            Nov 28 '18 at 12:32











          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%2f53497460%2ftrying-to-create-delete-button-to-delete-records-in-database%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









          1














          Create a button and name it Delete



          <a class="delete" href="delete.php?id=<?php echo $row['id']; ?>">Delete</a>


          Then make a delete.php file and have these code there.



          <?php include_once 'db_config_filename.php';
          // get id value
          $id = $_GET['id'];

          // sql to delete a record
          $sql = "DELETE FROM tablename WHERE id='$id'";

          // print_r($sql);

          if (mysqli_query($conn, $sql)) {
          echo "Record deleted successfully";
          }
          else {
          echo "Error deleting record: " . mysqli_error($conn);
          }

          //redirect here
          include 'data.php';

          ?>





          share|improve this answer
























          • im getting a 503 service error

            – Jordi manders
            Nov 27 '18 at 10:58











          • paste your codes here

            – Nayeem Parvez Chowdhury
            Nov 27 '18 at 12:37











          • i updated the code now i also got an extra file for the delete code

            – Jordi manders
            Nov 27 '18 at 12:44













          • syntax problem DELETE FROM users WHERE id =$_GET['id']; still without seeing all the codes I couldn't get you

            – Nayeem Parvez Chowdhury
            Nov 27 '18 at 15:03













          • Sorry i don't understand that so what do i need to do? because nothing is working now

            – Jordi manders
            Nov 28 '18 at 7:38
















          1














          Create a button and name it Delete



          <a class="delete" href="delete.php?id=<?php echo $row['id']; ?>">Delete</a>


          Then make a delete.php file and have these code there.



          <?php include_once 'db_config_filename.php';
          // get id value
          $id = $_GET['id'];

          // sql to delete a record
          $sql = "DELETE FROM tablename WHERE id='$id'";

          // print_r($sql);

          if (mysqli_query($conn, $sql)) {
          echo "Record deleted successfully";
          }
          else {
          echo "Error deleting record: " . mysqli_error($conn);
          }

          //redirect here
          include 'data.php';

          ?>





          share|improve this answer
























          • im getting a 503 service error

            – Jordi manders
            Nov 27 '18 at 10:58











          • paste your codes here

            – Nayeem Parvez Chowdhury
            Nov 27 '18 at 12:37











          • i updated the code now i also got an extra file for the delete code

            – Jordi manders
            Nov 27 '18 at 12:44













          • syntax problem DELETE FROM users WHERE id =$_GET['id']; still without seeing all the codes I couldn't get you

            – Nayeem Parvez Chowdhury
            Nov 27 '18 at 15:03













          • Sorry i don't understand that so what do i need to do? because nothing is working now

            – Jordi manders
            Nov 28 '18 at 7:38














          1












          1








          1







          Create a button and name it Delete



          <a class="delete" href="delete.php?id=<?php echo $row['id']; ?>">Delete</a>


          Then make a delete.php file and have these code there.



          <?php include_once 'db_config_filename.php';
          // get id value
          $id = $_GET['id'];

          // sql to delete a record
          $sql = "DELETE FROM tablename WHERE id='$id'";

          // print_r($sql);

          if (mysqli_query($conn, $sql)) {
          echo "Record deleted successfully";
          }
          else {
          echo "Error deleting record: " . mysqli_error($conn);
          }

          //redirect here
          include 'data.php';

          ?>





          share|improve this answer













          Create a button and name it Delete



          <a class="delete" href="delete.php?id=<?php echo $row['id']; ?>">Delete</a>


          Then make a delete.php file and have these code there.



          <?php include_once 'db_config_filename.php';
          // get id value
          $id = $_GET['id'];

          // sql to delete a record
          $sql = "DELETE FROM tablename WHERE id='$id'";

          // print_r($sql);

          if (mysqli_query($conn, $sql)) {
          echo "Record deleted successfully";
          }
          else {
          echo "Error deleting record: " . mysqli_error($conn);
          }

          //redirect here
          include 'data.php';

          ?>






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 27 '18 at 10:39









          Nayeem Parvez ChowdhuryNayeem Parvez Chowdhury

          397




          397













          • im getting a 503 service error

            – Jordi manders
            Nov 27 '18 at 10:58











          • paste your codes here

            – Nayeem Parvez Chowdhury
            Nov 27 '18 at 12:37











          • i updated the code now i also got an extra file for the delete code

            – Jordi manders
            Nov 27 '18 at 12:44













          • syntax problem DELETE FROM users WHERE id =$_GET['id']; still without seeing all the codes I couldn't get you

            – Nayeem Parvez Chowdhury
            Nov 27 '18 at 15:03













          • Sorry i don't understand that so what do i need to do? because nothing is working now

            – Jordi manders
            Nov 28 '18 at 7:38



















          • im getting a 503 service error

            – Jordi manders
            Nov 27 '18 at 10:58











          • paste your codes here

            – Nayeem Parvez Chowdhury
            Nov 27 '18 at 12:37











          • i updated the code now i also got an extra file for the delete code

            – Jordi manders
            Nov 27 '18 at 12:44













          • syntax problem DELETE FROM users WHERE id =$_GET['id']; still without seeing all the codes I couldn't get you

            – Nayeem Parvez Chowdhury
            Nov 27 '18 at 15:03













          • Sorry i don't understand that so what do i need to do? because nothing is working now

            – Jordi manders
            Nov 28 '18 at 7:38

















          im getting a 503 service error

          – Jordi manders
          Nov 27 '18 at 10:58





          im getting a 503 service error

          – Jordi manders
          Nov 27 '18 at 10:58













          paste your codes here

          – Nayeem Parvez Chowdhury
          Nov 27 '18 at 12:37





          paste your codes here

          – Nayeem Parvez Chowdhury
          Nov 27 '18 at 12:37













          i updated the code now i also got an extra file for the delete code

          – Jordi manders
          Nov 27 '18 at 12:44







          i updated the code now i also got an extra file for the delete code

          – Jordi manders
          Nov 27 '18 at 12:44















          syntax problem DELETE FROM users WHERE id =$_GET['id']; still without seeing all the codes I couldn't get you

          – Nayeem Parvez Chowdhury
          Nov 27 '18 at 15:03







          syntax problem DELETE FROM users WHERE id =$_GET['id']; still without seeing all the codes I couldn't get you

          – Nayeem Parvez Chowdhury
          Nov 27 '18 at 15:03















          Sorry i don't understand that so what do i need to do? because nothing is working now

          – Jordi manders
          Nov 28 '18 at 7:38





          Sorry i don't understand that so what do i need to do? because nothing is working now

          – Jordi manders
          Nov 28 '18 at 7:38













          0














          I almost have it working. i have now this code:



          delete.php:

          <?php

          include ("connectie.php");


          $sql = "SELECT * FROM users ";
          $result = $conn->query($sql);

          echo "<select name='user'>";
          if ($result->num_rows>0) {
          while($row = $result->fetch_assoc()) {
          echo "<option value=" . $row['username'] . ">" . $row['username'] . "
          </option>";
          }
          }
          echo "</select>";

          ?>


          <form action="" method="GET">
          <input type="hidden" name="id" value=".$row['id']." />
          <input type="submit" name="delete" value="verwijderen">
          </form>

          <?php
          include ("deletecode.php");
          ?>

          Deletecode.php:

          <?php
          $servername = "localhost";
          $username = "student14_admin";
          $password = "lol12345";
          $dbname = "student14_jordi";

          // Create connection
          $conn = mysqli_connect($servername, $username, $password, $dbname);
          // Check connection
          if (!$conn) {
          die("Connection failed: " . mysqli_connect_error());
          }

          // sql to delete a record
          $sql = "DELETE FROM users WHERE id=3";

          if (mysqli_query($conn, $sql)) {
          echo "Record deleted successfully";
          } else {
          echo "Error deleting record: " . mysqli_error($conn);
          }

          mysqli_close($conn);
          ?>


          well this works for only record 3 afcourse. but now i need to have it that if i select a record from the dropdown menu and click on the delete button it needs to delete that record from my database.






          share|improve this answer



















          • 1





            I fixed it already

            – Jordi manders
            Nov 28 '18 at 12:32
















          0














          I almost have it working. i have now this code:



          delete.php:

          <?php

          include ("connectie.php");


          $sql = "SELECT * FROM users ";
          $result = $conn->query($sql);

          echo "<select name='user'>";
          if ($result->num_rows>0) {
          while($row = $result->fetch_assoc()) {
          echo "<option value=" . $row['username'] . ">" . $row['username'] . "
          </option>";
          }
          }
          echo "</select>";

          ?>


          <form action="" method="GET">
          <input type="hidden" name="id" value=".$row['id']." />
          <input type="submit" name="delete" value="verwijderen">
          </form>

          <?php
          include ("deletecode.php");
          ?>

          Deletecode.php:

          <?php
          $servername = "localhost";
          $username = "student14_admin";
          $password = "lol12345";
          $dbname = "student14_jordi";

          // Create connection
          $conn = mysqli_connect($servername, $username, $password, $dbname);
          // Check connection
          if (!$conn) {
          die("Connection failed: " . mysqli_connect_error());
          }

          // sql to delete a record
          $sql = "DELETE FROM users WHERE id=3";

          if (mysqli_query($conn, $sql)) {
          echo "Record deleted successfully";
          } else {
          echo "Error deleting record: " . mysqli_error($conn);
          }

          mysqli_close($conn);
          ?>


          well this works for only record 3 afcourse. but now i need to have it that if i select a record from the dropdown menu and click on the delete button it needs to delete that record from my database.






          share|improve this answer



















          • 1





            I fixed it already

            – Jordi manders
            Nov 28 '18 at 12:32














          0












          0








          0







          I almost have it working. i have now this code:



          delete.php:

          <?php

          include ("connectie.php");


          $sql = "SELECT * FROM users ";
          $result = $conn->query($sql);

          echo "<select name='user'>";
          if ($result->num_rows>0) {
          while($row = $result->fetch_assoc()) {
          echo "<option value=" . $row['username'] . ">" . $row['username'] . "
          </option>";
          }
          }
          echo "</select>";

          ?>


          <form action="" method="GET">
          <input type="hidden" name="id" value=".$row['id']." />
          <input type="submit" name="delete" value="verwijderen">
          </form>

          <?php
          include ("deletecode.php");
          ?>

          Deletecode.php:

          <?php
          $servername = "localhost";
          $username = "student14_admin";
          $password = "lol12345";
          $dbname = "student14_jordi";

          // Create connection
          $conn = mysqli_connect($servername, $username, $password, $dbname);
          // Check connection
          if (!$conn) {
          die("Connection failed: " . mysqli_connect_error());
          }

          // sql to delete a record
          $sql = "DELETE FROM users WHERE id=3";

          if (mysqli_query($conn, $sql)) {
          echo "Record deleted successfully";
          } else {
          echo "Error deleting record: " . mysqli_error($conn);
          }

          mysqli_close($conn);
          ?>


          well this works for only record 3 afcourse. but now i need to have it that if i select a record from the dropdown menu and click on the delete button it needs to delete that record from my database.






          share|improve this answer













          I almost have it working. i have now this code:



          delete.php:

          <?php

          include ("connectie.php");


          $sql = "SELECT * FROM users ";
          $result = $conn->query($sql);

          echo "<select name='user'>";
          if ($result->num_rows>0) {
          while($row = $result->fetch_assoc()) {
          echo "<option value=" . $row['username'] . ">" . $row['username'] . "
          </option>";
          }
          }
          echo "</select>";

          ?>


          <form action="" method="GET">
          <input type="hidden" name="id" value=".$row['id']." />
          <input type="submit" name="delete" value="verwijderen">
          </form>

          <?php
          include ("deletecode.php");
          ?>

          Deletecode.php:

          <?php
          $servername = "localhost";
          $username = "student14_admin";
          $password = "lol12345";
          $dbname = "student14_jordi";

          // Create connection
          $conn = mysqli_connect($servername, $username, $password, $dbname);
          // Check connection
          if (!$conn) {
          die("Connection failed: " . mysqli_connect_error());
          }

          // sql to delete a record
          $sql = "DELETE FROM users WHERE id=3";

          if (mysqli_query($conn, $sql)) {
          echo "Record deleted successfully";
          } else {
          echo "Error deleting record: " . mysqli_error($conn);
          }

          mysqli_close($conn);
          ?>


          well this works for only record 3 afcourse. but now i need to have it that if i select a record from the dropdown menu and click on the delete button it needs to delete that record from my database.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 28 '18 at 9:05









          Jordi mandersJordi manders

          64




          64








          • 1





            I fixed it already

            – Jordi manders
            Nov 28 '18 at 12:32














          • 1





            I fixed it already

            – Jordi manders
            Nov 28 '18 at 12:32








          1




          1





          I fixed it already

          – Jordi manders
          Nov 28 '18 at 12:32





          I fixed it already

          – Jordi manders
          Nov 28 '18 at 12:32


















          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%2f53497460%2ftrying-to-create-delete-button-to-delete-records-in-database%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







          Popular posts from this blog

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

          Calculate evaluation metrics using cross_val_predict sklearn

          Insert data from modal to MySQL (multiple modal on website)