为什么会收到此错误?:'字段列表'中的未知列'firstname'

时间:2015-06-22 04:27:58

标签: php database mysqli

  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');
        }

}

2 个答案:

答案 0 :(得分:0)

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

你需要在sql查询中围绕文本字段单引号

将上述查询更改为

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

答案 1 :(得分:0)

由于表列名与您在查询中指定的名称不匹配,可能会发生此错误。如果不是这种情况,请尝试以下代码。

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

在您的查询中,您将一些字符串部分与php变量连接起来。因此,应使用连接运算符以上述方式完成此连接。