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.

Recommended Answers

All 3 Replies

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 :).

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 "<b>Work Site:</b><select name='site'>";
while ($rows = mysql_fetch_array($result)){
echo "<option>".$rows."</option>";
}//end while
echo "</select>";
__________________

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"]); ?>
<input type="text" name="site" value="<?php echo $site; ?>" size="30">
__________________

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.

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>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.