我的陈述有什么问题

时间:2015-02-25 13:35:50

标签: php forms file-upload

当我提交表单时,它得到并且错误:出现错误消息“帖子未添加”。然后它将进入循环并继续打印。

你能看到错误:

这真令人讨厌,每次都让我的新鲜感十分鲜艳。

  <?php 
              $q = "SELECT user_id FROM users WHERE id = $id";
              $r = mysqli_query($dbc, $q);

              while($vote_list = mysqli_fetch_assoc($r)) { ?>

      <?php 

                if(isset($_POST['submitted']) == 1) {

                $ds = DIRECTORY_SEPARATOR;  //1

                $storeFolder = '../uploads';   //2

                $text = mysqli_real_escape_string($dbc, $_POST['text']);
                $ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
                $uploadfile_type = $_FILES['file']['type'];
                $newname = time();
                $rand = rand(100,999);
                $name = $newname.$rand.'.'.$ext;
                //if($_POST['id'] != '')


                if(($uploadfile_type !='image/jpeg') && ($uploadfile_type !='image/png')){  
                   echo "Post was not added!";  

                }else{
                    echo "Post was added!";             
                    $q = "INSERT INTO votes (user_id, text, image) VALUES ('$vote_list[user_id]', '$text', '$name')";

                }
                $r = mysqli_query($dbc, $q);



                if (!empty($_FILES)) {

                        $tempFile = $_FILES['file']['tmp_name'];          //3             

                        $targetPath = dirname( __FILE__ ) . $ds. $storeFolder . $ds;  //4

                        $targetFile =  $targetPath. $name;  //5

                        move_uploaded_file($tempFile,$targetFile); //6

                }



            }

        ?>
  <?php } ?> 

1 个答案:

答案 0 :(得分:1)

请看以下几行:

}else{
    echo "Post was added!";             
    $q = "INSERT INTO votes (user_id, text, image) VALUES ('$vote_list[user_id]', '$text', '$name')";
}
$r = mysqli_query($dbc, $q);

你的mysqli_query应该在else语句中,如下所示:

}else{
    echo "Post was added!";             
    $q = "INSERT INTO votes (user_id, text, image) VALUES ('$vote_list[user_id]', '$text', '$name')";
    $r = mysqli_query($dbc, $q);
}

在你的代码中,$r的值每次都在循环中重置,因此它变成了无限循环。