简单的插入语句不能在PHP代码中工作。适用于MySQL Workbench

时间:2011-10-15 04:33:58

标签: php mysql insert

我有一个简单的脚本,我已经包含在这里。 select查询工作正常但插入查询失败。我在我的macbook上运行php,apache和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();
}

1 个答案:

答案 0 :(得分: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')";