2014-06-18 32 views
0

代碼----數據庫查詢失敗:不正確的整數值: '' 列 'ID' 在行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; 
    } 
} 

問題 - 假設是歸納腳本,以便它可以用於任何數據庫結構。它不在我的工作。

+3

顯示插入變量後的查詢。同時顯示實際的錯誤信息。 –

+0

數據庫查詢失敗:錯誤的整數值:''用於第1行的列'id' – user3752792

+0

我的sql代碼正常工作,但教師正在讓我們改變這一點,以便在擴展類中使用晚期靜態綁定。 – user3752792

回答

0

您的問題可能是您正在使用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') 

只是猜測這裏,希望幫助。

+0

MySQL可以自動將''1''轉換爲'1',這不是問題。但它不能將''''轉換爲'0',這就是這個特定問題的原因。 – DCoder

+0

我猜'$屬性'值是字符串(不是'1',但更多是'a'),所以試圖在int類型列中添加字符串將會失敗。你可能是對的,猜測我們缺乏信息。 – Meeuuuhhhh

+0

引用的錯誤消息表明被插入的值正好是'''',而不是其他字符串;)(在一般情況下,OP也需要正確處理NULL值)。 – DCoder

2

我認爲可能有一個問題與魔術報價。請使用addslash php函數array_values($attributes)

+0

謝謝,我試過了,它沒有工作。問題是我從一個工作的sql格式如果將我的id,用戶名,密碼,名字和姓氏按照我的教師的格式插入到這個格式中,但它不起作用 – user3752792

+0

您能打印sql並粘貼到這裏,也請分享這個結構 –

+0

這是太多的數據放在這裏,我可以發郵件給你。 – user3752792

0

我運行相同的問題。如果有人遇到同樣的問題,我想發佈回覆。我認爲你是按照lynda.com教程。正如DCoder提到的,id是自動遞增值,並且無法知道該值是什麼,而且,join函數使每個屬性值 - 列值字符串,在您的情況下,您會得到空的""字符串。但是id字段是一個int。 您的第一個創建函數,沒有連接的函數,因爲ID字段被遺漏了。 現在以固定第二功能創造後再行$attributes = $this->attributes();添加以下代碼
if($attributes["id"]==""){ array_shift($attributes); } 行後$attributes = $this->attributes(); 你的屬性擁有屬性的列表;我也假設你的代碼和我遇到的教程是一樣的。所以你檢查一下id是否爲空。如果它是空的,你做一個array_shift。這將刪除第一個屬性,即id,並且在此過程中,新屬性數組將沒有id,然後在應用連接時,它將與第一個創建函數相同。這將解決這個問題。但我仍然想知道教師如何通過教程沒有任何問題。偉大的教練和麪向對象的優秀教程 - 我正在談論lynda.com教程。 你可以更詳細地闡述你的問題,以獲得更多的答覆或正確的答案。由於我運行相同的問題使我更容易。

0

爲了讓MySQL的生成序列號爲AUTO_INCREMENT字段,你有一些選擇:

明確指定NULL; 明確指定0

公共函數來創建(){ 全球$數據庫; //如果您的檔案需要清理,請爲您的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; 
    } 
} 
0

這是我想通解決這個問題

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

這是不是從你的最終編程錯誤這裏的問題是MySQL沒有解釋這個動作是有效的,因爲它SQL_MODE是在STRICT模式下。您可以選擇編輯您的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 = ''"); 
    } 
相關問題