2016-01-06 105 views
0

我正在製作一個應用程序來保護你家門窗。該應用程序完成了80%,但我無法弄清楚如何刪除一個房子門的數據庫記錄。問題是我找不到一種方法來讀出正確的數據來刪除記錄。你可以看看我爲刪除頁面製作的腳本嗎?使用php刪除數據庫中的SQL數據頁面

我有一個頁面可以在表中插入數據以概述所有連接到應用程序的設備。

<section> 
     <form action="verwijderen.php" method="post"> 
      <?php 
       $sql = "SELECT device_id, naam, plaats.plaats, type.type, status FROM devices, type, plaats, status 
         WHERE devices.type_id = type.type_id 
         AND plaats.plaats_id = devices.plaats_id 
         AND status.status_id = devices.status_id"; 

        $result = $con->query($sql); 
        if ($result->num_rows > 0) 
        { 
         echo '<table><tr><td>ID</td><td>Naam</td><td>Plaats</td><td>Type</td><td>Status</td><td>Verwijderen</td></tr>'; 

         while($row = $result->fetch_assoc()) 
         { 

          echo '<tr>'; 
          echo '<td>' .$row['device_id']. '</td>'; 
          echo '<td>' .$row['naam']. '</td>'; 
          echo '<td>' .$row['plaats']. '</td>'; 
          echo '<td>' .$row['type']. '</td>'; 
          echo '<td>' .$row['status']. '</td>'; 
          echo '<td>'; 
      ?> 

      <input type="submit" value="Verwijderen" name="submit"> 
     </form>        
       <?php '</td>'; 
         } 
        echo '</table>';       
        } 
       ?> 
</section> 

我擁有的其他頁面是刪除插入到表中的記錄的頁面。刪除的問題是我無法找到刪除記錄的方法。以$ _ POST/$ _ GET工作正常..

<?php 
error_reporting(E_ERROR); 
include '../dbconnect.php'; 

//$connection = mysql_connect("", "", ""); // Establishing Connection with Server 
//$db = mysql_select_db("cerar", $connection); // Selecting Database from Server 
    if(isset($_POST['submit'])) 
    { // Fetching variables of the form which travels in URL 

    }  
    mysqli_close($con); // Closing Connection with Server 
    //header("refresh:3;url=index.php"); 
?> 
+0

您的表單標籤被關閉多次。將其移出循環。或者在循環中移動開始標籤。 – trincot

+0

以什麼方式幫助我從數據庫中刪除我的記錄? –

+0

我在我的答案中解釋過。我只是想強調你目前有一個無效的HTML結構。 – trincot

回答

0

添加記錄到數據庫中的HTML創建你的表中的每一行的形式,包括含有密鑰值傳遞給一個隱藏的輸入字段服務器。所以之前移動打開form標籤input type="submit"你,和包括另一個隱藏的輸入,像這樣:

<form action="verwijderen.php" method="post"> 
     <input type="hidden" value="<?=$row['device_id']?>" name="device_id"> 
     <input type="submit" value="Verwijderen" name="submit"> 
    </form>        

然後在PHP中,你可以發現,必須$_POST['device_id']被刪除的設備ID。編寫刪除語句應該很容易。

0
<section> 
    <form action="verwijderen.php" method="post"> 
    <?php 
     $sql = "SELECT device_id, naam, plaats.plaats, type.type, status FROM devices, type, plaats, status WHERE devices.type_id = type.type_id AND plaats.plaats_id = devices.plaats_id AND status.status_id = devices.status_id"; 
     $result = $con->query($sql); 
     if ($result->num_rows > 0) { 
      echo '<table> 
        <thead> 
         <tr> 
          <th>ID</th> 
          <th>Naam</th> 
          <th>Plaats</th> 
          <th>Type</th> 
          <th>Status</th> 
          <th>Verwijderen</th> 
         </tr> 
        </thead> 
        <tbody>'; 
      while($row = $result->fetch_assoc()){ 
        echo '<tr>'; 
         echo '<td>' .$row['device_id']. '</td>'; 
         echo '<td>' .$row['naam']. '</td>'; 
         echo '<td>' .$row['plaats']. '</td>'; 
         echo '<td>' .$row['type']. '</td>'; 
         echo '<td>' .$row['status']. '</td>'; 
         echo '<td>';?> 
          <input type="hidden" value="<?php echo $row['device_id']; ?>" name="device_id"> 
          <input type="submit" value="Verwijderen" name="submit"> 
         <?php '</td>'; 
      } 
      echo '</tbody> 
      </table>'; 
     } 
    ?> 
    </form> 
</section> 

PHP代碼

<?php 
error_reporting(E_ERROR); 
include '../dbconnect.php'; 
extract($_POST); 
//$connection = mysql_connect("", "", ""); // Establishing Connection with Server 
//$db = mysql_select_db("cerar", $connection); // Selecting Database from Server 
    if(isset($_POST['submit']) && $_POST['submit'] == 'Verwijderen') 
    { 
     //delete query 
     //DELETE FROM devices WHERE device_id = $device_id 
    }  
    mysqli_close($con); // Closing Connection with Server 
    //header("refresh:3;url=index.php"); 
?>