php - 来自 html 表单的 pgsql 日期范围查询

标签 php html postgresql

我正在尝试构建一个允许从 pgsql 数据库生成自定义报告的页面。在 PHP 中,我声明了变量 $table、$datea 和 &datez... 然后我有一个 html 表单,用户可以在其中发布选定的表和这些变量的日期以供查询。但是,我收到一条错误消息(错误 500 页面不工作,无法处理此请求)。谁能提供一些建议?

$datea= $_POST["userDatea"];
$table= $_POST["userTable"];
$datez= $_POST["userDatez"];

if(isset($_POST['submit'])
// Create connection
$conn = pg_connect("host=xx.xx.xx.xx port=xxxx dbname=fpscdb001 user=xxx password=xxxxxxxxxxxxxx");

// Check connection
if (!$conn) {
echo "Did not connect.\n";
exit;
}
$result = pg_query($conn, "SELECT *
FROM 
fpscdb001_ws_001.$table
WHERE 
$table.created_on BETWEEN '$datea' AND '$datez' AND
$table.soft_delete_id = '0';");

Form:
<form method="post" id="report" action="custom.php">
<div class="formitem" style="max-width: 200px;">
<p style="font-color: white" style="font-weight: 800px">Select a Table:</p>
<p><select name="table" class="form-control" id="userTable">
<option value="dispatch_1">Dispatch</option>
<option value="normal_usb__a_t">Inventory</option>
<option value="incident">Real Estate</option>
<option value="tech_support2">Tech-Support</option>
</select></p>
</div>
<div class="formitem" style="max-width: 200px" style="margin-bottom: 20px">
<p>FROM DATE</p> <input type="DATE" class="textarea" id="userDatea" style="height: 30px; border-radius: 5px;"><br><br>
<p>TO DATE</p> <input type="DATE" class="textarea" id="userDatez" style="height: 30px; border-radius: 5px;"><br><br>
</div>                          
<div style="padding-bottom: 120px">
<input type="submit" class="btn btn-small greyBtn light submit" value="Submit" style="max-width: 200px; max-height: 125px"> 
</div>
</form>

最佳答案

试试这个。 请参阅我在代码中的评论。

<?php
    // show error messages
    ini_set('error_reporting', E_ALL);
    ini_set("display_errors", 1);

    $datea= $_POST["userDatea"];
    $table= $_POST["userTable"];
    $datez= $_POST["userDatez"];

    // You need to do all of this if and only if this is a post request
    // Also this method of detecting a post request is more consistent
    if( !empty($_SERVER['REQUEST_METHOD']) && (strcasecmp($_SERVER['REQUEST_METHOD'], 'post')===0)  ) {
        // Create connection
        $conn = pg_connect("host=xx.xx.xx.xx port=xxxx dbname=fpscdb001 user=xxx password=xxxxxxxxxxxxxx");

        // Check connection
        if (!$conn) {
            echo "Did not connect.\n";
            exit;
        }

        $result = pg_query($conn,
            "SELECT *
            FROM 
            fpscdb001_ws_001.$table
            WHERE 
            $table.created_on BETWEEN '$datea' AND '$datez' AND
            $table.soft_delete_id = '0';"
        );


        if (!$result) {
            echo "Query failed\n";
            exit;
        }

        exit('Success?');
    }
?>
<!-- Make sure your HTML is well formed  -->
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>Test</title>
</head>
<body>
Form:
<form method="post" id="report" action="custom.php">
    <div class="formitem" style="max-width: 200px;">
        <p style="font-color: white" style="font-weight: 800px">Select a Table:</p>
        <!-- The attribute name is used as the key for the $_POST array. Without a name the form control will not be submitted   -->
        <p><select name="userTable" class="form-control" id="userTable">
                <option value="dispatch_1">Dispatch</option>
                <option value="normal_usb__a_t">Inventory</option>
                <option value="incident">Real Estate</option>
                <option value="tech_support2">Tech-Support</option>
            </select></p>
    </div>
    <div class="formitem" style="max-width: 200px" style="margin-bottom: 20px">
        <!-- The attribute name is used as the key for the $_POST array. Without a name the form control will not be submitted   -->
        <p>FROM DATE</p> <input type="DATE" class="textarea" id="userDatea" name="userDatea" style="height: 30px; border-radius: 5px;"><br><br>
        <!-- The attribute name is used as the key for the $_POST array. Without a name the form control will not be submitted   -->
        <p>TO DATE</p> <input type="DATE" class="textarea" id="userDatez" name="userDatez"  style="height: 30px; border-radius: 5px;"><br><br>
    </div>
    <div style="padding-bottom: 120px">
        <input type="submit" class="btn btn-small greyBtn light submit" value="Submit" style="max-width: 200px; max-height: 125px">
    </div>
</form>
</body>
</html>

关于php - 来自 html 表单的 pgsql 日期范围查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41250065/

相关文章:

javascript - Sequelize.js ownsToMany 实例方法 create 不起作用?

php - 查询 Laravel Eloquent 多对多,其中所有 id 都相等

php - PDO 参数化与非参数化查询速度

php - 是否可以使用 PHP cURL 获得部分响应?

javascript - html:如何将上传文件格式限制为纯文本

sql - PostgreSQL 按单个值过滤组

php - 要为一行插入的多维数组

javascript - 有什么方法可以确定鼠标悬停在哪个角色上而不创建大量跨度?

javascript - 如何在无需用户点击的情况下进行复制

mysql - RoR : Cannot change_column in postgres, 在 MySQL 中正常(MySQL 用于开发,Postgres on Heroku)