title: Trying to delete specific row in a list of items from a MySQL database using PHP
body: <p>I have a list of items that is being output via PHP / MySQL. I also have an Edit button and a Delete button in one column. I am trying to figure out how to delete a list item on a specific row by clicking the Delete button. I have tried the following:</p>
  
  <pre><code>$id = $_GET['id'];
  
  if(isset($_POST["deletelist"])) {
  
      $query = "SELECT * FROM lists";
      $result = mysqli_query($db, $query);
      if(mysqli_num_rows($result) == 1) {
  
      $query = "DELETE FROM lists WHERE id = '$id'";
  } else {
          echo "Cannot delete";
      }
  }
  </code></pre>
  
  <p>This of course does not work. Can anyone help me out with this?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>This is the code for the entire page:</p>
  
  <p><a href="https://pastebin.com/raw/qjnZkUU2" rel="nofollow noreferrer">https://pastebin.com/raw/qjnZkUU2</a></p>
  
  <p><strong>UPDATED CODE</strong></p>
  
  <pre><code>$id = $_GET['id'];
  
  if(isset($_POST["deletelist"])) {
  
      $query = "SELECT * FROM lists";
      $result = mysqli_query($db, $query);
      if(mysqli_num_rows($result) == 1) {
  
      $query = "DELETE FROM lists WHERE id = '$id'";
      mysqli_query($db, $query);
  } else {
          echo "Cannot delete";
      }
  }
  </code></pre>
  
  <p>What I am confused about is how does the query know which item to delete? Should I be appending the ID to the URL to pass the ID?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Ok I think I get it....In the delete button, I need to echo the ID of that row so when the query runs from clicking the delete button, it knows which ID to delete correct?</p>
  
  <p><strong>RESOLVED</strong></p>
  
  <p>Alright. I got it figured out!</p>
  
  <p>I have a button that references the ID of the list item:</p>
  
  <pre><code>echo "&lt;a href='includes/deletelist.php?id=$row[id]'&gt;&lt;input class=\"btn btn-danger\" value=Delete style=\"width: 85px;\"&gt;&lt;/a&gt;
  </code></pre>
  
  <p>I then pass that ID to deletelist.php</p>
  
  <pre><code>if (!isset($_GET['id'])) {
      echo 'No ID was given...';
      exit;
  }
  
  if ($db-&gt;connect_error) {
      die('Connect Error (' . $con-&gt;connect_errno . ') ' . $con-&gt;connect_error);
  }
  
  $sql = "DELETE FROM lists WHERE id = ?";
  if (!$result = $db-&gt;prepare($sql)) {
      die('Query failed: (' . $db-&gt;errno . ') ' . $db-&gt;error);
  }
  </code></pre>
  
  <p>Item gets deleted.</p>