Angga Dwi Krishnajaya
Angga Dwi Krishnajaya

Reputation: 33

Show multiple database value using drop-down

So, I have this code that direct form value to the other page

<form action="table2.php" method="post">
    Date : (yyyy-mm-dd)<br>
    <select name="date1">

     <?php 
        while ($row = mysqli_fetch_array($query,MYSQLI_ASSOC)){
          echo "<option value=\"date1\">" . $row['Date'] . "</option>";
        }
     ?>
    </select>
        <br><br>
        <p>Sampai</P>

    Date : (yyyy-mm-dd)<br>
    <select name="date2">

        <?php 
           while ($row = mysqli_fetch_array($query,MYSQLI_ASSOC)){
            echo "<option value=\"date2\">" . $row['Date'] . "</option>";
           }
        ?>
    </select>
    <br><br>

First Date work fine, but the second Date wont show the value of database. Can anyone help me please ? Thanks

Upvotes: 1

Views: 64

Answers (3)

Sougata Bose
Sougata Bose

Reputation: 31739

After running the first mysqli_fetch_array(), the resource will be empty. Try with -

    Date : (yyyy-mm-dd)<br>
    <select name="date1">
    <?php 
    $query1 = $query;
    while ($row = mysqli_fetch_array($query1,MYSQLI_ASSOC)){
    echo "<option value=\"date1\">" . $row['Date'] . "</option>";
    }
    ?>
    </select>
        <br><br>
        <p>Sampai</P>

    Date : (yyyy-mm-dd)<br>
    <select name="date2">

        <?php 
           while ($row = mysqli_fetch_array($query,MYSQLI_ASSOC)){
            echo "<option value=\"date2\">" . $row['Date'] . "</option>";
           }
        ?>
    </select>

Upvotes: 1

user1937699
user1937699

Reputation:

Maybe you can try to retrieve date1 and date2 using JOIN statement and loop it into drop down rather then doing it separately

or

select a.date1 , b.date2 from table1 as , table2 as b (if there is any connection between two table example using ID you may but in condition here )

Upvotes: 0

Shailesh Katarmal
Shailesh Katarmal

Reputation: 2785

<form action="table2.php" method="post">
Date : (yyyy-mm-dd)<br>
<select name="date1">

 <?php 
    //your sql query here............
    while ($row = mysqli_fetch_array($query,MYSQLI_ASSOC)){
      echo "<option value=\"date1\">" . $row['Date'] . "</option>";
    }
 ?>
</select>
    <br><br>
    <p>Sampai</P>

Date : (yyyy-mm-dd)<br>
<select name="date2">

    <?php 
     //again your SQL query here.........
       while ($row = mysqli_fetch_array($query,MYSQLI_ASSOC)){
        echo "<option value=\"date2\">" . $row['Date'] . "</option>";
       }
    ?>
</select>
<br><br>

Upvotes: 0

Related Questions