简体   繁体   English

能够从SQL while循环中删除某些结果

[英]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? 我如何从表单中删除特定的表单并将其发布到SQL语句中进行删除?

Here is my current select statement. 这是我当前的select语句。

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

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 如果要实际删除数据库中的数据/行,则需要按照以下说明使用DELETE语句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. $ row ['requestid']是您要删除的数据行的实际唯一键标识符。

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. 编辑:您可以创建一个按钮/链接到具有GET数据(yourpage.php?deleteid = 1)的确切页面,以传递要从数据库中删除的数据的ID。

Keep in mind that using GET paratemers isn't the best way to do this. 请记住,使用GET解析器并不是做到这一点的最佳方法。 You should rather use a form and POST the regarding id. 您应该使用表单,然后发布About ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM