Reputation: 443
I am attempting to build a page that will allow custom report generation from a pgsql database. In PHP I have declared the variables $table, $datea and &datez... I then have an html form where a user can post the selected table and dates for these variables for the query. However, I am getting an error message (ERROR 500 Page isn't working, unable to handle this request). Can anyone offer some advice?
$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>
Upvotes: 2
Views: 169
Reputation: 5041
Try this. See my comments in the code.
<?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>
Upvotes: 1