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

时间:2016-12-20 19:41:08

标签: php html postgresql

我正在尝试构建一个允许从pgsql数据库生成自定义报告的页面。在PHP中,我已声明变量$ table,$ datea和& datez ...然后我有一个html表单,用户可以在其中发布所选表格和查询的这些变量的日期。但是,我收到一条错误消息(ERROR 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>

1 个答案:

答案 0 :(得分:1)

试试这个。 在代码中查看我的评论。

<?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>