Selecting item from populated dropdown list and displaying results from database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Panna
    New Member
    • Jul 2007
    • 4

    Selecting item from populated dropdown list and displaying results from database

    Hey there

    I am using phpMyAdmin and MySQL to develop a dbase application.

    I have a dropdown menu on one of the pages which gets populated automatically with data from a table in the database. The code for this is as follows:

    [code=php]$sql3="SELECT ID, UploadDate FROM Telkom";
    $result2=mysql_ query($sql3);
    $options="";

    while ($row=mysql_fet ch_array($resul t2)) {

    $id=$row["ID"];
    $date=$row["UploadDate "];
    $options.="<OPT ION VALUE=\"$id\">" .$date;

    The code for the actual dropdown menu is as follows:

    <form name="selectdat a" action="select_ telkom_data.php " method="post">
    <SELECT NAME="thedate">
    <OPTION>Choos e
    <?=$options?>
    </SELECT>
    <input type="submit" name="Submit" value="Go">
    </form>

    As you can see there is an additional php script (select_telkom_ data.php). This script is supposed to check the $date variable against the UploadDate table and then return the correct record based on the date the user has selected in the dropdown menu.

    The code for select_telkom_d ata.php looks like this:

    <?php

    include "admin/dataw.php";
    include("global .inc.php");
    $sysadminemail = "webmaster@dtms i.org";

    process_login() ;
    die();


    function process_login() {
    global $dbhost;
    global $dbuser;
    global $dbpass;
    global $db;

    // define homepage and text variables
    global $homepage;
    global $homedir;
    global $sysadminemail;
    global $userstable;


    $date=$_POST['thedate'];
    $link = mysql_connect(" $dbhost", "$dbuser", "$dbpass")
    or die('Could not connect: ' . mysql_error());
    echo 'Connected successfully';
    mysql_select_db ("$db") or die('Could not select database');

    $query = "SELECT * FROM Telkom WHERE UploadDate='$da te' ";
    $result = mysql_query($qu ery) or die('Query failed: ' . mysql_error());


    $num_rows = mysql_num_rows( $result);
    $row = mysql_fetch_arr ay($result);


    // test -- does the date exist
    if ($num_rows < 1) {
    print "<HTML>";
    print "<HEAD>";
    print "<TITLE>";
    print "Error";
    print "</TITLE>";
    print "<BODY>";
    print "<CENTER>";
    print "<B><CENTER>Sor ry - no records exist. ";


    }
    else {


    include("show_t elkom_data.php" );

    mysql_close($li nk);
    }


    }

    ?>[/code]

    [Please use CODE tags when posting source code. Thanks! --pbmods]

    The script connects to the database, but it doesn't return any records from the Telkom table. In other words, all I get is the message 'Sorry - no records exist'.

    What am I doing wrong?

    Thanks guys!
    Panna
  • kovik
    Recognized Expert Top Contributor
    • Jun 2007
    • 1044

    #2
    The value that you are getting from your selection is not the UploadDate, it's the id. The UploadDate is just the label of the option, not the value of it.

    if you print_r($_POST) , you'll see the values that are being passed from the form.

    Comment

    • Panna
      New Member
      • Jul 2007
      • 4

      #3
      Originally posted by volectricity
      The value that you are getting from your selection is not the UploadDate, it's the id. The UploadDate is just the label of the option, not the value of it.

      if you print_r($_POST) , you'll see the values that are being passed from the form.
      ok, thanks very much! Just one more question though: I'm not entirely sure what to change to make it work. I would REALLY appreciate if you could help me out one more time.
      shot!

      Comment

      • kovik
        Recognized Expert Top Contributor
        • Jun 2007
        • 1044

        #4
        Think about it.


        What condition are you using to get the data from your database?
        What values are you provided to get that data?

        Like I said, use print_r($_POST) and figure it out. ^_^

        Comment

        • Panna
          New Member
          • Jul 2007
          • 4

          #5
          hey, thanks for all your help! I managed to figure it out up to a point where the correct records get selected and displayed.
          panna

          Comment

          Working...