dsubq24666 2017-09-08 06:32
浏览 41
已采纳

无法将信息插入数据库

Hi i have created a simple insert into database function where the student enters his/her information and upload their document into the database,but nothing gets inserted into the database only the files which were uploaded appears on the upload file but not on the database.

html code:

<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 Code:

<?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');

}

?>

file hierarchy:

database structure

Thank you for your help

  • 写回答

3条回答 默认 最新

  • duanhui5344 2017-09-08 06:36
    关注

    You need to execute the query. All you have is basically a string. As per my comment, you need to use prepared statements. Your code is risky. Also you are inserting the wrong variable for the file location. use the $location variable But to make it insert, add this

    $conn->query($sql);
    

    Using prepared statements, do this

    // prepare and bind
    $stmt = $conn->prepare("INSERT INTO students (student_name, student_email, student_city,img_location) VALUES (?, ?, ?,?)");//prepares the query. This returns true/false
    $stmt->bind_param("ssss", $_POST["stu_name"], $_POST["stu_email"], $_POST["stu_city"],$location);//bind the variables to placeholders to prevent SQL injections
    if($stmt->execute() === TRUE){//check if everything went well(executed!)
      echo 'Record Saved';
    } else {
      echo 'Error BEcause '.$stmt->error;//get error if something went wrong
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 CSS实现渐隐虚线边框
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题