0

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>';
?>
2
  • This would be a case of How to do basic form handling in PHP. Commented Jul 20, 2017 at 7:06
  • Yes that's right. I saw this but i did a mistake in the form before. Commented Jul 20, 2017 at 7:56

1 Answer 1

1

just change like below

$result = pg_exec("SELECT * FROM import.mock_data WHERE date BETWEEN '" . $_POST['from_date'] . "' AND  '" . $_POST['to_date']. "'ORDER by id DESC");
Sign up to request clarification or add additional context in comments.

1 Comment

Did you ever hear about Bobby Tables? Because that's how you invite him!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.