James
James

Reputation: 63

Being able to delete a certain result from an SQL while loop

I have looked around for an answer but cannot seem to find one.

I would like to be able to delete a specific query from the database.

How am I able to remove a specific one from the form and post it into an SQL statement to delete?

Here is my current select statement.

<?
if (isset($_POST['submit']))
{
  echo "Hello!";
  $selectOption = $_POST['info'];

  $query = "SELECT * FROM `REQUESTS` ORDER BY $selectOption"; 
  echo $query;
  $result = $db->query($query);
  if ($result == FALSE) { 
        die ("could not execute statement $query<br />");
  } 
    else
  echo "<form action='' method='post'>";
  echo "<table>";     
  while($row=$result->fetchRow()){ 

  if ($row['status'] == 0) {
      $type="rejected";
  }
  else if ($row['status'] == 1){
      $type="pending";
  }
  else if ($row['status'] == 2){
      $type="accepted";
  }


  echo "<tr id=".$type."><td>" . $row['requestid'] . "</td>";
  echo "<td>" . $row['booking_type'] . "</td>";
  echo "<td>" . $row['userid'] . "</td>";
  echo "<td>" . $row['moduleid'] . "</td>";
  echo "<td>" . $row['no_rooms'] . "</td>";
  echo "<td>" . $row['park'] . "</td>";
  echo "<td>" . $row['semester'] . "</td>";
  echo "<td>" . $row['day'] . "</td>";
  echo "<td>" . $row['start_time'] . "</td>";
  echo "<td>" . $row['end_time'] . "</td>";
  echo "<td>" . $row['length'] . "</td>";
  echo "<td>" . $row['students'] . "</td>";
  echo "<td>" . $row['priority'] . "</td>";
  echo "<td>" . $row['comments'] . "</td>";
  echo "<td>" . $row['status'] . "</td>";


  // <td class="'.(($row['status'] == 2) ? 'status-rejected' : 'status-accepted'). '">'.$row['status'].'</td>';
  echo "<td>".$stats."</td>";*/
  echo "<td>" . $row['round'] . "</td>";
  echo "<td>" . $row['date_submitted'] . "</td>";
  }

  echo "</tr>";

  echo "</table>"; 
  echo "</form>";
}
  echo "</tr>";
  echo "</table>"; 

Upvotes: 0

Views: 61

Answers (1)

DurtyFree
DurtyFree

Reputation: 79

I don't exactly understand what you are trying to delete.

If you want to actually delete a data / row in your database, you need to use the DELETE Statement as explained here: http://www.w3schools.com/sql/sql_delete.asp

I guess your statement would look something like this:

DELETE FROM REQUESTS
WHERE requestid=$row['requestid']; 

$row['requestid'] is the actual unique key identifier to the data row you would like to delete.

edit: You could create a button / link to the exact page with an GET data (yourpage.php?deleteid=1) to pass the id of the data you would like to delete from the database.

Keep in mind that using GET paratemers isn't the best way to do this. You should rather use a form and POST the regarding id.

Upvotes: 1

Related Questions