【问题标题】:Can't Insert information into database无法将信息插入数据库
【发布时间】: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');

}

?>

file hierarchy:

database structure

感谢您的帮助

【问题讨论】:

  • 请使用准备好的语句。这真的很有帮助。 Mysqli 支持它。你目前拥有的东西已经超出了风险,所以它不能拯救任何东西的事实是一种祝福。另外它不起作用的原因仅仅是因为您从未执行过查询

标签: php mysqli upload insert


【解决方案1】:

您没有执行查询。您刚刚进行了查询。

注意:比创建自己的查询更好的方法是“准备”语句。这将使您免于 SQL 注入和查询的意外中断。

看看这个:

https://www.w3schools.com/php/php_mysql_prepared_statements.asp

【讨论】:

    【解决方案2】:

    您需要执行查询。你所拥有的基本上是一个字符串。根据我的评论,您需要使用准备好的语句。你的代码是有风险的。此外,您为文件位置插入了错误的变量。使用 $location 变量但要使其插入,请添加此

    $conn->query($sql);
    

    使用准备好的语句,这样做

    // 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
    }
    

    【讨论】:

    • 感谢您的评论,我将确保使用准备好的语句...我添加了查询并插入了数据,但上传文件未插入到数据库中
    • 非常感谢它的工作......当然我将不得不使用准备好的陈述非常感谢你
    • 好的没问题..我只是想问一下“ssss”是什么
    • “ssss”表示要绑定四个字符串变量。 i 捐赠它是整数,d 捐赠它是 double
    【解决方案3】:

    试试这个$conn = mysqli_connect($servername, $username, $password, $dbname);你的连接,在你的$sql是mysqli_query($conn, $sql);之后,你现在可以做一些重定向。

    【讨论】:

    • 你不需要这样做。您可以以过程方式或面向对象的方式进行。 OP选择了后者。唯一需要的是执行查询。并修复安全漏洞
    • 好吧,我同意你的看法。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-19
    • 1970-01-01
    • 2013-10-14
    • 2012-04-13
    相关资源
    最近更新 更多