簡體   English   中英

數據庫查詢失敗:第1行的列'id'的整數值不正確:“

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

編碼 - -

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

問題-假設要泛化腳本,以便可以在任何數據庫結構中使用它。 它不能在我的沼澤中工作。

我認為魔術報價可能存在問題。 請為array_values($attributes)使用addslash php函數

這不是您的編程錯誤,這里的問題是,由於SQL_MODE處於STRICT模式,因此MySQL無法將其視為有效。 您可以選擇編輯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 = ''");
    }

您的問題可能是您在要插入的值中使用了join("', '", array_values($attributes)) 但是我想,您的列類型不是全部都是字符串,因此您正在創建類似以下內容的內容:

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

問題可能是您id列的類型為int ,這可能就是為什么您使用Incorrect integer value 所以你應該有這樣的東西:

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

只是在這里猜測,希望能有所幫助。

我遇到了同樣的問題。 如果有人遇到相同的問題,我想發表回復。 我認為您正在關注lynda.com教程。 如DCoder所述,id是自動遞增值,無法知道該值是什么,此外,join函數使每個屬性值-列值字符串成為字符串,在這種情況下,您會得到空的""字符串。 但是id字段是一個int。 您的第一個create函數(沒有連接的函數)可以工作,因為id字段被忽略了。 現在修復在$attributes = $this->attributes();行之后的第二個create函數$attributes = $this->attributes(); 添加以下代碼
if($attributes["id"]==""){ array_shift($attributes); } if($attributes["id"]==""){ array_shift($attributes); }$attributes = $this->attributes(); 您的屬性包含屬性列表; 我還假設您的代碼與我遇到的教程相同。 因此,您檢查ID是否為空。 如果為空,則執行array_shift。 這將刪除第一個屬性(即id),並且在此過程中,新屬性數組將沒有id,然后在應用聯接時,它將與第一個create函數相同。 這樣可以解決問題。 但是我仍然想知道講師如何順利通過本教程。 面向OOP的出色講師和教程-我正在談論lynda.com教程。 您可以詳細闡述您的問題,以獲得更多答復或正確答案。 由於我遇到了同樣的問題,因此對我來說比較容易。

要讓MySql為AUTO_INCREMENT字段生成序列號,您可以選擇以下選項:

顯式分配NULL; 明確分配0

公共功能create(){全局$ database; //如果要清除文件,請為auto_increment使用'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;
  }
}

這就是我想解決這個問題的方式

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