...

View Full Version : Help with drop down results!



emuse
08-09-2007, 10:23 PM
Ok, So I have been on here before and you guys are great help! I am coming here once again for help, lol.

Currently I am pretty new at php/mysql. So please bare with me. Currently I am trying to create a drop down menu of a list of people, and when I click on it I want it to show all the information to that specific person. Here is what I have written so far.


<?php

// Make a MySQL Connection

// Construct our join query

$con = mysql_connect("localhost", "root", "blah");

if (!$con)

{

die('Could not connect: ' . mysql_error());



}mysql_select_db("Bus", $con);





$query = "SELECT drivers.last_name, sales.meals, sales.date, sales.bus, sales.sales,

sales.percent ".

"FROM drivers, sales ".

"WHERE drivers.PID = sales.drivers_DID";



$result = mysql_query($query) or die(mysql_error());

//pick the driver to view results.
echo "<select name=\"drivers_DID\" id=\"drivers_DID\">";
echo "<option>Choose Driver:</option>";

//Driver drop down while loop.
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{

echo "<option value=\"{$row['PID']}\">{$row['last_name']},&nbsp{$row['first_name']}</option>";
}
echo "</select><br />";
echo '<br></br>';


//Table Headers for driver sales report.

echo '<center><table><thead>

<tr>

<th width=\"col\">DATE</th>

<th width=\"col\">TYPE</th>

<th scope=\"col\">BUS</th>

<th scope=\"col\">SALES</th>

<th scope=\"col\">%</th>

<th scope=\"col\">MEALS</th>

<th scope=\"col\">COMMISION</th>

<th scope=\"col\">PAYMENT</th>

<th scope=\"col\">BALANCE DUE</th>

</center></tr></thead>';



// Driver Sales Report.

while($row = mysql_fetch_array($result)){

echo "<tbody><td align=\"center\" width=\"10%\">";

echo $row['date']. " </td><td align=\"center\" width=\"6%\"> ". $row['bus']. " </td><td align=\"center\" width=\"6%\"> "

. "$". $row['sales']. " </td><td align=\"center\" width=\"6%\"> " . $row['percent']

. " </td><td align=\"center\" width=\"6%\"> " . $row['meals'] . " </td><td align=\"center\" width=\"6%\"> " . "$". $row['sales']*0.05 ;

echo "</td><tr>";

}

echo '</tr></tbody>

</table>';

?>

Cuase currently it shows all information for all people aka drivers. So any help would be very aprecative!:thumbsup:

mcjwb
08-09-2007, 11:44 PM
I think you'll find the code you posted doesn't work! Perhaps you've cut and pasted it wrong?

Anyway I've done my best to second quess the little details of your script so apologies if they are wrong, the main code/idea should work though. That said I haven't tested it!


<?php
echo "<html><head><title>Test Page</title></head><body>";
// Make a MySQL Connection
// Construct our join query
$con = mysql_connect("localhost", "root", "blah");
if (!$con){
die('Could not connect: ' . mysql_error());
}
mysql_select_db("Bus", $con);
$query = "SELECT drivers.PID, drivers.last_name, drivers.first_name "
."FROM drivers";
$result = mysql_query($query) or die(mysql_error());
echo "<form action=\"$_SERVER['PHP_SELF']\" method=\"post\">"
//pick the driver to view results.
echo "<select name=\"drivers_DID\" id=\"drivers_DID\">";
echo "<option>Choose Driver:</option>";
//Driver drop down while loop.
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo "<option value=\"{$row['PID']}\">{$row['last_name']},&nbsp{$row['first_name']}</option>";
}
echo "</select><br />";
echo "<input type=\"submit\">";
echo '<br></br>';
echo "</form>";
//Table Headers for driver sales report.
if(isset($_POST['drivers_DID'])){
//IMPORTANT check data received from form before using it (research SQL Injection Attacks), for example:
if(!is_numeric($_POST['drivers_DID']){
die("Invalid Driver ID submitted!");
}
$query = "SELECT sales.meals, sales.date, sales.bus, sales.sales, sales.percent "
."FROM drivers, sales "
."WHERE drivers.PID = sales.drivers_DID "
."AND drivers.PID = '".$_POST['drivers_DID']."'";
$result = mysql_query($query) or die(mysql_error());
echo '<center><table><thead>
<tr>
<th width=\"col\">DATE</th>
<th width=\"col\">TYPE</th>
<th scope=\"col\">BUS</th>
<th scope=\"col\">SALES</th>
<th scope=\"col\">%</th>
<th scope=\"col\">MEALS</th>
<th scope=\"col\">COMMISION</th>
<th scope=\"col\">PAYMENT</th>
<th scope=\"col\">BALANCE DUE</th>
</tr>
</thead>';
// Driver Sales Report.
echo "<tbody>";
while($row = mysql_fetch_array($result)){
echo "<tr>"
."<td align=\"center\" width=\"10%\"> ". $row['date']. " </td>"
."<td align=\"center\" width=\"6%\"> " . $row['bus']. " </td>"
."<td align=\"center\" width=\"6%\"> " . "$". $row['sales']. " </td>"
."<td align=\"center\" width=\"6%\"> " . $row['percent'] . " </td>"
."<td align=\"center\" width=\"6%\"> " . $row['meals'] . " </td>"
."<td align=\"center\" width=\"6%\"> " . "$". $row['sales']*0.05 ." </td>"
."<tr>";
}
echo '</tbody></table></center>';
}
echo "</body></html>";
?>

emuse
08-11-2007, 11:26 PM
Well, That should have worked but for some reason all, I get is a blank page. That is the idea though (thanks so much) Basicly the drop down menue show the driver names from the drivers table and when click on it, it should show everything according do that drivers is aka DID from the sales table. If that makes sense? It could just be something misstyped in the code, so I will try to keep checking it over, but still much help would be greatly appreciated!:thumbsup:

mcjwb
08-11-2007, 11:48 PM
Found the mistakes!

This line:

echo "<form action=\"$_SERVER['PHP_SELF']\" method=\"post\">"
should be:

echo "<form action=\"$_SERVER[PHP_SELF]\" method=\"post\">";
(no quotes aroung PHP_SELF and a semicolon at the end.)

And this line:

if(!is_numeric($_POST['drivers_DID']){
should be

if(!is_numeric($_POST['drivers_DID'])){
(missing a closing round braket,). )

I think that's it! Sorry I missed those first time round.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum