无法使DELETE功能正常工作

时间:2014-09-21 01:46:46

标签: php

好的,开始...我有一个页面,将显示我的数据库中的缩略图。 我希望用户能够从下拉菜单中选择图像库,然后图像将显示在其下方,并且只显示该图库中的图像。我所有这一部分工作正常,我现在唯一的问题是,当我选择图片(通过它下面的复选框)并单击删除时,DELETE []函数不起作用...实际上' if语句'具有unlink()函数甚至不会运行。我在这里遗漏了一些东西。我正在使用switch语句在选项菜单中循环。这是导致问题的switch语句吗?我有所有案例选项和默认设置,这里没有显示。

任何帮助将不胜感激,谢谢。我接受对我的代码的所有批评。(sql注入和排序将在稍后处理)我远离专业人士。 :)

FORM
<form action="delete_pics.php" method="post" enctype="multipart/form-data">
<select name="gallerySelection" class="btnExample">
<option value="" class="btnExample">Select A Gallery</option>
<option value="mainGallery" class="btnExample">Main Gallery</option>
<option value="theBikes" class="btnExample">The Bikes</option>
<option value="thePits" class="btnExample">The Pits</option>
<option value="theAction" class="btnExample">The Action</option>
<option value="theBuilds" class="btnExample">The Builds</option>
</select>
</td></tr><tr><td>
<input type='submit' name='next' value='Show Selection' class='btnExample'>
</form>

然后这是带有代码的Switch语句:

if(isset($_POST['next']))
   {
   if(isset($_POST['gallerySelection']))
      {
      $selection = $_POST['gallerySelection'];
      }

     switch($selection)
      {
        case "theBikes":    
         {      
           echo"<h4>The Bikes</h4><p><form action='delete_pics.php' method='POST'>";
           $pics = mysql_query("SELECT p_id, theBikes, theBikes_thumb, descr FROM gallery WHERE theBikes        !='' ORDER by p_id DESC");
   if(@mysql_num_rows($pics) >0)
         {
           echo"<table border='0'>";
           $count=0;
             while($row3=mysql_fetch_array($pics)){

            if(isset($_POST['delete'])) /////////////This is where the failed code starts
             {  
                $delete = $_POST['delete'];
                   foreach($delete as $deleted)
                       {
                          $pics_delete = mysql_query("SELECT p_id, theBikes, theBikes_thumb, descr FROM gallery WHERE    p_id=$deleted");
                          $row4=mysql_fetch_row($pics_delete);
                          unlink($row4[0]);
                          unlink($row4[1]);
                          mysql_query("DELETE FROM gallery WHERE p_id=$deleted");
                          printf("<script>location.href='picture_delete.php'</script>");
                        }
              }     ////////////////////////////////////This is where the failed code ends
          else
              {
                 $delete = array();
              }
          if($count==0)
              {
                 echo"<tr><td align='center' width='150'>";
              }
          else
              {
                 echo"<td align='center' width='150'>";
              }
       echo"<div><img src='$row3[theBikes_thumb]' style='border:0px;' width='60' height='75'>   <p>$row3[descr]</div>";
       echo "<input type='checkbox' name='delete[]' value='{$row3['p_id']}'>"; //////////This is the checkbox to check the picture to be deleted

     if($count==5)
        {
           echo"</td></tr>";
           $count=0;
        }
    else
        {
           echo"<td>";
           $count++;
        }
  }  /////////////End of whle loop
       $cells_left = 5 - $count;
    if( $cells_left > 0 ) 
        {
           $i = 0;
              while( $i <= $cells_left ) 
                 {
                    echo '<td></td>';
                    $i++;
                 }
                    echo '</tr>';
         }
               echo '</table>';
        } 
    else
        {
           echo "No images in the database.";
        }

echo"<div><input type='submit' name='submit' value='DELETE PICTURE' class='btnExample'>

</form>
</div>
";
break;
}

感谢您的帮助! 祝你有美好的一天。 :)

1 个答案:

答案 0 :(得分:0)

您的删除表单(PHP代码第12行)未发送&#39; next&#39;输入字段。当没有选择图片时,POST请求仅传输此数组:

Array ( [submit] => DELETE PICTURE )

应该是:

Array ( [gallerySelection] => theBikes [next] => Show Selection [submit] => DELETE PICTURE )

因此,删除表单永远不会输入您的初始IF语句,并忽略删除请求。

以下是建议:将整个删除过程移出开关案例,并添加令牌检查以防止表单插入。理想情况下,图像删除过程应在您的表单处理检查后立即发生(IF语句寻找$ _POST [&#39; next&#39;]的声明)。还为$ _POST添加了隐藏的输入字段[&#39; next&#39;]。

示例

if (isset ( $_POST['next'] )) { // Your current code for deletion doesn't make it past this check.

    // Check if deletion has been requested
    if (isset ( $_POST['delete'] )) {
        // Deletion process

    }

    if (isset ( $_POST['gallerySelection'] )) {
        $selection = $_POST['gallerySelection'];
    }

    switch ($selection) {
        // Fill in the blanks ...
    }
}

编辑:您的示例也缺少两个闭括号或&#34;}}&#34;。