【问题标题】:Convert "INSERT" MySQL query to Postgresql query将“INSERT”MySQL 查询转换为 Postgresql 查询
【发布时间】:2019-06-22 15:38:33
【问题描述】:

我被困了一段时间来解决这个问题。我关注了这篇文章https://www.sitepoint.com/creating-a-scrud-system-using-jquery-json-and-datatables/ 创建 SCRUD 系统。但是当我需要向 PostgreSQL 添加新记录时,我卡住了。

代码的工作 MySQL 部分是:

$db_server   = 'localhost';
$db_username = 'root';
$db_password = '123456';
$db_name     = 'test';
$db_connection = mysqli_connect($db_server, $db_username, $db_password, $db_name);
$query = "INSERT INTO it_companies SET ";
if (isset($_GET['rank']))         { $query .= "rank         = '" . mysqli_real_escape_string($db_connection, $_GET['rank'])         . "', "; }
if (isset($_GET['company_name'])) { $query .= "company_name = '" . mysqli_real_escape_string($db_connection, $_GET['company_name']) . "', "; }
if (isset($_GET['industries']))   { $query .= "industries   = '" . mysqli_real_escape_string($db_connection, $_GET['industries'])   . "', "; }
if (isset($_GET['revenue']))      { $query .= "revenue      = '" . mysqli_real_escape_string($db_connection, $_GET['revenue'])      . "', "; }
if (isset($_GET['fiscal_year']))  { $query .= "fiscal_year  = '" . mysqli_real_escape_string($db_connection, $_GET['fiscal_year'])  . "', "; }
if (isset($_GET['employees']))    { $query .= "employees    = '" . mysqli_real_escape_string($db_connection, $_GET['employees'])    . "', "; }
if (isset($_GET['market_cap']))   { $query .= "market_cap   = '" . mysqli_real_escape_string($db_connection, $_GET['market_cap'])   . "', "; }
if (isset($_GET['headquarters'])) { $query .= "headquarters = '" . mysqli_real_escape_string($db_connection, $_GET['headquarters']) . "'";   }
$query = mysqli_query($db_connection, $query);

我设法写了这个,但它对 PostgreSQL 不起作用:

$conn_string = "dbname=test user=postgres password=123456";
$query = "INSERT INTO it_companies VALUES ";
if (isset($_GET['rank']))         { $query .= "('" . pg_escape_string($db_connection, $_GET['rank'])         . "', "; }
if (isset($_GET['company_name'])) { $query .= "'" . pg_escape_string($db_connection, $_GET['company_name']) . "', "; }
if (isset($_GET['industries']))   { $query .= "'" . pg_escape_string($db_connection, $_GET['industries'])   . "', "; }
if (isset($_GET['revenue']))      { $query .= "'" . pg_escape_string($db_connection, $_GET['revenue'])      . "', "; }
if (isset($_GET['fiscal_year']))  { $query .= "'" . pg_escape_string($db_connection, $_GET['fiscal_year'])  . "', "; }
if (isset($_GET['employees']))    { $query .= "'" . pg_escape_string($db_connection, $_GET['employees'])    . "', "; }
if (isset($_GET['market_cap']))   { $query .= "'" . pg_escape_string($db_connection, $_GET['market_cap'])   . "', "; }
if (isset($_GET['headquarters'])) { $query .= "'" . pg_escape_string($db_connection, $_GET['headquarters']) . "');";   }
$query = pg_query($db_connection, $query);

我从系统得到的消息是:“添加请求失败:parsererror”

编辑和删除功能运行良好。

我按照 PGSQL 站点示例构建此子句:

INSERT INTO films VALUES
    ('UA502', 'Bananas', 105, '1971-07-13', 'Comedy', '82 minutes');

我做错了什么吗?谢谢!

更新 查询的回声和错误是 id 列。在 Mysql 代码中,ID 列没有问题。为什么当我使用 pgsql 时它会这样做?:

INSERT INTO it_companies (rank,company_name,industries,revenue,fiscal_year,employees,market_cap,headquarters) 
VALUES ('1', 'asd', 'asd', '1', '2000', '2', '3', 'asdf');

    Warning: pg_query(): Query failed: ERROR: duplicate key value violates unique constraint "it_companies_pkey" DETAIL: Key (company_id)=(2) already exists. in C:\WEB\Apache24\htdocs\datatableeditor\data.php on line 121
        {"result":"error","message":"query error"

,"data":[]}

更新2 有一个错误的工作代码:

$query = "INSERT INTO it_companies (rank,company_name,industries,revenue,fiscal_year,employees,market_cap,headquarters) VALUES ";
if (isset($_GET['rank']))         { $query .= "('" . $_GET['rank']         . "', "; }
if (isset($_GET['company_name'])) { $query .= "'" . $_GET['company_name'] . "', "; }
if (isset($_GET['industries']))   { $query .= "'" . $_GET['industries']   . "', "; }
if (isset($_GET['revenue']))      { $query .= "'" . $_GET['revenue']      . "', "; }
if (isset($_GET['fiscal_year']))  { $query .= "'" . $_GET['fiscal_year']  . "', "; }
if (isset($_GET['employees']))    { $query .= "'" . $_GET['employees']    . "', "; }
if (isset($_GET['market_cap']))   { $query .= "'" . $_GET['market_cap']   . "', "; }
if (isset($_GET['headquarters'])) { $query .= "'" . $_GET['headquarters'] . "') RETURNING company_id;";   }
echo $query;

此查询后,消息“添加请求失败:解析器错误”仍然存在。但是在手动刷新页面后,新数据会被保存。知道为什么会出现此消息并且不会自动加载数据吗?

更新 3 - 成功 我忘记从导致错误消息的代码中删除echo $query;。 现在一切正常。感谢大家的帮助! :)

【问题讨论】:

  • 您是否回显了查询以查看其外观?
  • 在实际尝试运行查询之前,请查看$query 的内容。它可能不包含您认为的内容。
  • 我对 PHP 一无所知,但我怀疑 isset 中的 1 个返回 false,rankheadquarters,导致 () 丢失,因此创建了一个语法错误。即使我错了,请考虑将() 放在ifs 之外,因为它们始终是必需的。
  • 清理您的输入 :)

标签: php mysql postgresql


【解决方案1】:

您需要在查询字符串构建方面做更多工作。

如果排名存在,您只添加左括号(

如果总部在场,您只需添加右括号 )

您还需要指定哪个字段列获取哪个值,否则您以headquarter 名称结尾到fiscal_year 字段中。如果未指定列,则按与表中定义相同的顺序添加值。

INSERT INTO TABLE_NAME (column1, column2, column3,...columnN)
VALUES (value1, value2, value3,...valueN);

其他人有评论,请检查$query 看看你有什么。

【讨论】:

  • 我填写了所有字段。此时我不知道如何回显代码以查看更多信息。对于列名,我会尝试。知道如何在 $query 变量中转义“(”吗?我有一列用于 id 自动递增。
  • 如何在 php 中进行调试? echo是显示变量内容的方式php.net/manual/es/function.echo.php
  • 我知道但我不理解其余代码中的程序逻辑,现在我设法得到了回声。我更新了问题。对不起
  • 自动数字是哪一列?秩?在这种情况下,您不应该包含该列并让 db 为您创建一个不同的数字。
  • 我有company_id integer NOT NULL DEFAULT nextval('it_companies_company_id_seq'::regclass)
猜你喜欢
  • 1970-01-01
  • 2021-10-30
  • 2018-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-06
相关资源
最近更新 更多