2011-10-15 31 views
2

我有一個簡單的腳本,我已經包括在這裏。選擇查詢工作正常,但插入查詢失敗。我在我的macbook上運行php,apache和mysql。簡單的插入語句不能在PHP代碼中工作。從MySQL工作臺工作

表city_profile具有ID作爲自動增量主鍵。並且名稱是非空的。

function testMySQL() { 
    $db = new mysqli('localhost', 'root', NULL, 'citee'); 
    //$query = "select * from city_profile"; //this query works 
    $query = "insert into city_profile ('name','state','country') values ('charlotte','north carolina','usa')"; 
    //whereas the above one fails.. 
    $results = $db->query($query); 
    if($results) { 
    echo '<p>The query is successful.</p>'; 
    }else { 
    echo '<p>The query is NOT successful.</p>'; 
    } 

    //close the connection 
$db->close(); 
} 
+0

改變'''入' – diEcho

+0

是的,但僅限於字段名,而不是價值,或者只是刪除字段名稱的單引號。 – stivlo

+0

謝謝...它的工作原理。 – edorahg

回答

3

嘗試改變這一行:

$query = "insert into city_profile ('name','state','country') values ('charlotte','north carolina','usa')"; 

成這樣:

$query = "insert into `city_profile` (`name`,`state`,`country`) values ('charlotte','north carolina','usa')"; 
+1

感謝您的回答。有用 !!! – edorahg