简体   繁体   English

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

[英]PHP to Postgres, Pass a date variable from a form to PHP to excute a query

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. 我在PHP中使用它来在HTML页面上显示它是一个PGSQL查询。 I am using the correct PHP to get it to display for a manually entered date. 我正在使用正确的PHP使它显示为手动输入的日期。 What I want to do is replace the last line of the PGSQL: 我想做的是替换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. 我想将变量从HTML表单传递给PHP,以便一旦用户点击提交,它将使用选定的日期执行查询。 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. 我只想从下拉选择框中选择一个月,然后输入的日期将是9月的2011-09-01至2011-10-01,或2011-09--01至2011-09- 01代表8月,依此类推。 I really need some help doing so. 我确实需要一些帮助。

My php looks like this right now to display the query: 我的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);

HTML: HTML:

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

PHP: 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;

You can use a plpgsql function for that (stored procedure). 您可以为此使用plpgsql函数 (存储过程)。

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): 致电(11月):

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. 编辑:12月不需要特殊情况,只需添加间隔。
  • If you enter an invalid month you trigger an error. 如果输入的月份无效,则会触发错误。
  • Be careful not to use the OUT parameters as unqualified column names. 注意不要将OUT参数用作不合格的列名。 (name conflict!) That's why I commented that out: -- AS "DSI" (名称冲突!)这就是为什么我将其注释掉:-AS“ DSI”
  • Some column aliases were redundant 一些列别名是多余的

General advice on PostgreSQL functions PostgreSQL函数的一般建议

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM