Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jul 2007
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Help with drop down results!

    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 Code:
    <?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($resultMYSQL_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!

  • #2
    Regular Coder
    Join Date
    Jul 2007
    Location
    UK
    Posts
    223
    Thanks
    0
    Thanked 14 Times in 14 Posts
    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 Code:
    <?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($resultMYSQL_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>";
    ?>

  • #3
    New Coder
    Join Date
    Jul 2007
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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!

  • #4
    Regular Coder
    Join Date
    Jul 2007
    Location
    UK
    Posts
    223
    Thanks
    0
    Thanked 14 Times in 14 Posts
    Found the mistakes!

    This line:
    PHP Code:
    echo "<form action=\"$_SERVER['PHP_SELF']\" method=\"post\">" 
    should be:
    PHP Code:
    echo "<form action=\"$_SERVER[PHP_SELF]\" method=\"post\">"
    (no quotes aroung PHP_SELF and a semicolon at the end.)

    And this line:
    PHP Code:
     if(!is_numeric($_POST['drivers_DID']){ 
    should be
    PHP Code:
     if(!is_numeric($_POST['drivers_DID'])){ 
    (missing a closing round braket,). )

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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •