-1

I am new to php. I would want to get the values selected by the user from 2 different select options.
CODE:

<?php
// Create database connection
$db = mysqli_connect("localhost", "root", "", "organisation");
if (isset($_POST['submit'])) {
    $from_date = $_POST['from_date'];
    $to_date =$_POST['to_date'];
    $result = mysqli_query($db, "SELECT * 
                                FROM images 
                                WHERE year >= '$from_date' 
                                AND year <= '$to_date';");
} 
?>
<!DOCTYPE html>
<html>
<head>

<title>Gallery</title>
<meta charset="utf-8">
</head>
<body>
    <div id="header"></div><br>
        <form action="#" method="post">
            <select name="from_date">
                <option value="2018-01-01">2018-01-01</option>
                <option value="2019-01-01">2019-01-01</option>
                <option value="2020-01-01">2020-01-01</option>
                <option value="2021-01-01">2021-01-01</option>
                <option value="2022-01-01">2022-01-01</option>
            </select> BETWEEN
            <select name="to_date">
                <option value="2018-12-31">2018-12-31</option>
                <option value="2019-12-31">2019-12-31</option>
                <option value="2020-12-31">2020-12-31</option>
                <option value="2021-12-31">2021-12-31</option>
                <option value="2022-12-31">2022-12-31</option>
            </select>
            <input type="submit" name="submit" value="SUBMIT" />
<?php
    while ($row = mysqli_fetch_array($result)) {
        echo "<div id='img_div'>";
            echo "<img src='images/".$row['images']."' >";
        echo "</div>";
    } 
?>
        </form>
</body>
</html>

I have a feeling that I am making a veryy silly mistake, but I have been going through the net for the last 2 hours and it's driving me crazy, pls help.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
B Banner
  • 21
  • 5

2 Answers2

0

Right, you have to remember the life cycle of a page.

The first time this page is run, from a link on another page or directly by entering its address in the browser, it will not have been submitted and therefore there will be no data in the $_POST array.

You correctly check this at the top of the page by doing this

if (isset($_POST['submit']))

but that will not stop your database fetch code later in the page from running regardless, and therefore erroring as there is no valid $result.

There are many ways to rewite this, but this is possibly the simplest.

So I would suggest that you amend the code something like this to make sure you only run code dependant upon the form being submitted when it is actually being submitted.

<?php
// Create database connection
$db = mysqli_connect("localhost", "root", "", "organisation");
if (isset($_POST['submit'])) {
    $from_date = $_POST['from_date'];
    $to_date =$_POST['to_date'];
    $result = mysqli_query($db, "SELECT * 
                                FROM images 
                                WHERE year >= '$from_date' 
                                AND year <= '$to_date';");
} 
?>
<!DOCTYPE html>
<html>
<head>

<title>Gallery</title>
<meta charset="utf-8">
</head>
<body>
    <div id="header"></div><br>
        <form action="#" method="post">
            <select name="from_date">
                <option value="2018-01-01">2018-01-01</option>
                <option value="2019-01-01">2019-01-01</option>
                <option value="2020-01-01">2020-01-01</option>
                <option value="2021-01-01">2021-01-01</option>
                <option value="2022-01-01">2022-01-01</option>
            </select> BETWEEN
            <select name="to_date">
                <option value="2018-12-31">2018-12-31</option>
                <option value="2019-12-31">2019-12-31</option>
                <option value="2020-12-31">2020-12-31</option>
                <option value="2021-12-31">2021-12-31</option>
                <option value="2022-12-31">2022-12-31</option>
            </select>
            <input type="submit" name="submit" value="SUBMIT" />
        </form>
<?php
// check you have something to process
if (!empty($result) {
    while ($row = mysqli_fetch_array($result)) {
        echo "<div id='img_div'>";
            echo "<img src='images/".$row['images']."' >";
        echo "</div>";
    }
} 
?>
</body>
</html>

I also moved your </form> tag so that the form gets correctly closed before outputting the images.

You may also find that the query returns nothing as it is possible to enter a start date that is greater than the end date using the dropdowns, but that is just a matter of adding a check for this.

Also your script is wide open to SQL Injection Attack Even if you are escaping inputs, its not safe! Use prepared parameterized statements in either the MYSQLI_ or PDO API's

You would do that by

$stmt = $db->prepare("SELECT * 
                      FROM images 
                      WHERE year >= ?
                      AND year <= ?";
$stmt->bind_param('ss', $from_date, $to_date);
$stmt->execute();
// get resultset from prpared statement
$result = $stmt->get_result();
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
-1

please refer to this code, it will help you. https://www.sourcecodester.com/php/11560/how-select-data-between-two-dates-phpmysql.html

  • I see you are short of reps, but this is really just a comment. This type of answer attracts downvotes or gets flagged ___I did not DV___ and if that happens you will loose rep points and take longer to get to the 50 reps you need to comment on any question. Until then, stick to questions that are well asked and therefore easily answered without needing clarification. http://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead – RiggsFolly Jun 22 '18 at 11:13