2017-08-29 135 views
0

所以我在今年夏天開始語言,我有問題,我不知道如何使刪除按鈕,我看了很多頁面,但我無法找到如何與pdo 。我想刪除按鈕

countryandcity.php

<?php 

    require 'pdo.php'; 
    $connect=connect(); 

?><!DOCTYPE html> 
<html> 
    <head> 
     <link href="style.css" type="text/css" rel="stylesheet" /> 
    </head> 
    <body> 
     <div class="wrapper"> 

      <table class="table" > 

       <thead> 
        <tr> 
         <th>Country</th> 
         <th>City</th> 
         <th>Image</th> 
        </tr> 
        <form action="deleteall.php" method="POST" > 

        <?php 
         $sql = connect()->prepare("SELECT * FROM countries ORDER BY country"); 
         $sql->execute(); 
         while($result = $sql->fetch(PDO::FETCH_ASSOC)) { 

          echo"<tr>"; 
          echo"<td>".$result['country']."</td>"; 
          echo"<td>".$result['city']."</td>"; 
          if(!empty($result['image'])){ 
           echo '<td><img src="images/'.$result['image'].'"/></td>'; 
          } 

          else { 
           echo"<td>-</td>"; 
          } 

          echo "<td><a href='edit.php?uid=".$result['country']."'>Edit</a></td>"; 
          echo "<td><a href='deleteall.php?uid=".$result['country']."'>Delete</a></td>"; 

          echo"</tr>"; 
         } 
        ?> 
       </form> 
      </thead> 
     </table> 
    </div> 
</body> 

deleteall.php

<?php 

require 'pdo.php'; 
$connect=connect(); 
if(isset($_POST['delete_btn'])) 

    ?><!DOCTYPE html> 
    <html> 
     <head> 
      <title></title> 
     </head> 
     <body> 

      <form> 
       <div class="form-group"> 
        <label>Do u want to delete?</label> 
       </div> 
       <button> 
        <input type="submit" value="YES" name="delete_btn"> 
       </button> 
       <button> 
        <input type="submit" name="no" value="NO"> 

       </button> 
      </form> 
     </body> 
    </html> 

請幫我SQL查詢和PHP代碼後,如果(isset),我需要幫幫我! 我只想從數據庫中刪除行。

我該如何解決這個問題? 對不起,我的英語不好。

謝謝!

+2

那麼,如果你想刪除記錄,那麼SQL查詢將涉及'DELETE'關鍵字。當然,各種介紹性教程涵蓋了這一點。說實話,聽起來你應該從一些教程開始,並獲得更多的基礎知識。 – David

+0

要刪除詳細信息頁面中的操作還是要添加刪除的鏈接? –

+0

可能重複的[如何刪除mysql中的行?](https://stackoverflow.com/questions/32755253/how-to-delete-a-row-in-mysql) – Martin

回答

0

這是你首創頁的校正(在<tbody>而不是<thead>內容,不<form>需要,...)countryandcity.php:刪除你的記錄在deleteall

  <table> 
       <thead> 
        <tr> 
         <th>column name...</th> 
        </tr> 
       </thead> 
       <tbody> 

<?php 

$sql = connect()->prepare("SELECT * FROM countries ORDER BY country"); 
$sql->execute(); 
if($result = $sql->fetch(PDO::FETCH_ASSOC)) { 
    foreach($result as $i => $item) { 
     extract($item); 

     echo "<tr><td>$country</td>"; 
     echo "<td>$city</td>"; 

     if(!empty($image)) { 
      echo "<td><img src=\"images/$image\"/></td>"; 
     } 

     else { 
      echo "<td>-</td>"; 
     } 

     echo "<td><a href=\"edit.php?uid=$country\">Edit</a></td>"; 
     echo "<td><a href=\"deleteall.php?uid=$country\">Delete</a></td></tr>"; 

    } 
} 

?> 
       </tbody> 
      </table> 
     </div> 
    </body> 
</html> 

這裏的解決方案.PHP

<?php 

    if(isset($_GET['delete_btn'])) { 

     $country = addslashes($_GET['delete_btn']); 
     $q = "DELETE FROM countries WHERE country = '$country'"; 

     require 'pdo.php'; 
     $sql = connect()->prepare($q); 
     $sql->execute(); 
    } 

?> 
0

希望它會幫助你,你可以添加刪除鏈接,然後去這個網頁它會刪除

<a href="http://localhost/edit.php?uid=22" onclick="return confirm('Are you sure?')">Edit</a> 
<a href="http://localhost/edit.php?uid=22" onclick="return confirm('Are you sure?')">Delete</a> 

<?php 

require 'pdo.php'; 
$connect = connect(); 

?> 


<!DOCTYPE html> 
<html> 
<head> 
    <link href="style.css" type="text/css" rel="stylesheet"/> 
</head> 
<body> 
<div class="wrapper"> 

    <table class="table"> 

     <thead> 
     <tr> 
      <th>Country</th> 
      <th>City</th> 
      <th>Image</th> 
     </tr> 
     <form action="deleteall.php" method="POST"> 
      <?php 
      $sql = connect()->prepare("SELECT * FROM countries ORDER BY country"); 
      $sql->execute(); 
      while ($result = $sql->fetch(PDO::FETCH_ASSOC)) { 

       if (!empty($result['image'])) { 
        $content_image = '<td><img src="images/' . $result['image'] . '"/></td>'; 
       } else { 
        $content_image = '<td>-</td>'; 
       } 
       ?> 

       <tr> 
        <td> <?= $result['country'] ?></td> 
        <td><?= $result['city'] ?></td> 
        <?= $content_image ?> 
        <td><a href="edit.php?uid=<?= $result['country'] ?>">Edit</a></td> 
        <td><a href="deleteall.php?uid=<?= $result['country'] ?>" onclick="return confirm('Are you sure?')">Delete</a> 
        </td> 
       </tr> 

      <?php } 

      ?> 

     </form> 
     </thead> 
    </table> 
</div> 
</body> 
</html> 

那麼這就是你deleteall.php

<?php 
$uid = trim($_GET['uid']); 
if(isset($uid)) { 
    $sql = "DELETE FROM countries WHERE country = ?"; 
    $q = connect()->prepare($sql); 

    $response = $q->execute(array($uid)); 
} 
?> 
+0

感謝它幫助了我很多! – Csabikaaaa

0

對於形式,我建議有一個按鈕,作爲功能,這樣你不小心丟失所有數據時頁面加載,或類似的東西。這只是三行代碼。

<form action="deleteall.php" method="POST"> 
    <input type="hidden" value="true" name="cameFromForm"> 
    <button type="submit" name="confirmButton">Delete All</button> 
</form> 

當點擊該按鈕時,那麼它會觸發deleteall.php的入口點,然後取輸入,並從表中刪除所有數據。

<?php 

    if(isset($_POST["cameFromForm"]) && $_POST["cameFromForm"] == "true") { 
    // This makes sure that the form actually sent it. 
     try { 

      $tableName = "Insert the name of your table here"; 

      $mysqli = new mysqli(
       'databaseHost', 
       'databaseUser', 
       'databasePassword', 
       'databaseName'); 

      if (mysqli_connect_errno()) { 
       printf("Connect failed: %s\n", mysqli_connect_error()); 
       exit(); 
      } 

      $stmt = $mysqli->prepare("DELETE FROM ?"); 
      $stmt->bind_param('s', $tableName); 
      $stmt->execute(); 

      $stmt->close(); 
      $mysqli->close(); 

      header('Location: http://afterDeltePage.com'); 

     } 
     catch (Exception $e) { 
      print($e); 
      die(); 
     } 
    } 
    else { 
     header('Location: http://notAuthorisedPage.com'); 
    } 

?> 

完成該過程後,您應該將用戶發送到帶有UI的頁面。嘗試分離顯示用戶和流程所需的內容。另外,如果這是公開的,你需要確保deleteall不被任何人通過URL訪問。

爲此,您需要將文件移出wwwroot(通常爲public_html)。有關如何做到這一點的各種話題。

PHP Forms信息和PHP MySQLi信息。

+0

感謝它的幫助我很多! – Csabikaaaa

+0

我很高興遇到這種情況!請記住編輯您的問題,以確保人們可以在將來查看它並瞭解甚至解決他們自己的問題! – Will

+0

Okey,謝謝,即時通訊新的在stackoverflow,所以我嘗試! – Csabikaaaa