php - 简单的插入语句在 php 代码中不起作用。在 MySQL Workbench 上运行

标签 php mysql insert

我有一个简单的脚本,已包含在此处。选择查询工作正常,但插入查询失败。我在我的 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/

相关文章:

javascript - Bootstrap 模式弹出窗口显示状态 -3 错误

php - 用 PHP 调用 JavaScript

python - 如何在Python中基于列表元素创建MySQL表?

mysql - varchar 或 text - mysql 性能

postgresql - 如何尽快使用查询将批量数据从表加载到表? (postgresql)

php - 如何在一个查询中执行插入操作?

c - 向链表中插入一个元素

php excel 导出 - 文本被分成新行

php - 这个php脚本有什么问题?

mysql - ER_HOST_NOT_PRIVILEGED - docker 容器无法连接到 mariadb