Hello there. Sorry that I don't really know much about drop down button in php. I was assign to do a booking system as my final project but somehow I don't know how to make the button works. It suppost to be like this. The user needs to choose which school he is going to book the locker. And after he has choosen, the numbers of lockers will be listed down and he can choose which one that he wants. But the problem here is, the value from drop down button seems like it can't pass the value from the database. Here is the codes :

<?php 

session_start();

$user = $_SESSION["user_id"];
$pass = $_SESSION["password"];

?>

<form name="booking" method="post" action="booking.php">
    <table width="213" border="0">     
       <tr>
            <td width="300" height="34">Select School</td>
      </tr>
      <tr>
            <td><select name="school">
            <option value="1">SMK Seri Pantai</option>            
            <option value="2">SMK Tengku Sulaiman</option>
            <option value="3">SMK Convent Butterworth</option>
            <option value="4">SMK Dato' Megat Khas</option>
            </select>
            </td>
      </tr>
      <tr>
            <td width="10"> </td>
            <td><input name="submit" type="submit" value="Next">
            <input name="next" type="hidden" id="next"></td>
       </tr>        
    </table>
</form>

I seperated this to so that it won't confuse me much.

<?php 
session_start();

$user = $_SESSION["user_id"];
$pass = $_SESSION["password"];

$conn = mysql_connect("localhost", "root", "") or die ("Could not connect to server");
$db = mysql_select_db("Smartlocker", $conn) or die ("Could not connect to database");
$sql = "SELECT * FROM locker WHERE school='$next' ";
$result = mysql_query($sql) or die ("Could not execute query");
$myrow=mysql_fetch_row($result);

if($myrow=mysql_fetch_row($result))
{
echo "

    <table border='1' bordercolor='C0F7FE'>
        <tr>
            <td><center><strong>School</strong></center></td>
            <td><center><strong>Locker No.</strong></center></td>
            <td><center><strong>Price (RM)</strong></center></td>
            <td><center><strong>Status</strong></center></td>
            <td><center><strong>Book</strong></center></td>
        </tr>";

    do{
    echo "
        <tr>
            <td>$myrow[2]</td>
            <td>$myrow[3]</td>
            <td>$myrow[5]</td>
            <td></td> //I haven't don't this part yet
            <td><form name='booked' action='booking-process.php' method='post'>
                <input type='submit' name='submit' value='Book'>
                <input name='book' type='hidden' id='booked' value='$myrow[0]'> //$myrow[0] is my primary key                
                </form></td>  
        </tr>";

    }while($myrow=mysql_fetch_row($result));

echo "</table>";
}
?>

I hope someone can help me with this. :(

Recommended Answers

All 12 Replies

Please share what you get when you do

ehco "<pre>;
print_r($_POST); 
echo "</pre>";"

sorry but what do you mean by that?

maybe what baig772 sayin is insert those lines he post from booking.php.

as i cant see you only use $next. maybe @ first should be line this

$next=$_POST['next']

but does not make since cause the school values has
diffirent name so it should be like this

$next=$_POST['school']

should be receiving an int
and for this line

$sql = "SELECT * FROM locker WHERE school='$next' "; 

school column should contains number since the value of you option
are numbers

 <option value="1">SMK Seri Pantai</option>
 <option value="2">SMK Tengku Sulaiman</option>

okay thanks! I got it. :D but the first data didn't appear. What I mean is, okay it's like this. The data in the database for option 1 that is SMK Seri Pantai, there are 10 data. but it only display 9 data. Do you think there is something missing in the code? =/

try using while loop instead of do while i guess =)

maybe this help:

index.php

<?php
session_start();
$user = $_SESSION["user_id"];
$pass = $_SESSION["password"];
?>

<form name="booking" method="post" action="booking.php">
    <table width="213" border="0">     
       <tr>
            <td width="300" height="34">Select School</td>
      </tr>
      <tr>
            <td><select name="school">
            <option value="1">SMK Seri Pantai</option>            
            <option value="2">SMK Tengku Sulaiman</option>
            <option value="3">SMK Convent Butterworth</option>
            <option value="4">SMK Dato' Megat Khas</option>
            </select>
            </td>
      </tr>
      <tr>
            <td width="10"> </td>
            <td><input name="submit" type="submit" value="Next">
            <input name="next" type="hidden" id="next"></td>
       </tr>        
    </table>
</form>

2.booking.php

session_start();
$user = $_SESSION["user_id"];
$pass = $_SESSION["password"];

<?php
require_once("../includes/config.php");
require_once("../includes/dbconnect.php");

if (isset($_POST['submit'])) {
    $school = $_POST['school'];

    $sql = mysql_query("SELECT * FROM locker WHERE school_id = '$school'") or die(mysql_error());
    ?>
    <table border='1' bordercolor='C0F7FE'>
        <tr>
            <td><center><strong>School</strong></center></td>
            <td><center><strong>Locker No.</strong></center></td>
            <td><center><strong>Price (RM)</strong></center></td>
            <td><center><strong>Status</strong></center></td>
            <td><center><strong>Book</strong></center></td>
        </tr>
        <?php
        if (mysql_num_rows($sql) > 0) {
            while ($row = mysql_fetch_array($sql)) {
                $looker_id = $row['id'];
                $name = $row['name'];
                ?>
                <tr>
                    <td><?= $school ?> //school id, related from database</td>
                    <td><?= $name ?></td>
                    <td>// Price</td>
                    <td>// Status</td>
                    <td>
                        <form action='booking-process.php' method='post'>
                            <input type='submit' name='submit' value='Book'>
                            <input name='book' type='hidden' value='<?= $looker_id ?>'>          
                        </form>
                    </td>  
                </tr>
                <?php
            }
        } else {
            ?>
            <tr><td colspan="5">No data</td></tr>
            <?php
        }
        ?>
    </table>

    <?php
}
else{
    echo "No data";
}
?>

code739 :
while? the data shown is even lesser.

dean8710:
sorry but I don't get what does require_once("../includes/config.php");and require_once("../includes/dbconnect.php"); does. before what I learn is really basic. What is requires inside this includes?

@hudhasama

I notice your fetching twice $myrow=mysql_fetch_row($result); as you begin displaying rows

from line 11,13,39

from ther first fetch you got the first row but
the second fetch is on the if condition as the second fetch goes it fetches the second
row
then it enters the do while loop and display the second row instead of first row as what you expects

the displaying of query starts from the second row sinse you are fetching twice as it begin.

my advice is you can remove the line 11 fetcher =)

sorry but I don't get what does require_once("../includes/config.php");and require_once("../includes/dbconnect.php"); does. before what I learn is really basic. What is requires inside this includes?

to answer your question require_once is just the info of your database and server connection which is in your code the $conn = mysql_connect("localhost", "root", "") or die ("Could not connect to server"); and $db = mysql_select_db("Smartlocker", $conn) or die ("Could not connect to database"); part

that should be ok

@code739 : Okay I got it.. Thank you!! :D oh my. feel like crying. hihi. Thanks again!

@ehpratah : yea. I finally realize it after I post this. hehe. sorry. and thanks! :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.