I have four database tables

locality table: localityid, locality.

jobtype table: typeid, type.

applywork table: id, userid, applyfor applicationsent, jobtypeid, vacancyseenon companyname, doorno buildingname, street, localityid, postcode, telephone, website, email, notes

users: id, role, name, surname, gender, username, password.

I have a page to edit selected field from applywork. I made two dropdowns, one of them used to display the localities and the other one is to display the job types. in these two dropdowns shows all localities and job types but my problem is how to display the locality and jobtype saved in applywork table first if the user didn't change the job type and locality but if the user wish to change it, select other localities and jobtype from same dropdowns. if it is possible anyone help me to make it?

This is my code using form to make an update(the id of user and applywork are get from another page) exsuse me if the indentation is not good:

<?php 
    require("../headerloggedin.php"); 
?>
<div id="content">
<?php	
    if(!empty($_GET["editid"]) && !empty($_GET["edituserid"]))
    {
        $applyid = $_GET["editid"];
        $idofuser = $_GET["edituserid"];
		
        $query = "SELECT * FROM applywork WHERE id = $applyid AND userid = $idofuser";
        $resultset = $db->query($query);
		
        while($row = mysql_fetch_array($resultset))
        {
?>
            <form method="post" name="vacancies" action="updateapplication.php">
                <input type="hidden" name="editid" value="<?php echo $applyid;?>"/>
                <input type="hidden" name="idofuser" value="<?php echo $idofuser;?>"/>
                <table>	
	           <tr>
                	      <td>Vacancy:</td> 
                        <td><input type="text" name="applyfor" value="<?php echo $row["applyfor"]; ?>" /></td>
                	  </tr>    
                    <tr>
                	      <td>Vacancy seen on:</td> 
                        <td><input type="text" name="vacancyseenon" value="<?php echo $row["vacancyseenon"]; ?>"  /></td>
		  </tr>
                    <tr>
                	      <td>Application sent on:</td> 
                        <td><input type="text" name="applicationsent" value="<?php echo $row["applicationsent"]; ?>"  /></td>
		  </tr>
                    <tr>
                	      <td>Company Name:</td> 
                        <td><input type="text" name="companyname" value="<?php echo $row["companyname"]; ?>"  /></td>
	           </tr>
                    <tr>
                        <td>Door Number:</td> 
                        <td><input type="text" name="doorno" value="<?php echo $row["doorno"]; ?>" /></td>
		  </tr>
                    <tr>
                	      <td>Building Name:</td> 
                        <td><input type="text" name="building" value="<?php echo $row["buildingname"]; ?>" /></td>
	           </tr>
                    <tr>
                	      <td>Street:</td> 
                        <td><input type="text" name="street" value="<?php echo $row["street"]; ?>"  /></td>
		  </tr>
                    <tr>
                	      <td>Postcode:</td> 
                        <td><input type="text" name="postcode" value="<?php echo $row["postcode"]; ?>"  /></td>
		  </tr>
                    <tr>
                	      <td>Telephone:</td> 
                        <td><input type="text" name="telephone" value="<?php echo $row["telephone"]; ?>"  /></td>
		  </tr>
                    <tr>
                	      <td>Website:</td> 
                        <td><input type="text" name="website" value="<?php echo $row["website"]; ?>" /></td>
		  </tr>
                    <tr>
                	      <td>Email:</td> 
                        <td><input type="text" name="email" value="<?php echo $row["email"]; ?>" /></td>
		  </tr>
                    <tr>
                	      <td>Notes:</td> 
                        <td><textarea cols="40" rows="5" name="notes" value="notes" ><?php echo $row["notes"]; ?></textarea></td>
		  </tr>
                    <tr>
                	      <td>Job type:</td> 
                        <td>
                            <select name="typeid">
  			 <?php
			     $query = "SELECT * FROM jobtype";
			     $resultset = $db->query($query); 
			     while ($row = mysql_fetch_array($resultset)) 
	                       {
	                    ?>
                                    <option value="<?php echo $row["typeid"]; ?>"><?php echo $row["type"] ?></option>
  	                    <?php 									     } 
	                    ?>     
    	                    </select>
                             <br />
	               </td>
                    </tr>
                    <tr>
                        <td>Locality:</td> 
                        <td>
                            <select name="localityid">
                            <?php
                                $query = "SELECT * FROM locality";
                                $result = $db->query($query);
	                       while ($row = mysql_fetch_array($result))
	                       {
	                   ?>
                                    <option value="<?php echo $row['localityid'];?>"><?php echo $row['locality']?></option>
                            <?php
	                       }
	                   ?>
	                   </select>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2"><input type="submit" value="Edit Details" /></td>
                    </tr>
                </table>
	   </form>
        <?php	
            }
        }
        ?>
        <br />	
        <a href="editapplication.php">Go to main menu.</a>
   </div>
<?php require("../footerloggedin.php"); ?>

From this section
change lines 8, 11, 25, 28

<tr>
                	      <td>Job type:</td> 
                        <td>
                            <select name="typeid">
  			 <?php
			     $query = "SELECT * FROM jobtype";
			     $resultset = $db->query($query); 
			     while ($jrow = mysql_fetch_array($resultset)) 
	                       {
	                    ?>
                                    <option value="<?php echo $jrow["typeid"]; ?>" <?php if($jrow['typeid']==$row['jobtypeid']){ echo 'selected="selected"';} ?>><?php echo $jrow["type"] ?></option>
  	                    <?php 									     } 
	                    ?>     
    	                    </select>
                             <br />
	               </td>
                    </tr>
                    <tr>
                        <td>Locality:</td> 
                        <td>
                            <select name="localityid">
                            <?php
                                $query = "SELECT * FROM locality";
                                $result = $db->query($query);
	                       while ($lrow = mysql_fetch_array($result))
	                       {
	                   ?>
                                    <option value="<?php echo $lrow['localityid'];?>" <?php if($lrow['localityid']==$row['localityid']){ echo 'selected="selected"';} ?>><?php echo $lrow['locality']?></option>
                            <?php
	                       }
	                   ?>
	                   </select>
                        </td>
                    </tr>

Hi,

I make this code you gave me but didn't work unfortunatly. for example if the locality of the specific application is saved mriehel, I wish to display mriehel first and complete with the localities saved in locality table in option tag because if the user didn't change the locality, the application update the same locality. With my and your code pasted above, if the application saved with specific locality such as mriehel, the option shows me the first locality saved in the locality database not the locality saved in applywork table first.

Exsuse me if I don't explaine clearly my problem.

*I made this code but same problem

With my code, I'm assuming that the saved locality value exists in the locality table and that value should be selected in the select box regardless of where it shows in the list.

If the previously saved value does not exist in the table it can not match and therefore the first value in the select box will be displayed by default.

If you want to always display your previously saved value as the first in the list, whether it exists in the lookup table or not you can try:

<tr>
                        <td>Locality:</td> 
                        <td>
                            <select name="localityid">
                                <option value="<?php echo $row['localityid'];?>" selected="selected"><?php echo $row['locality']?></option>
                            <?php
                                $query = "SELECT * FROM locality";
                                $result = $db->query($query);
	                       while ($lrow = mysql_fetch_array($result))
	                       {
	                       		 if($lrow['localityid']!=$row['localityid'])
	                       		 {	
	                   ?>
                                <option value="<?php echo $lrow['localityid'];?>"><?php echo $lrow['locality']?></option>
                            <?php
	                       		 }
	                       }
	                   ?>
	                   </select>
                        </td>
                    </tr>

are you using a php mysql class scripts? if not then change:

$resultset = $db->query($query); 
to:
 	$resultset = mysql_query ($query);

if you are then you need to do something like this:

$db = new MySQL( $data ); $resultset = $db->query($query);

thanks lethargicCoder. works good your latest code with my application. Cleggy1987 why I am using mysql_query instead of $db->query ?

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.