Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php MySQL错误:未知列';d';在';字段列表';_Php_Mysql_Insert - Fatal编程技术网

Php MySQL错误:未知列';d';在';字段列表';

Php MySQL错误:未知列';d';在';字段列表';,php,mysql,insert,Php,Mysql,Insert,我正在学习W3Schools的MySQL教程,似乎有些代码已经过时了,或者我遗漏了一些非常愚蠢的东西。在尝试将信息传递到数据库时,我遇到以下错误: 错误:“字段列表”中的未知列“d” 我试图通过表单提交传递信息,然后链接到此页面,在该页面中,它获取用户输入的信息,以创建新的数据库条目 这是表单提交的代码 <html> <body> <form action="3ainsert.php" method="post"> Firstname: <input

我正在学习W3Schools的MySQL教程,似乎有些代码已经过时了,或者我遗漏了一些非常愚蠢的东西。在尝试将信息传递到数据库时,我遇到以下错误:

错误:“字段列表”中的未知列“d”

我试图通过表单提交传递信息,然后链接到此页面,在该页面中,它获取用户输入的信息,以创建新的数据库条目

这是表单提交的代码

<html>
<body>

<form action="3ainsert.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>

</body>
</html>

名字:
姓氏:
年龄:
以下是另一页的代码:

<?php
$con=mysqli_connect();
// Check connection
if (mysqli_connect_errno())
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// escape variables for security
$firstname = mysqli_real_escape_string($con, $_POST['firstname']);
$lastname = mysqli_real_escape_string($con, $_POST['lastname']);
$age = mysqli_real_escape_string($con, $_POST['age']);

$sql="INSERT INTO persons (FirstName, LastName, Age)
VALUES ($firstname, $lastname, $age)";

if (!mysqli_query($con,$sql))
{
  die('Error: ' . mysqli_error($con));
}
echo "1 record added";

mysqli_close($con);
?>

您的查询没有将值括在引号中:

$sql="INSERT INTO persons (FirstName, LastName, Age)
VALUES ('$firstname', '$lastname', '$age')";

但是请注意,通常使用预处理语句比直接插入查询更可取。

您缺少了其中一些“'”哇。可以我觉得自己像个笨蛋。应该有人更新,这样其他人就不会落入同样的陷阱。谢谢当网站允许我回答时,我会接受你的回答。@user3476944 Ohhh不让我们从W3TskTskwhy开始吗?对于这类事情来说,这是一个糟糕的网站吗?有没有更好的地方可以让我学习?@Dagoth是的,它的名声不好,因为里面散布着糟糕的信息。如果要使用php,请使用。到mysql的东西。当手册没有帮助时,直接来到StackOverflow并疯狂地搜索——答案可能已经在这里了,这只是正确搜索的问题。