我正在尝试制作一个非常基本的表格,将名字、姓氏和年龄添加到一个非常基本的数据库中,称为 Test。这是我的 HTML 代码:
<html>
<head>
</head>
<body>
<h1>Welcome!</h1>
<br>
<form action="insert.php" method="post">
Firstname: <input type="text" name="firstname">
Lastname: <input type="text" name="lastname">
Age: <input type="text" name="age">
<input type="submit">
</form>
<input type="submit">
</form>
</body>
</body>
</html>
这是我的 PHP 代码,隐藏了数据库名称、主机和登录信息:
<?php
$con=mysqli_connect("HOST","USER","PASS","DB");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql= "INSERT INTO Test ('First Name', 'Last Name', 'Age')
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
?>
这是我在表单中输入数据并提交时的错误。我被定向到此页面:
Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''First Name', 'Last Name', 'Age') VALUES ('Robert','Maxwell','18')' at line 1