【问题标题】:Simple insert statement not working in php code. Works from MySQL Workbench简单的插入语句在 php 代码中不起作用。从 MySQL Workbench 工作
【发布时间】:2011-10-15 04:33:58
【问题描述】:

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

【问题讨论】:

  • 是的,但仅适用于字段名称,不适用于值,或者只是删除字段名称周围的单引号。

标签: php mysql insert


【解决方案1】:

尝试改变这一行:

$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')"; 

【讨论】:

    猜你喜欢
    • 2016-11-09
    • 1970-01-01
    • 2012-08-05
    • 2014-06-28
    • 2013-07-19
    • 2012-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多