通过PHP和PostgreSQL中的表单选择两个日期之间的数据?

时间:2017-07-20 06:49:37

标签: php html postgresql

我正在尝试从postgreSQL数据库中显示一行。在此之前,我需要指定哪些行按日期过滤。并且需要通过html中的表单输入日期。 在这个时候,我完成了这个:

form.php的:

    <div id = "login">
    <form action = "table.php" method = "POST">
        From date: <input type = "text" name = "from_date" required>
        <input type = "text" name = "referer" style = "display: none" value = "<?=$from_date?>">
        <br />
        <br />
        To date: <input type = "text" name = "to_date" required>
        <input type = "text" name = "referer" style = "display: none" value = "<?=$to_date?>">
        <input type = "submit" name = "submit" value = "Enter">
    </form>
        <p>xxxx-xx-x</p>
</div>

table.php:

    <!DOCTYPE html>
<html>

  <head>
   <title>Тестова таблица</title>
   <link rel="stylesheet" type="text/css" href="style.css">
   <meta charset="UTF-8">
     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 
  <meta name="viewport" content="width=device-width, initial-scale=1">
  </head>
  <body bgcolor="white">

    <? 
  $link = pg_Connect("dbname=Test user=postgres password=1111");
  $result = pg_exec("SELECT * FROM import.mock_data WHERE date BETWEEN '" . $from_date . "' AND  '" . $to_date . "'ORDER by id DESC");
   ?>
<?
if (!$link)
{
    die('Error: Could not connect: ' . pg_last_error());
}


$query = pg_exec($link, "select * from import.mock_data;");

$result = pg_query($query);

$i = 0;
echo '<html><body><table><tr>';
while ($i < pg_num_fields($result))
{
    $fieldName = pg_field_name($result, $i);
    echo '<td>' . $fieldName . '</td>';
    $i = $i + 1;
}
echo '</tr>';
$i = 0;

while ($row = pg_fetch_row($result)) 
{
    echo '<tr>';
    $count = count($row);
    $y = 0;
    while ($y < $count)
    {
        $c_row = current($row);
        echo '<td>' . $c_row . '</td>';
        next($row);
        $y = $y + 1;
    }
    echo '</tr>';
    $i = $i + 1;
}
pg_free_result($result);

echo '</table></body></html>';
?>
  </body>

</html>

错误:

    Notice: Undefined variable: from_date in C:\WEB\Apache24\htdocs\table.php on line 15

Notice: Undefined variable: to_date in C:\WEB\Apache24\htdocs\table.php on line 15

Warning: pg_exec(): Query failed: ERROR: invalid input syntax for type date: "" LINE 1: SELECT * FROM import.mock_data WHERE date BETWEEN '' AND ''... ^ in C:\WEB\Apache24\htdocs\table.php on line 15

Warning: pg_query() expects parameter 1 to be string, resource given in C:\WEB\Apache24\htdocs\table.php on line 26

Warning: pg_num_fields() expects parameter 1 to be resource, null given in C:\WEB\Apache24\htdocs\table.php on line 30

Warning: pg_fetch_row() expects parameter 1 to be resource, null given in C:\WEB\Apache24\htdocs\table.php on line 39

Warning: pg_free_result() expects parameter 1 to be resource, null given in C:\WEB\Apache24\htdocs\table.php on line 54

请帮帮我吗?

编辑:  好的,谢谢,我现在搜索清除其他错误。如果有人可以帮助我,请:)

Warning: pg_query() expects parameter 1 to be string, resource given in C:\WEB\Apache24\htdocs\table.php on line 25

Warning: pg_num_fields() expects parameter 1 to be resource, null given in C:\WEB\Apache24\htdocs\table.php on line 29

Warning: pg_fetch_row() expects parameter 1 to be resource, null given in C:\WEB\Apache24\htdocs\table.php on line 38

Warning: pg_free_result() expects parameter 1 to be resource, null given in C:\WEB\Apache24\htdocs\table.php on line 53

编辑2:

哦对不起,我忘了搜索更具体的功能。现在使用此代码可以实现:

    <? 
$link = pg_Connect("dbname=Test user=postgres password=1111");
if (!$link)
{
    die('Error: Could not connect: ' . pg_last_error());
}


$result = pg_query($link, "SELECT * FROM import.mock_data WHERE date BETWEEN '" . $_POST['from_date'] . "' AND  '" . $_POST['to_date']. "'ORDER by id DESC");

$i = 0;
echo '<html><body><table><tr>';
while ($i < pg_num_fields($result))
{
    $fieldName = pg_field_name($result, $i);
    echo '<td>' . $fieldName . '</td>';
    $i = $i + 1;
}
echo '</tr>';
$i = 0;

while ($row = pg_fetch_row($result)) 
{
    echo '<tr>';
    $count = count($row);
    $y = 0;
    while ($y < $count)
    {
        $c_row = current($row);
        echo '<td>' . $c_row . '</td>';
        next($row);
        $y = $y + 1;
    }
    echo '</tr>';
    $i = $i + 1;
}
pg_free_result($result);

echo '</table></body></html>';
?>

1 个答案:

答案 0 :(得分:1)

只需改变如下

$result = pg_exec("SELECT * FROM import.mock_data WHERE date BETWEEN '" . $_POST['from_date'] . "' AND  '" . $_POST['to_date']. "'ORDER by id DESC");