954,580 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Using data from database to set select value

I'm an old Access programmer trying to get something I use there frequently to work in PHP/MySQL.
I want to look up data in a database and then have that appear as the value in a select statement on an update form.
The issue is selecting a new supervisor for an employee. I use a random number to keep the supervisors straight, so I don't want someone having to remember that. When the staff member's record is accessed, I want the form to display the current information in the database, which can then be updated as needed. This works great just using text boxes but when I've tried to do it with Select statements it always reverts to the first record from the database, not the one for that person, forcing the data to be re-entered each time.
Any help will be appreciated.

martymaven
Newbie Poster
5 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

Hi martymaven,

Can we please see at least a sample of the codes that you have..

e.g. $query =("SELECT * FROM employee WHERE id = '".$some_id."' LIMIT 1") ;

Something like that or your employee table structure such as

id | name | department | position | rate | supervisor | personal info 1 | personal info. 2 |

something very similar to the above can truly get people here going at it :).

veedeoo
Posting Pro in Training
438 posts since Oct 2011
Reputation Points: 149
Solved Threads: 60
 

I think the problem is I don't have any code.

I frequently use constructions like the following in forms used to enter new records:
__________________
$result = mysql_query("SELECT * FROM phones group by site order by site");
echo "Work Site:";
while ($rows = mysql_fetch_array($result)){
echo "".$rows['site']."";
}//end while
echo "";
__________________

What I'd like to do is provide users with the ability to select, in this case, the site on an update screen. When I've attempted that in the past, the code above wipes out the data already in the database, defaulting the value to the first item on the list.

Code like this:
__________________
Work Site:
<? $site = stripslashes($myrow["site"]); ?>

__________________

Picks up the value in the database and displays it, but does not provide the ability to change the data using a Select with a list of values in the database.

martymaven
Newbie Poster
5 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

I kept looking and found a solution to my own question.

Here's the code that works great:

<tr><td>
<?php
$selsuper = stripslashes($myrow["supervisor"]);
$result1 = mysql_query("SELECT phid as phid1, lastname as last1, firstname as first1 FROM phones order by lastname, firstname");
echo "<b>Supervisor:</b></td><td><select class='inputa' name='supervisor'>";
echo "<option></option>";
while ($row1 = mysql_fetch_assoc($result1)){
print "<option";
    if($row1['phid1'] == $selsuper)
    {
    echo ' selected';
    }
    ?> value="<? echo $row1['phid1']; ?>"><? echo $row1['last1'],', ', $row1['first1']; ?></option>
    <?
    }
    ?>
</select>
</td>
martymaven
Newbie Poster
5 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: