我有一个简单的脚本,我在这里包括。选择查询工作正常,但插入查询失败。我在我的macbook上运行php, apache和mysql。
表city_profile将ID作为自动递增的主键。name是非空值。
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();
}
尝试改变这一行:
$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')";