2016-09-21 69 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->准备大声笑......我想现在睡觉的时候了!非常感谢你!一点点的错误都让编程生活变得很悲惨 –