PHP如果在POST中只加载第一个if

时间:2015-03-25 21:11:20

标签: php mysqli

我有这段代码:

    if(isset($_POST['remove'])){

                      $con = mysqli_connect(".","","","");

                $q = mysqli_query($con,"UPDATE members SET picture = '' WHERE username = '".$_SESSION['username']."'");

        header( "refresh:2;url=settings.php" );
        echo "<div class='notemarg'>Profile Picture has been removed. Refreshing page within 3 seconds...</div>";
}

它正在运作,但我希望它能做到这样的事情

    if(isset($_POST['remove'])){

                      $con = mysqli_connect("","","","");
        while($row = mysqli_fetch_assoc($q)){
                                    if($row['picture'] == ""){
                                echo "<div class='notemarg'> No pictures to delete</div>";
        } else {
                $q = mysqli_query($con,"UPDATE members SET picture = '' WHERE username = '".$_SESSION['username']."'");

        header( "refresh:2;url=settings.php" );
        echo "<div class='notemarg'>Profile Picture has been removed. Refreshing page within 3 seconds...</div>";
        }
        }
}

这意味着只有当有任何图片时才会删除数据库中的图片..如果没有,那么它将显示该消息&#34;没有要删除的图片&#34; ...但它不起作用..它仍然显示错误消息,即使数据库中没有空行也没有图片,所以它不会删除行中的信息... 哪里有问题? 顺便说一句:第一个代码工作得很好......即使数据库中没有任何东西也能正常工作,所以有一点意义上没有删除&#34;个人资料图片。&#34;正在展示......

2 个答案:

答案 0 :(得分:0)

while循环之前,您需要运行此SELECT

$q = mysqli_query($con,"SELECT IFNULL(picture,'') 
                        FROM members 
                        WHERE username = '".$_SESSION['username']."'");

这将返回您的图片字段值。

答案 1 :(得分:0)

请改为尝试:

  $con = mysqli_connect("","","","");
 if(isset($_POST['remove'])){

   $q = mysqli_query($con, "SELECT picutre FROM members where username = '". $_SESSION['username']. "'");          
   $row = mysqli_fetch_assoc($q);
   if( empty($row['picture'])){
      echo "<div class='notemarg'> No pictures to delete</div>";
   } 
   else {
     $q = mysqli_query($con,"UPDATE members SET picture = '' WHERE username = '".$_SESSION['username']."'");

     header( "refresh:2;url=settings.php" );
     echo "<div class='notemarg'>Profile Picture has been removed. Refreshing page within 3 seconds...</div>";
    }
 }