简体   繁体   中英

Select data between two dates via form in PHP and PostgreSQL?

i'm trying to display a row from postgreSQL DB. Before that i need to specify which rows with filter by date. And the date need to be entered by form in html. In this time i accomplished this:

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>

And the errors:

    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

Can you help me, please?

Edit: Ok thanks now i search to clear the other errors. If someone can assist me, please :)

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

Edit 2:

Oh sorry i forgot to search more specific for the functions. With this code now its works:

    <? 
$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>';
?>

就像下面这样改变

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM