尝试将文件上传到数据库

时间:2017-03-05 16:23:37

标签: php mysql

尝试提交表单时,收到以下错误消息:

Notice: Undefined index: application_results in C:\xampp\htdocs\cas\insert.php 

这是我的表单php代码:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>CAS Application</title>
  <meta charset="utf-8">
 <meta name="viewport" content="width=device-width, initial-scale=1">
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap        /3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body style="background-color:#FF9933">
    <div class="container" >
        <form action="insert.php" method="post" class="form-horizontal"    role="form" enctype="multipart/form-data">
            <h2 style="text-align:center">Application Form</h2>

            </div>
            <div class="form-group">
                <label for="surname" class="col-sm-3 control-label">Surname</label>
                <div class="col-sm-9">
                    <input type="text" name="surname" id="surname" placeholder="Surname" class="form-control" autofocus>
                </div>
            </div>

            <div class="form-group">
                <label for="firstName" class="col-sm-3 control-label">First Name</label>
                <div class="col-sm-9">
                    <input type="text" name="first_name" id="first_name" placeholder="First Name" class="form-control" autofocus>
                </div>
            </div>

            <div class="form-group">
                <label for="email" class="col-sm-3 control-label">Email</label>
                <div class="col-sm-9">
                    <input type="text" id="email" placeholder="Email" class="form-control" name="email">
                </div>
            </div>

            <div class="col-sm-offset-3">
                <h2>Application Results</h2>

                <label for="application_results">Please upload your application results here:</label>

                    <input type="file" name="application_results" ><br>

            </div>

            <div class="form-group">
                <div class="col-sm-6 col-sm-offset-3">
                    <button type="submit" class="btn btn-primary btn-block" name="submit">Submit</button>
                </div>
            </div>

        </form> 

    </div>
</body>
</html>
<?php

 $con = mysql_connect("localhost","root","");
 mysql_select_db("cas",$con);

 if(isset($_POST['submit'])) {
     $file = rand(1000,100000). "-".$_FILES['application_results']['name'];
     $file_loc = $_FILES['application_results']['tmp_name'];

     $folder="application_results";

     if(move_uploaded_file($file)) {
      $sql="INSERT INTO applications (application_results) VALUES    ($application_results)";
      mysql_query($sql);
}
}

?>

这是我的插入代码:

<?php

    $link = mysqli_connect("localhost", "root", "", "cas");

    if($link === false){

        die("ERROR: Could not connect. " . mysqli_connect_error());

     }


    $surname = mysqli_real_escape_string($link, $_REQUEST['surname']);

    $first_name = mysqli_real_escape_string($link, $_REQUEST['first_name']);


    $email = mysqli_real_escape_string($link, $_REQUEST['email']);

    $application_results = mysqli_real_escape_string($link, $_REQUEST['application_results']);


// attempt insert query execution

$sql = "INSERT INTO applications ( surname, first_name, 
 email, application_results ) 
VALUES ('$surname', '$first_name', '$email', $application_results )";

if(mysqli_query($link, $sql)){

    echo "Your application has been submitted.";

} else{

    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);

}



// close connection

mysqli_close($link);

?>

我实际想要完成的是在插入调用application.php页面后,申请人的页面详细信息必须与上传的文件一起显示以供阅读。我怀疑大部分上传的文件都是pdf格式。

0 个答案:

没有答案