创建登记表。输入类型文件在数据库中返回null值

时间:2015-08-29 16:31:06

标签: php html mysqli

在我的注册表单中,表单中输入的所有值都将保存在数据库中,但input type = file除外。它在数据库中返回一个空值,即“0”。即使通过选择桌面文件输入路径名称。我想知道我哪里出错,因为它返回0.这是我创建的数据库或我的代码的问题 以下是我的代码供参考:

php code

if(isset($_POST['submit']))
{
    $firstname = $_POST['firstname'];
    $lastname = $_POST['lastname'];
    $course = $_POST['course'];
    $semester = $_POST['semester'];
    $DOB = $_POST['DOB'];
    $gender = $_POST['sex'];

    $file = $_FILES["file"]["name"];
    $password = $_POST['password'];
    $email_id = $_POST['email_id'];
    if($stmt = $conn->prepare("INSERT INTO user_detail(firstname,lastname,course,semester,DOB,gender,registernumber,password,email_id) values(?,?,?,?,?,?,?,?,?)")) 
    {
          $stmt->bind_param('ssssssiss',
                            $firstname,$lastname,
                            $course,$semester,$DOB,
                            $gender,$file,$password,$email_id);
            $result = $stmt->execute();
            $stmt->close();
            echo $result;
    } else {
        echo "error with insertion";
    }
} 


$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);

if ((($_FILES["file"]["type"] == "image/gif")
  || ($_FILES["file"]["type"] == "image/jpeg")
  || ($_FILES["file"]["type"] == "image/jpg")
  || ($_FILES["file"]["type"] == "image/pjpeg")
  || ($_FILES["file"]["type"] == "image/x-png")
  || ($_FILES["file"]["type"] == "image/png"))
  && ($_FILES["file"]["size"] < 20000)
  && in_array($extension, $allowedExts)) 
{
    if ($_FILES["file"]["error"] > 0) 
    {
         echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    } else {
        echo "Upload: " . $_FILES["file"]["name"] . "<br>";
        echo "Type: " . $_FILES["file"]["type"] . "<br>";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
        echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

        if (file_exists("upload/" . $_FILES["file"]["name"])) 
        {
            echo $_FILES["file"]["name"] . " already exists. ";
        } else {
             move_uploaded_file($_FILES["file"]["tmp_name"],
                               "upload/" . $_FILES["file"]["name"]);

             echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
        }
    }
} else {
    echo "Invalid file";
}

<form action="" method="POST" enctype="multipart/form-data">
    <fieldset>
        <legend>Please enter the following details:</legend>

        <div>
            First Name
            <br>
            <input type="text" name="firstname" />
        </div>

        <div>
            Last Name
            <br>
                <input type="text" name="lastname" />
        </div>

        <div>
            Course
            <br>
            <input type="text" name="course">
            </div>

        <div>
            Semester :
            <br>
            <input type="text" name="semester" >
        </div>

        <div>
            Age:
            <br>
            <input type="date" name="DOB">
        </div>

        <div>
            Gender
            <br>
            <input type="radio" name="sex" value ="male" checked>Male
            <input type="radio" name="sex" value ="female">Female
        </div>
            <br>

        <div>
            Upload Choice of Identity Verification
            <br>
            <input type="file" name="file" id="file" size="80">

        </div>
            <br>

        <div>
            Password
            <br>
                <input type="password" name="password" />
        </div>

        <div>
            Email-ID
            <br>
                <input type="email" name="email_id" />
        </div> 

        <input type="submit" name="submit" value="Send" class="btn btn-default"/>
        </fieldset>    
    </form> 

1 个答案:

答案 0 :(得分:0)

您没有使用print_r($ _ FILES);检查文件内容是否来了。

使用以下代码: -

<pre>
if(isset($_POST['submit']))
{
    //echo ""; // here pre tag will come in double quotes.
    //print_r($_POST);  // show post data
    //print_r($_FILES);  // show files data
    //die; // die to stop execution. 
    $firstname = $_POST['firstname'];
    $lastname = $_POST['lastname'];
    $course = $_POST['course'];
    $semester = $_POST['semester'];
    $DOB = $_POST['DOB'];
    $gender = $_POST['sex'];
</code>
    $file = $_FILES["file"]["name"];
    $password = $_POST['password'];
    $email_id = $_POST['email_id'];
    if($stmt =$conn->prepare("INSERT INTO user_detail(firstname,lastname,course,semester,DOB,gender,registernumber,password,email_id) values(?,?,?,?,?,?,?,?,?)"))
    {
        $stmt->bind_param('ssssssiss',$firstname,$lastname,$course,$semester,$DOB,$gender,$file,$password,$email_id);
        $result = $stmt->execute();
        $stmt->close();
        echo $result;
    }
    else
    {
        echo "error with insertion";
    }
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);

if ((($_FILES["file"]["type"] == "image/gif")
        || ($_FILES["file"]["type"] == "image/jpeg")
        || ($_FILES["file"]["type"] == "image/jpg")
        || ($_FILES["file"]["type"] == "image/pjpeg")
        || ($_FILES["file"]["type"] == "image/x-png")
        || ($_FILES["file"]["type"] == "image/png"))
        && ($_FILES["file"]["size"] < 20000)
        && in_array($extension, $allowedExts))
{
    if ($_FILES["file"]["error"] > 0)
    {
        echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
    else
    {
        echo "Upload: " . $_FILES["file"]["name"] . "<br>";
        echo "Type: " . $_FILES["file"]["type"] . "<br>";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
        echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

        if (file_exists("upload/" . $_FILES["file"]["name"]))
        {
            echo $_FILES["file"]["name"] . " already exists. ";
        }
        else
        {
            move_uploaded_file($_FILES["file"]["tmp_name"],
                    "upload/" . $_FILES["file"]["name"]);
            echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
        }
    }
}
else
{
    echo "Invalid file";
}
}
</pre>