if语句重置表单数据变量

时间:2016-08-22 10:07:19

标签: php

如果用户上传了错误的文件,那么如果语句将$name重置为空白。以下代码有什么问题?

if(isset($_POST['submit'])){
        $conn= new mysqli('localhost','root','','dilip');
        if(!$conn){
               die("Not connect".mysqli_error);
        }
        $stm = $conn->prepare("Insert into comment (Name, email, Message, Image) values (?, ?, ?, ?)");
        $stm->bind_param('ssss',$name,$email,$message,$image);


        $name = $_POST['name'];
        $message = $_POST['readerInput'];
        $email = $_POST['email'];
        $image='upload/comment/default.jpg';
        $time = date("s");


        //if(isset($_FILES['image'])){
        if(is_uploaded_file($_FILES['image']['tmp_name'])){
                $tmp_image = $_FILES['image']['size'];
                $finfo = new finfo(FILEINFO_MIME_TYPE);
                $file = $finfo->file($_FILES['image']['tmp_name']); //This line checks MIME Type of uploaded image

                if($file!=='image/jpeg' && $file !=='image/gif' || $tmp_image > 1024*1024*2){ //1024*1024*2 = 2MB
                    echo "Upload your Profile Image in jpg/gif format and lower than 2mb. Otherwise continue without Image.";   
                }

                else{

                    $photo = move_uploaded_file($_FILES['image']['tmp_name'],'upload/comment/'.$name.$time.'.jpg');
                    $image = 'upload/comment/'.$name.$time.'.jpg';
                    echo '<script> alert("Your file is accepted.")</script>';
                    $stm->execute();

                    $message = 'Your Comment';
                    $name = 'Your Name';
                    $email = 'Your eMail';

                }
    }else{
                $stm->execute();
                $message = 'Your Comment';
                $name = 'Your Name';
                $email = 'Your eMail';
    }
}

1 个答案:

答案 0 :(得分:1)

请尝试使用以下代码段。您正在准备变量初始化和图像上载代码之前的插入语句。我已将代码移到文件上传代码下面。

if(isset($_POST['submit'])){
        $flag = 0;
        $conn= new mysqli('localhost','root','','dilip');
        if(!$conn){
               die("Not connect".mysqli_error);
        }

        $name = $_POST['name'];
        $message = $_POST['readerInput'];
        $email = $_POST['email'];
        $image='upload/comment/default.jpg';
        $time = date("s");


        //if(isset($_FILES['image'])){
        if(is_uploaded_file($_FILES['image']['tmp_name'])){
                $tmp_image = $_FILES['image']['size'];
                $finfo = new finfo(FILEINFO_MIME_TYPE);
                $file = $finfo->file($_FILES['image']['tmp_name']); //This line checks MIME Type of uploaded image

                if($file!=='image/jpeg' && $file !=='image/gif' || $tmp_image > 1024*1024*2){ //1024*1024*2 = 2MB
                    echo "Upload your Profile Image in jpg/gif format and lower than 2mb. Otherwise continue without Image.";   
                    $flag = 1;
                }else{
                    $photo = move_uploaded_file($_FILES['image']['tmp_name'],'upload/comment/'.$name.$time.'.jpg');
                    $image = 'upload/comment/'.$name.$time.'.jpg';
                    echo '<script> alert("Your file is accepted.")</script>';
                }
    }

    $stm = $conn->prepare("Insert into comment (Name, email, Message, Image) values (?, ?, ?, ?)");
    $stm->bind_param($name,$email,$message,$image);
    if($flag==0){
        $stm->execute();
    }    
}