简体   繁体   中英

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)

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. You can either choose to edit your MySQL settings or add this code to your database class:

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. 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 . 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. 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. But id field is an int. Your first create function, the one without join, worked because the id field is left out. Now to fix the second create function after the line $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(); 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. If it is empty, you do an 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. 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. 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:

explicitly assign NULL; explicitly assign 0

public function create() { global $database; // USE '0' for your auto_increment not null filed if your filed need to sanitize. $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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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