2013-04-22 42 views
0

我可能只是太累了,我錯過了一些非常簡單的東西,但我無法弄清楚。看起來無法找到SQL Statement錯誤的地方

試圖執行以下查詢:在此錯誤

INSERT INTO chars (charName,charClass,charLevel,charLife,charES,charInt,charStr,charDex) 
VALUES (mlkauschwitz,ranger,81,4500,50,50,300,250) 
ON DUPLICATE KEY UPDATE charClass=ranger,charLevel=81,charLife=4500,charES=50,charInt=50,charStr=300,charDex=250; 

結果:

"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'mlkauschwitz' in 'field list'" 

爲什麼想的值是場?

使用以下PHP:

include "db.php"; 
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
$char = array(
    'charClass'=>'ranger', 
    'charDex'=>'250', 
    'charES'=>'50', 
    'charInt'=>'50', 
    'charLevel'=>'81', 
    'charLife'=>'4500', 
    'charName'=>'mlkauschwitz', 
    'charStr'=>'300', 
); 
$sql = 'INSERT INTO chars (charName,charClass,charLevel,charLife,charES,charInt,charStr,charDex) VALUES ('.$char["charName"].','.$char["charClass"].','.$char["charLevel"].','.$char["charLife"].','.$char["charES"].','.$char["charInt"].','.$char["charStr"].','.$char["charDex"].') ON DUPLICATE KEY UPDATE charClass='.$char["charClass"].',charLevel='.$char["charLevel"].',charLife='.$char["charLife"].',charES='.$char["charES"].',charInt='.$char["charInt"].',charStr='.$char["charStr"].',charDex='.$char["charDex"].';'; 
$stmt = $conn->prepare($sql); 
$stmt->setFetchMode(PDO::FETCH_ASSOC); 
if ($stmt) { 
    try { 
     $stmt->execute(); 
    } 
    catch (PDOException $e) { 
     var_dump($e); 
    } 
} 

CHARNAME字段獨特/初級。

回答

4

生成的SQL中的值需要引用。使用PDO ,你應該做這樣的事情:

$sql = 'INSERT INTO chars (charName,charClass,charLevel,charLife,charES,charInt,charStr,charDex) VALUES (?, ?, ?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE charClass='.$char["charClass"].',charLevel='.$char["charLevel"].',charLife='.$char["charLife"].',charES='.$char["charES"].',charInt='.$char["charInt"].',charStr='.$char["charStr"].',charDex='.$char["charDex"].';'; 
$stmt = $conn->prepare($sql); 
$stmt->setFetchMode(PDO::FETCH_ASSOC); 
if ($stmt) { 
    try { 
     $stmt->execute(array($char["charName"],$char["charClass"],$char["charLevel"],$char["charLife"],$char["charES"],$char["charInt"],$char["charStr"],$char["charDex"])); 
    } 
    catch (PDOException $e) { 
     var_dump($e); 
    } 
} 

不知道確切的語法,但你可以看看它的說明書中無。這可以確保PDO正確引用所有值,並避免mysql注入問題。

0

如果mlkauschwitz不列,但價值,你有引號,

看到mysqli_escape_string()

+0

注意,這不會_add_引號的字符串。另外,PDO不需要PDO,因爲PDO負責引用和轉義。 – 2013-04-22 05:07:08

0

更改您的查詢INSERT INTO chars (charName,charClass,charLevel,charLife,charES,charInt,charStr,charDex) VALUES ('mlkauschwitz','ranger','81','4500','50','50','300','250') ON DUPLICATE KEY UPDATE charClass='ranger',charLevel='81',charLife='4500',charES='50',charInt='50',charStr='300',charDex='250';