I am trying to set up a user edit page. I have a query that selects the specific user I want to edit. I also have a query that grabs the companies from the database that I iterate as options in a SELECT field.
I want to have the OPTION in the company field automatically selected. I do this all the time in single database uses. How do I use the company value from the user query to automatically select the correct company during the user edit?
Fields:
users->company = company->id
User query:
$query='SELECT * FROM users WHERE id=:id';
Company query:
$query2='SELECT id,companyname FROM companies';
Company field:
<select name="company" required>
<?php while ($row=$companylist->fetch(PDO::FETCH_ASSOC)) {extract($row); ?>
<option value="<?php echo $id; ?>"><?php echo $companyname; ?></option>
<?php } ?>
</select>
2
<select name="company" required>
<?php while ($row=$companylist->fetch(PDO::FETCH_ASSOC)) {extract($row); ?>
<option value="<?php echo $id; ?>"
<?php if($users->company == $id){ ?> selected <?php } ?> > <?php echo $companyname; ?></option>
<?php } ?>
</select>
The way you set up the text to select the company that is already associated with the user is to do the following. Pull down the user information, see if the users company id matches the companies id in your while statement. if it does add the attribute selected to that option.
2