我正在尝试将数据从表单存储到数据库中,已经在您的网站和其他网站上搜索了所有答案和代码,但是它们都不起作用。
它正在连接到数据库,确定正常,但是在提交表单时不断收到错误消息。
谢谢
我的表单是
<html><head>
<link rel="stylesheet" href="form.css" type="text/css">
<meta charset="utf-8">
</head>
<body>
<h1>A small example page to insert some data in to the MySQL database using
PHP</h1>
<form action="insert.php" method="post">
Firstname: <input type="text" name="firstname" /><br><br>
Lastname: <input type="text" name="lastname" /><br><br>
<input type="submit" />
</form>
</body>
</html>
我的PHP代码是
<?php
$servername = "server";
$username = "username";
$password = "xxxx";
$database = "xxx_com";
$conn = mysqli_connect($servername, $username, $password, $database);
{
$first_name = $_POST['firstname'];
$last_name = $_POST['lastname'];
$dbh->query = "INSERT INTO nametable (firstname, lastname)
VALUES ('$_POST[firstname]', '$_POST[lastname]')";
}
if (!mysqli_query($user_info, $connect)) {
die('Error: ' . mysqli_error());
}
echo “Your information was added to the database.”;
mysqli_close($connect);
?>
答案 0 :(得分:0)
您使用的html表单
Firstname: <input type="text" name="fname" /><br><br>
Lastname: <input type="text" name="lname" /><br><br>
但是您的PHP来自
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
它应该使用相同的名称。喜欢
$first_name = $_POST['fname'];
$last_name = $_POST['lname'];
正如人们评论所提到的,请学习如何避免SQL注入问题