嗨,我已经创建了一个简单的插入到数据库的功能和上传自己的文件到数据库中,但没有被插入到数据库中则只会出现上传文件在上传文件上但不在数据库上。无法将信息插入到数据库所在的学生进入他/她的信息
的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="[email protected]"/><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支持它。你现在拥有的是超出风险的,所以它不保存任何东西是一件幸事。此外,它不工作的原因很简单,因为你永远不执行查询 – Akintunde007