PHP 到 Postgres,将日期变量从表单传递到 PHP 以执行查询

发布于 2024-12-09 10:44:50 字数 1251 浏览 0 评论 0原文

好的,我被要求编写这段代码。这是我在 PHP 中使用的 PGSQL 查询,用于在 HTML 页面上显示。我正在使用正确的 PHP 让它显示手动输入的日期。我想要做的是替换 PGSQL 的最后一行:

SELECT cm."ID",a."ID" as "DSI",cm."Date",c."Amount",ct."Name" as "Name",cm."Comments" as "Comments"
FROM "Memo" cm
LEFT JOIN "Credit" c ON (c."ID" = cm."CreditID")
LEFT JOIN "Account" a ON (c."AccountID" = a."ID")
LEFT JOIN "CreditMemoReason" ct ON (ct."ID" = cm."CreditMemoReasonID")
WHERE cm."Date" >= '2011-09-01' AND cm."Date" < '2011-10-01';

我希望将变量从 HTML 表单传递到 PHP,以便一旦用户点击提交,它就会使用所选日期执行查询。上面确实显示了完整的日期。我只想从下拉选择框中选择一个月,然后输入的日期将为 2011-09-01 至 2011-10-01 作为 9 月,或 2011-08-01 至 2011-09- 8 月为 01,依此类推。我真的需要一些帮助才能做到这一点。

我的 php 现在看起来像这样显示查询:

$result = pg_query($query) or die('Query failed: ' .
pg_last_error());
// Printing results in HTML
echo "<h2 align=center>Revenue Report</h2>";
echo "<table align=center border=1 solid width=500px>\n";
while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
  echo "\t<tr>\n";
  foreach ($line as $col_value) {
      echo "\t\t<td>$col_value</td>\n";
  }
  echo "\t</tr>\n";
}
echo "</table>\n";// Free resultset
pg_free_result($result);// Closing connection
pg_close($dbconn);

Ok so I was posed this bit of coding. It is a PGSQL query that I use in PHP to display on a HTML page. I am using the correct PHP to get it to display for a manually entered date. What I want to do is replace the last line of the PGSQL:

SELECT cm."ID",a."ID" as "DSI",cm."Date",c."Amount",ct."Name" as "Name",cm."Comments" as "Comments"
FROM "Memo" cm
LEFT JOIN "Credit" c ON (c."ID" = cm."CreditID")
LEFT JOIN "Account" a ON (c."AccountID" = a."ID")
LEFT JOIN "CreditMemoReason" ct ON (ct."ID" = cm."CreditMemoReasonID")
WHERE cm."Date" >= '2011-09-01' AND cm."Date" < '2011-10-01';

I want to have the variable passed from the HTML form to PHP so that once the user hits submit it excutes the Query with the date selected. Really the above shows a full date. I am only wanting to have the select be a month only from a drop down select box and then the date entered will be 2011-09-01 thru 2011-10-01 as September, or 2011-08-01 thru 2011-09-01 for August and so on. I really need some help doing so.

My php looks like this right now to display the query:

$result = pg_query($query) or die('Query failed: ' .
pg_last_error());
// Printing results in HTML
echo "<h2 align=center>Revenue Report</h2>";
echo "<table align=center border=1 solid width=500px>\n";
while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
  echo "\t<tr>\n";
  foreach ($line as $col_value) {
      echo "\t\t<td>$col_value</td>\n";
  }
  echo "\t</tr>\n";
}
echo "</table>\n";// Free resultset
pg_free_result($result);// Closing connection
pg_close($dbconn);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北城孤痞 2024-12-16 10:44:51

您可以使用 plpgsql 函数(存储过程)。

编辑:更简单,如下所示:

CREATE OR REPLACE FUNCTION f_test(integer)
  RETURNS TABLE(
 "ID"       integer  -- !guessing your data types, you may have to adjust!
,"DSI"      integer
,"Date"     date
,"Amount"   integer
,"Name"     text
,"Comments" text) AS
$BODY$
DECLARE
  mydate    date := (to_char(now(), 'YYYY') || '-' || $1::text || '-1')::date;
BEGIN

RETURN QUERY
SELECT cm."ID"
      ,a."ID" -- AS "DSI"
      ,cm."Date"
      ,c."Amount"
      ,ct."Name"
      ,cm."Comments"
  FROM "Memo" cm
  LEFT JOIN "Credit" c ON (c."ID" = cm."CreditID")
  LEFT JOIN "Account" a ON (c."AccountID" = a."ID")
  LEFT JOIN "CreditMemoReason" ct ON (ct."ID" = cm."CreditMemoReasonID")
 WHERE cm."Date" >= mydate AND cm."Date" < (mydate + interval '1 month')::date;

END;
$BODY$
  LANGUAGE plpgsql;

致电(11 月):

SELECT * FROM f_test(11);

要点:

  • 假设您想要当前年份。你没有告诉。
  • 编辑:12 月不需要特殊情况,只需添加间隔即可。
  • 如果您输入无效的月份,则会触发错误。
  • 请注意不要将 OUT 参数用作不合格的列名。 (名称冲突!)这就是我注释掉的原因: -- AS "DSI"
  • 某些列别名是多余的 关于

PostgreSQL 函数的一般建议

You can use a plpgsql function for that (stored procedure).

Edit: much simpler like this:

CREATE OR REPLACE FUNCTION f_test(integer)
  RETURNS TABLE(
 "ID"       integer  -- !guessing your data types, you may have to adjust!
,"DSI"      integer
,"Date"     date
,"Amount"   integer
,"Name"     text
,"Comments" text) AS
$BODY$
DECLARE
  mydate    date := (to_char(now(), 'YYYY') || '-' || $1::text || '-1')::date;
BEGIN

RETURN QUERY
SELECT cm."ID"
      ,a."ID" -- AS "DSI"
      ,cm."Date"
      ,c."Amount"
      ,ct."Name"
      ,cm."Comments"
  FROM "Memo" cm
  LEFT JOIN "Credit" c ON (c."ID" = cm."CreditID")
  LEFT JOIN "Account" a ON (c."AccountID" = a."ID")
  LEFT JOIN "CreditMemoReason" ct ON (ct."ID" = cm."CreditMemoReasonID")
 WHERE cm."Date" >= mydate AND cm."Date" < (mydate + interval '1 month')::date;

END;
$BODY$
  LANGUAGE plpgsql;

Call (for November):

SELECT * FROM f_test(11);

Major points:

  • I assumed you want the current year. You did not tell.
  • Edit: no special case for December needed, just add interval.
  • If you enter an invalid month you trigger an error.
  • Be careful not to use the OUT parameters as unqualified column names. (name conflict!) That's why I commented that out: -- AS "DSI"
  • Some column aliases were redundant

General advice on PostgreSQL functions

从此见与不见 2024-12-16 10:44:51

HTML:

<select name="startdate">
<option value="2014-01-01">2014-01-01</option>
</select>

PHP:

$startdate = pg_escape_string($_REQUEST['startdate']);

$query = <<<SQL
SELECT cm."ID",a."ID" as "DSI",cm."Date",c."Amount",ct."Name" as "Name",cm."Comments" as "Comments"
FROM "Memo" cm
LEFT JOIN "Credit" c ON (c."ID" = cm."CreditID")
LEFT JOIN "Account" a ON (c."AccountID" = a."ID")
LEFT JOIN "CreditMemoReason" ct ON (ct."ID" = cm."CreditMemoReasonID")
WHERE cm."Date" >= {$startdate} AND cm."Date" < ({$startdate}+ interval '1 month')::date;
SQL;

HTML:

<select name="startdate">
<option value="2014-01-01">2014-01-01</option>
</select>

PHP:

$startdate = pg_escape_string($_REQUEST['startdate']);

$query = <<<SQL
SELECT cm."ID",a."ID" as "DSI",cm."Date",c."Amount",ct."Name" as "Name",cm."Comments" as "Comments"
FROM "Memo" cm
LEFT JOIN "Credit" c ON (c."ID" = cm."CreditID")
LEFT JOIN "Account" a ON (c."AccountID" = a."ID")
LEFT JOIN "CreditMemoReason" ct ON (ct."ID" = cm."CreditMemoReasonID")
WHERE cm."Date" >= {$startdate} AND cm."Date" < ({$startdate}+ interval '1 month')::date;
SQL;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文