1 回答
TA贡献1828条经验 获得超4个赞
问题是您实际上并没有运行查询。您刚刚将查询字符串分配给变量,因此它不会在MySQL中执行。
您的代码容易受到SQL注入的攻击,因此我提出了一个解决方案:
<?php
$userinput = $_POST['stuff'];
$servername = "localhost";
$username = "root";
$password = "";
$database = "testing";
$conn = new mysqli($servername, $username, $password, $database);
if ($conn->connect_error)
{
die("connection failed: " . $conn->connect_error);
}
else
{
echo "Connected successfully ";
echo $userinput;
$sql = "INSERT INTO `entries` (`input`) VALUES (?)";
if ($stmt = $conn->prepare($sql)) { // Prepare statement
$stmt->bind_param("s", $userinput); //Bind the string (s), with the content from $userinput to the statement marker (?)
$stmt->execute(); // Run (execute) the query
$stmt->close(); //clean up
}
此代码应该有效,并且还可以保护您免受SQL注入的影响。
- 1 回答
- 0 关注
- 162 浏览
添加回答
举报
