2011-05-23 57 views
1

這是我的插入查詢。無法在PHP中使用MySql插入數據

$insertQuery = "INSERT INTO timeline (id_str, from_user, text, timestamp, location) VALUES ($resultsAry[$x]['id_str'], $resultsAry[$x]['from_user'], $resultsAry[$x]['text'], $datetime, $locationAry[$i]['place'])"; 

傳遞給查詢的值似乎是正確的(通過回顯所有值進行檢查)。但我得到了以下錯誤。

Insert failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '['id_str'], Array['from_user'], Array['text'], 2011-05-23 18:58:27, Array['place' at line 1

請幫忙?

+0

我認爲您需要引用您的值。 – martynthewolf 2011-05-23 11:07:13

回答

1

試試這個:

$insertQuery = "INSERT INTO timeline (id_str, from_user, text, timestamp, location) VALUES ('".$resultsAry[$x]['id_str']."', '".$resultsAry[$x]['from_user']."', '".$resultsAry[$x]['text']."', '".$datetime."', '".$locationAry[$i]['place']."')"; 
+0

謝謝。有用。 =) – 2011-05-23 11:31:53

2

爲了讓PHP正確解析您的數組鍵,你可以用{}封裝它們是這樣的:

$insertQuery = "INSERT INTO timeline (id_str, from_user, text, timestamp, location) VALUES ({$resultsAry[$x]['id_str']}, {$resultsAry[$x]['from_user']}, {$resultsAry[$x]['text']}, $datetime, {$locationAry[$i]['place']})"; 
2

幫自己一個忙,至少是逃脫值來保護數據。將值連接到查詢中:

$insertQuery = " 
INSERT INTO timeline (
    id_str, 
    from_user, 
    text, 
    timestamp, 
    location 
) 
VALUES (
    '" . mysql_real_escape_string($resultsAry[$x]['id_str']) . "', 
    '" . mysql_real_escape_string($resultsAry[$x]['from_user'] . "', 
    '" . mysql_real_escape_string($resultsAry[$x]['text'] . "', 
    '" . mysql_real_escape_string($dateTime) . "', 
    '" . mysql_real_escape_string($locationAry[$i]['place']) . "' 
);"; 

echo $insertQuery; 
+0

我認爲你錯過了a)最後... – BlueEel 2011-05-23 11:13:21

+0

我也這麼認爲,謝謝你的頭:) – 2011-05-23 11:14:45