简体   繁体   English

数据库查询失败:第1行的列'id'的整数值不正确:“

[英]Database query failed: Incorrect integer value: '' for column 'id' at row 1

The code---- 编码 - -

public function create(){
global $database;
//this code works perfectly to insert the new user into my database.
$sql = "INSERT INTO users (";
$sql .= "username, password, first_name, last_name";
$sql .= ") VALUES ('";
$sql .= $database->escape_value($this->username) ."', '";
$sql .= $database->escape_value($this->password) ."', '";
$sql .= $database->escape_value($this->first_name) ."', '";
$sql .= $database->escape_value($this->last_name) ."')";
if($database->query($sql)){
    $this->id = $database->insert_id();
return true;
}else{
return false;
}       

public function create()
{
    global $database;
       //this code is to be universal for other database types but it is not working.
    $attributes = $this->attributes();
    $sql        = "INSERT INTO ".self::$table_name." (";
    $sql .= join(", ", array_keys($attributes));
    $sql .= ") VALUES ('";
    $sql .= join("', '", array_values($attributes));
    $sql .= "')";
    if ($database->query($sql)) {
        $this->id = $database->insert_id();

        return true;
    } else {
        return false;
    }
}

the problem - suppose to be generalizing the script so it can be used in any database structure. 问题-假设要泛化脚本,以便可以在任何数据库结构中使用它。 Its not working in my wamp. 它不能在我的沼泽中工作。

I think there may be an issue with magic quote . 我认为魔术报价可能存在问题。 Kindly use addslash php function for array_values($attributes) 请为array_values($attributes)使用addslash php函数

This is not a programming error from your end the problem here is that MySQL is not interpreting this action as valid due to its SQL_MODE being in STRICT mode. 这不是您的编程错误,这里的问题是,由于SQL_MODE处于STRICT模式,因此MySQL无法将其视为有效。 You can either choose to edit your MySQL settings or add this code to your database class: 您可以选择编辑MySQL设置,也可以将此代码添加到数据库类中:

private function open_connection() {
    $this->connection = mysqli_connect(DB_SERVER,DB_USER,DB_PASSWORD,DB_NAME);
    if (!$this->connection) {
      # code...
      die("Connection to database failed" . mysqli_errno($this->connection));
    }else{
      /// this is the part you should take note of i changed the sql mode here using this code 
      mysqli_query($this->connection, "SET GLOBAL sql_mode = ''");
    }

Your problem might be that you are using join("', '", array_values($attributes)) into the values you are trying to insert. 您的问题可能是您在要插入的值中使用了join("', '", array_values($attributes)) But i guess, your column types aren't all strings, so you are creating something like : 但是我想,您的列类型不是全部都是字符串,因此您正在创建类似以下内容的内容:

INSERT INTO mytable(id,column1)
VALUES('id','mycolumn1')

Problem is probably that your id column's type is int , that might be why you have Incorrect integer value . 问题可能是您id列的类型为int ,这可能就是为什么您使用Incorrect integer value So you should have something like : 所以你应该有这样的东西:

INSERT INTO mytable(id,column1)
VALUES(1,'mycolumn1')

Just guessing here, hope that helps. 只是在这里猜测,希望能有所帮助。

I run the same issue. 我遇到了同样的问题。 I wanted to post reply if anyone goes through the same problem. 如果有人遇到相同的问题,我想发表回复。 I think you were following lynda.com tutorial. 我认为您正在关注lynda.com教程。 As DCoder mentioned, id is auto increment value, and there is no way knowing what the value is, and moreover, join function made each property values-column values strings, in your case you get empty "" string. 如DCoder所述,id是自动递增值,无法知道该值是什么,此外,join函数使每个属性值-列值字符串成为字符串,在这种情况下,您会得到空的""字符串。 But id field is an int. 但是id字段是一个int。 Your first create function, the one without join, worked because the id field is left out. 您的第一个create函数(没有连接的函数)可以工作,因为id字段被忽略了。 Now to fix the second create function after the line $attributes = $this->attributes(); 现在修复在$attributes = $this->attributes();行之后的第二个create函数$attributes = $this->attributes(); add the following code 添加以下代码
if($attributes["id"]==""){ array_shift($attributes); } if($attributes["id"]==""){ array_shift($attributes); } after the line $attributes = $this->attributes(); if($attributes["id"]==""){ array_shift($attributes); }$attributes = $this->attributes(); Your attributes holds a list of properties; 您的属性包含属性列表; I am also making assumption that your code is the same as the tutorial I come across. 我还假设您的代码与我遇到的教程相同。 So you check if the id is empty. 因此,您检查ID是否为空。 If it is empty, you do an array_shift. 如果为空,则执行array_shift。 This remove the first attribute, that is id, and in the process the new attributes array will have no id, and then when you apply the join it will be the same as your first create function. 这将删除第一个属性(即id),并且在此过程中,新属性数组将没有id,然后在应用联接时,它将与第一个create函数相同。 This will fix the issue. 这样可以解决问题。 But I still would like to know how the instructor pass the tutorial without any issue. 但是我仍然想知道讲师如何顺利通过本教程。 Great instructor and great tutorial for OOP--I am talking about the lynda.com tutorial. 面向OOP的出色讲师和教程-我正在谈论lynda.com教程。 You could elaborate your question more to get more response or the right answer. 您可以详细阐述您的问题,以获得更多答复或正确答案。 Since I run the same issue made it easier for me. 由于我遇到了同样的问题,因此对我来说比较容易。

To let MySql generate sequence numbers for an AUTO_INCREMENT field you have some options: 要让MySql为AUTO_INCREMENT字段生成序列号,您可以选择以下选项:

explicitly assign NULL; 显式分配NULL; explicitly assign 0 明确分配0

public function create() { global $database; 公共功能create(){全局$ database; // USE '0' for your auto_increment not null filed if your filed need to sanitize. //如果要清除文件,请为auto_increment使用'0'而不是空文件。 $this->id = 0; $ this-> id = 0;

    $attributes = $this->sanitized_attributes();

  $sql = "INSERT INTO ".self::$table_name." (";
    $sql .= join(", ", array_keys($attributes));
  $sql .= ") VALUES ('";
    $sql .= join("', '", array_values($attributes));
    $sql .= "')";

  if($database->query($sql)) {
    $this->id = $database->insert_id();

    return true;
  } else {
    return false;
  }
}

This is how I figured to solve this problem 这就是我想解决这个问题的方式

public function create(){
    global $database;

    $attributes = $this->sanitized_attributes();
    $keys = array_keys($attributes);
    $values = array_values($attributes);
    $id = array_shift($values);
    $id = (int) $id;
    $sql  = "INSERT INTO ".static::$table_name." (";
    $sql .= join(", ", $keys);
    $sql .= ") VALUES ($id,'";
    $sql .= join("', '", $values);
    $sql .= "')";
    if($database->query($sql)) {
        $this->id = $database->insert_id();
        return true;
    } else {
        return false;
    }
}

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

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