Skip to content
Advertisement

How do i link different rows of database into buttons and display the appropriate informations?

Below is the php page and code i created to display information from two rows of the database.

enter image description here

<?php
              $sql = "SELECT * from events;";
              $result = mysqli_query($conn, $sql);
              $resultCheck = mysqli_num_rows($result);

              if($resultCheck > 0) {
                while($row = mysqli_fetch_assoc($result)) {
                  echo '
                  <div class="full border" id="border">
                  <p>
                    <img src="event.jpg" alt=" " id="event1"/>
                    <h1>'.$row['EVENT_NAME'].'</h1>
                    <a>'.$row['EVENT_DESC'].'</a>
                    <p align="right">
                      <form action="booking.php" method="get">
                      <input type="hidden" name="eventId" value="$eventId">
                      <button type="submit" class="button">More information</button>
                      </form>
                    </p>
                  </p>
                  </div>

                  <br/><br/>
                  ';
                }
              }
            ?>

This is the picture of my database.

And this is the code and page where the user will be redirected to after clicking the ‘More Information’ button. enter image description here

<?php
              $sql = "SELECT * from events;";
              $result = mysqli_query($conn, $sql);
              $resultCheck = mysqli_num_rows($result);

              if($resultCheck > 0) {
                while($row = mysqli_fetch_assoc($result)){
                    echo '
                    <h1>'.$row['EVENT_NAME'].'</h1>
                    <tr>
                        <td><h5>Venue</h5></td>
                        <td><h5>Date</h5></td>
                        <td><h5>Time</h5></td>
                        <td><h5>Joining Price</h5></td>
                        <td><h5>Seats left</h5></td>
                    </tr>
                    <tr>
                        <td><h5>'.$row['EVENT_VENUE'].'</h5></td>
                        <td><h5>'.$row['START_DATE'].' - '.$row['END_DATE'].'</h5></td>
                        <td><h5>'.$row['START_TIME'].'-'.$row['END_TIME'].'</h5></td>
                        <td><h5>'.$row['TICKET_PRICE'].'</h5></td>
                        <td><h5>'.$row['SEATS_AVAILABILITY'].'</h5></td>
                    </tr>
                    </table>
                </p>

                <form id="form" style="font-size: 15px;">
                    <fieldset class="form">
                        <legend>Please fill this form:</legend>
                        <label for="name">Enter your name (same as IC) : </label>
                        <input type="text" class="a" id="name"></br><br>

                        <label for="studentId">Enter your student ID : </label>
                        <input type="text" class="a" id="studentId"></br></br>

                        <label for="email">Enter your email: </label>
                        <input type="text" class="a" id="email"></br></br>
                        
                        <label for="phoneNumber">Enter your phone number: </label>
                        <input type="number" class="a" id="phoneNumber"></br></br>

                        Select your preferred payment method:</br>
                        <input type="radio" id="type1" name="payment" value="eWallet">
                        <label for="type1">E-wallet</label></br>

                        <input type="radio" id="type2" name="payment" value="onlineBanking">
                        <label for="type2">Online banking</label></br>

                        <input type="radio" id="type3" name="payment" value="creditCard">
                        <label for="type3">Credit card</label></br></br>

                    <button type="button" onclick="window.location.href="records.php"">Submit</button>
                    </fieldset>  
                    </form>
                    
                    <div style="padding: 10px;margin-left: 20%; margin-right: 20%; border: 1px dotted black;">
                        <h3 align="center">Terms and conditions: </h3>
                        <p align="left">
                        '.$row['TERMS'].'
                        </p>
                    </div>
                ';
                }
            }
        ?>

What I am actually trying to do is to make the booking page display the correct information within the table and the ‘Terms and Conditions’ for each event by getting the data from the database. I’ve tried few methods but all didn’t seems to work.

Advertisement

Answer

Changes you need, where you are creating the button.

<input type="hidden" name="eventId" value="'.$row['EVENT_ID'].'">

The above code will set the value of the hidden field with the EVENT_ID field in your database. Use this eventId in the next php page to fetch the exact (single) row from the database.

$eid = $_POST["eventId"];
$eid = addslashes($eid);
$sql = "SELECT * from events WHERE `EVENT_ID`='".$eid."';";

Using the above sql, you will get the data corresponding to the EVENT_ID. Remove the while loop and populate the fields with the fetched data.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement