2016-09-21 38 views
1

我試圖刪除數據庫中的多個條目。我正在使用複選框來確定需要刪除的數據。一旦我勾選了複選框,它會向我發送我用來刪除它的主鍵的值。到目前爲止,我想出了這個使用PDO,PHP和MYSQL使用primaryKey和複選框刪除語句

當我嘗試刪除單個或多個數據,它直接到成功頁面,成功地計算刪除玩家的預期量的數量。但是當我顯示所有表格時沒有任何反應。

HTML/PHP代碼

  <?php 
       $self = htmlentities($_SERVER['PHP_SELF']); 
       echo "<form id='delete' method='POST' action='$self'>"; 
      ?> 
    <div class="content">     
      <h3>Delete Athlete</h3> 
      <hr> 
      <table> 
       <tr> 
        <th>Select Player</th><th>Photo</th><th>First Name</th><th>Last Name</th><th>Gender</th><th>Sport</th><th>Country</th><th>Population</th><th>Flag</th> 
       </tr> 
        <?php 
         foreach($showResult as $row) 
         { 
          echo("<tr><td><input type='checkbox' name='checkdelete[]' value='$row[athleteID]'>$row[athleteID]</td> 
          <td><img class='photo' src='files/$row[image]' alt='$row[lastName].jpg'/></td> 
          <td>$row[firstName]</td><td>$row[lastName]</td> 
          <td><img class='genderImg' src='files/$row[gender].png' alt='$row[gender].jpg'/></td> 
          <td>$row[sport]</td><td>$row[countryName]</td><td>$row[population]</td> 
          <td><img class = 'flag' src='files/flags/$row[flag]' alt='$row[flag].jpg'/></tr>"); 
         } 
        ?>      
      </table>  
    <br><input type="submit" name="deleteAth" value="Delete">    
    </div> 
</body> 

,我有此代碼爲PHP

<?php 

包括 'connect.inc.php';

try // select statement for the output page/delete page 
{ 
    $showJoinedTbl = "SELECT athleteID,image, firstName, lastName, gender, sport, 
    countryName, population, flag from athTbl join 
    counTbl on athTbl.countryID = counTbl.countryID ORDER by firstName"; 
    $showResult = $pdo->query($showJoinedTbl); 
} 
catch (PDOException $e) 
{ 
    $error = 'Select statement error'; 
    include 'error.html.php'; 
    exit(); 
} 
$dataCorrect = true; 
if (isset($_POST['deleteAth'])) 
{   
    try 
    { 
     $valueID = $_POST['checkdelete']; 
     $N = count ($valueID); 
     for ($i=0; $i; $i++) 
     { 
      $deleteQry = $db->prepare("DELETE from athTbl WHERE athleteID= ?"); 
      echo ("$valueID[$i]"); 

      //$stmt = $pdo->prepare($deleteQry); 
      $stmt->bindValue(1,$valueID[$i]); 
      $stmt->execute(); 
     } //when I try to delete single or multiple data, it goes straight to success page which successfully count the number of intended amount of deleted player. But nothing happens when I show all tables. 
    } 
    catch (PDOException $e) 
    { 
     $error = 'Error deleting data from athleteTbl'; 
     include 'error.html.php'; 
     exit(); 
    } 
    include 'DeleteSuccess.html.php';   // output screen if I deleted it successfully. 
} 
else 
{ 
    include 'Delete.html.php'; 
} 
?> 
+0

形式是HTML/PHP。顯示在Delete.html.php –

+0

中的顯示每個運動員ID的checkBox的$ showResult首先,執行select操作,然後處理刪除操作。因此,選定的結果仍然會包含要刪除的記錄,因爲刪除語句尚未運行。 – Shadow

回答

0

你在你的循環有一個錯誤:

... 
$N = count ($valueID); 
for ($i=0; $i; $i++) 
      ^^ 0 on the first iteration 
{ 
    ... 

你的循環會運行,而條件(中間值)true。您將$i設置爲0,其值爲false,因此您的循環永遠不會運行。

或者從manual

在每次迭代的開始,表達式2進行評估。如果 的計算結果爲TRUE,則循環將繼續並執行嵌套語句 。如果它評估爲FALSE,則循環的執行結束。

你可能想:

... 
$N = count ($valueID); 
for ($i=0; $i < $N; $i++) 
{ 
    ... 
+1

謝謝!已經設法讓它現在工作。除了你指出的,我用$ db而不是$ pdo->準備大聲笑......我想現在睡覺的時候了!非常感謝你!一點點的錯誤都讓編程生活變得很悲慘 –