PHP 到 Postgres,将日期变量从表单传递到 PHP 以执行查询
好的,我被要求编写这段代码。这是我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 plpgsql 函数(存储过程)。
编辑:更简单,如下所示:
致电(11 月):
要点:
PostgreSQL 函数的一般建议
You can use a plpgsql function for that (stored procedure).
Edit: much simpler like this:
Call (for November):
Major points:
General advice on PostgreSQL functions
HTML:
PHP:
HTML:
PHP: