简体   繁体   English

为什么会出现此错误?:“字段列表”中的未知列“名字”

[英]Why am getting this error?: Unknown column 'firstname' in 'field list'

  if(isset($_POST["submit"]))
  {

    // Details for inserting into the database

        $id = htmlentities($_POST["id"]);
        $firstname = htmlspecialchars($_POST["firstname"]);
        $lastname = htmlspecialchars($_POST["lastname"]);
        $username = htmlspecialchars($_POST["username"]);
        $password = htmlspecialchars($_POST["password"]);

    // Dealing with inserting 

        $query = "INSERT INTO `myDatabaseForAll`.`users` (`id`, `firstname`, `lastname`, `username`, `password`)
                 VALUES (NULL, $firstname, $lastname,$username,$password)";
        $result = mysqli_query($connection,$query);


        if(!$result) {
            die('There were some errors '.mysqli_error($connection));
        } else {
            redirect_to('index.php');
        }

}
$query = "INSERT INTO `myDatabaseForAll`.`users` (`id`, `firstname`, `lastname`, `username`, `password`)
         VALUES (NULL, $firstname, $lastname,$username,$password)";

you need single quote around text feilds in sql queries 您需要在SQL查询中的文本字段周围加上单引号

change above query to 将以上查询更改为

$query = "INSERT INTO `myDatabaseForAll`.`users` (`id`, `firstname`, `lastname`, `username`, `password`)
         VALUES (NULL, '$firstname', '$lastname','$username','$password')";

This error can be occurred due to mismatch of table column name with the name you have given in the query. 由于表列名称与您在查询中给定的名称不匹配,可能会发生此错误。 If it is not the case please try the below code. 如果不是这种情况,请尝试以下代码。

$query = "INSERT INTO `myDatabaseForAll`.`users` (id, firstname, lastname, username, password)
         VALUES (NULL, '".$firstname."', '".$lastname."','".$username."','".$password."')";

In your query you are concatenating some string parts with php variables. 在查询中,您将一些字符串部分与php变量连接在一起。 So this concatenation should be done in the above way using the concatenating operator. 因此,应使用连接运算符以上述方式完成此连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM