1 Replies - 44680 Views - Last Post: 13 March 2013 - 12:33 AM Rate Topic: -----

#1 clfc_developer02   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 12-March 13

dropdown list generated from mysql use to retrieve data from database

Posted 12 March 2013 - 11:41 PM

Hi everyone! I'm a newbie in php. I'm having a trouble with displaying data from MySQL through drop down list generated from MySQL. I can generate a dropdown list from MySQL. However, I couldn't use that data to perform another query in MySQL. I know I'm a bit confusing, but could you help me figure out what's wrong / lacking in my codes. Below is my code that generates the dropdown list from MySQL:

<form action="viewemp_branch2.php" method="post">

$db = mysql_connect('localhost', 'root', '');
mysql_select_db('eis', $db) or die (mysql_error());
$query = "select bname from branches ORDER BY bid ASC";

$result = mysql_query($query) or die (mysql_error());
echo '<select name="bname">';
while ($row = mysql_fetch_assoc($result)) {
    echo "<option value='" . $row['bname'] ."'>" . $row['bname'] ."</option>"; 
echo '</select>';
echo '<br /><br /><input type="submit" name="SUBMIT" value="Submit" />
      <input type="reset" name="RESET" value="Cancel" />'; 

The following codes is my viewemp_branch2.php

$bname = $_POST['bname'];

$db = mysql_connect('localhost', 'root', '');
mysql_select_db('eis', $db) or die (mysql_error());

$query = "SELECT employee.fname, employee.lname, employee.mname,
            (SELECT empposition.description FROM empposition WHERE empposition.poscode=empstatlog.designation) AS position,
            (SELECT empstatlog.empstatdate FROM empstatlog WHERE empstatlog.empid = employee.empid ORDER BY empstatlog.empstatid ASC LIMIT 1) AS regularized,
            (SELECT empcompenlog.basic FROM empcompenlog WHERE empcompenlog.empid=employee.empid ORDER BY empcompenlog.compenid DESC LIMIT 1) AS basic,
          FROM employee
          INNER JOIN empstatlog ON (employee.empid = empstatlog.empid)
          INNER JOIN empposition ON (empstatlog.designation = empposition.poscode)
          INNER JOIN empcompenlog ON (empcompenlog.empid = employee.empid)
          WHERE empstatlog.branch =  '".mysql_real_escape_string($bname)."' 
          GROUP BY employee.empid";

$result = mysql_query($query, $db) or die (mysql_error());
$num_result = mysql_num_rows($result);

if($num_result == 1) {
    echo '<b>ERROR</b>'; 
    while ($row = mysql_fetch_row($result)) {
        echo '<tr>';
        echo '<td>' .$row[0]. '</td>';
        echo '<td>' .$row[1]. '</td>';
        echo '<td>' .$row[2]. '</td>';
        echo '<td>' .$row[3]. '</td>';
        echo '<td>' .$row[4]. '</td>';
        echo '<td>' .$row[5]. '</td>';
        echo '<td>' .$row[6]. '</td>';
    echo '</table>';
    echo '<td><tr><table><br /><br />';

I get nothing from the above codes. What’s wrong with them? Please help me..Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: dropdown list generated from mysql use to retrieve data from database

#2 Takk   User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 111
  • Joined: 08-March 13

Re: dropdown list generated from mysql use to retrieve data from database

Posted 13 March 2013 - 12:33 AM

Without doing too much digging into your code, what I would do is switch the form method to GET so you can see what you're working with and do a print on the SQL statement once it's generated.. if the problem doesn't pop out at you then, take the query and run it inside of phpmyadmin and see if that turns up anything useful.

If you're still having problems, start printing variable values along the way and see where you lose what you're looking for, right off it looks like you should be checking to see that num_results=0 instead of 1 in order to raise an error, and you're missing an 'else' in that same if-then block, this could even be the sum of your issues.

And yes my paragraphs are backwards, but sue me, it's late ;) hope this helps.

And just a side note because I scrolled back up, look into the mySQL 'AS' keyword, it may help you to keep those row[] references from your loop a bit more readable once things get more hectic als the project grows and things get moved around.

This post has been edited by Takk: 13 March 2013 - 12:36 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1