【发布时间】:2017-09-08 06:32:03
【问题描述】:
您好,我创建了一个简单的插入数据库功能,学生可以在其中输入他/她的信息并将他们的文档上传到数据库中,但是没有任何内容被插入到数据库中,只有上传的文件出现在上传文件中,而不是出现在数据库。
html代码:
<form accept-charset="utf-8" action="page.php" enctype="multipart/form-data" id="wb_Form1" name="form" method="post" data-toggle="validator" role="form" >
<label>Student Name :</label>
<input type="text" name="stu_name" id="name" required="required" placeholder="Please Enter Name"/><br /><br />
<label>Student Email :</label>
<input type="email" name="stu_email" id="email" required="required" placeholder="john123@gmail.com"/><br/><br />
<label>Student City :</label>
<select name="stu_city">
<option value="works">test</option>
<option value="works3">test</option>
</select>
<label>Image:</label><input type="file" name="image">
<input type="submit" value=" Submit " name="submit"/><br />
</form>
php代码:
<?php
if(isset($_POST["submit"])){
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$fileinfo=PATHINFO($_FILES["image"]["name"]);
$newFilename=$fileinfo['filename'] ."_". time() . "." . $fileinfo['extension'];
move_uploaded_file($_FILES["image"]["tmp_name"],"upload/" . $newFilename);
$location="upload/" . $newFilename;
$sql = "INSERT INTO students (student_name, student_email, student_city,img_location)
VALUES ('".$_POST["stu_name"]."','".$_POST["stu_email"]."','".$_POST["stu_city"]."','".$_POST["location"]."')";
header('location:index.php');
}
?>
感谢您的帮助
【问题讨论】:
-
请使用准备好的语句。这真的很有帮助。 Mysqli 支持它。你目前拥有的东西已经超出了风险,所以它不能拯救任何东西的事实是一种祝福。另外它不起作用的原因仅仅是因为您从未执行过查询