我有一个简单的脚本,已包含在此处。选择查询工作正常,但插入查询失败。我在我的 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')";
关于php - 简单的插入语句在 php 代码中不起作用。在 MySQL Workbench 上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7775763/