php数据未使用XAMPP存储在mySQL数据库中

时间:2016-03-03 22:21:49

标签: php mysqli xampp

我有这个代码,我使用的是最新版本的XAMPP:

filename:store.html

<!DOCTYPE html>

<html>
<body>

<form action="store.php" method="post">
User input: <input type="text" name="userinput"><br>
<input type="submit">
</form>

</body>
</html>

filename:store.php

<?php

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "database";


// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);
 } 
 echo "Connected successfully";

$input = $_POST["userinput"];


$sql = "INSERT INTO table_1 (s_num)
VALUES ('$input')";


 ?>

无论我做什么,都没有数据添加到数据库中。请帮忙。谢谢。

2 个答案:

答案 0 :(得分:2)

这里的问题是你从未执行过查询。

$sql = mysqli_query($conn,"INSERT INTO table_1 (s_num)
VALUES ('$input')");

if(!sql){

   echo "Error: " . mysqli_error($conn);

}

else{

   echo "Success";

}

参考:

如果用户输入(除了您自己)参与其中,您的现有代码对SQL injection开放。

使用prepared statementsPDO with prepared statements它们更安全

另外,如果您在自己的计算机上运行此操作,请确保您以http://localhost/file.php而非file:///file.php进行访问。

答案 1 :(得分:1)

您没有执行sql insertcode。 在这一行之后:

$sql = "INSERT INTO table_1 (s_num)
VALUES ('$input')";

添加以下行:

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
相关问题