0

I would like to prevent drop-down option duplicate in my edit page..

This is my query:

$res= mysql_query ("SELECT * FROM Acc_skills
INNER JOIN Accounts ON 
Accounts.Role_ID = Acc_skills.FK_Role_ID 
INNER JOIN Skills ON 
Skills.Skills_id = Acc_skills.FK_Skills_ID
WHERE Acc_skills.Acc_skills_id = '$id'");
$row = mysql_fetch_array($res); 

Here is my code:

<?php 
echo '<option value= '.$row[3].' selected="selected">'.$row[3].' </option>';
echo '<option id="Low" value="Low">Low</option>
      <option id="Average" value="Average">Average</option>
      <option value="High">High</option>
</select>';
?>

I have managed to pull out the skill level data from my database (Eg, High), however, it shows duplicate drop down option now (Eg, there are 2 "High" options now). As the skill level option is manually typed in, how do i prevent having duplicate drop down options?

Thanks in advance!

7
  • then exclude the "High" level skill in your query Commented Jan 26, 2016 at 5:30
  • what is the value of $row[3]? Commented Jan 26, 2016 at 5:32
  • 2
    SELECT DISTINCT...? Commented Jan 26, 2016 at 5:34
  • The value of $row[3] is the skill level of the current record selected from the database, it can be high, average or low depending on the database record Commented Jan 26, 2016 at 5:54
  • and than u want to use selected="" according the database value right? Commented Jan 26, 2016 at 6:11

2 Answers 2

2

As per your comments, you need to use selected="" option as per database value. you can use like that:

<select>
<option <?=($row[3] == 'Low' ? 'selected=""' : '')?> id="Low" value="Low">Low</option>
<option <?=($row[3] == 'Average' ? 'selected=""' : '')?> id="Average" value="Average">Average</option>
<option <?=($row[3] == 'High' ? 'selected=""' : '')?> value="High">High</option>
</select>

Explanation:

If $row[3] is equal to "Average" or "High" or "Low" than select related option menu and add selected="" attribute in <option> tag.

Side Note:

Please use mysqli_* or PDO extensions becuase mysql_* extension is deprecated and not available in PHP 7.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! i got it now! Really appreciate it!
1

first of all:

1- Please travel from mysql into mysqli or PDO , i suggest PDO.

2- if duplicated data in your database use "SELECT DISTINCT" for fetching result.

3-if you have static option try like this:

<?php 
echo "<select>";
echo '<option value= '.$row[3].' selected="selected">'.$row[3].' </option>';
echo '<option id="Low" value="Low">Low</option>
      <option id="Average" value="Average">Average</option>
      <option value="High">High</option>
</select>';
?>

or if you have fetching all rows act like this:

 echo "<select>";
    echo "<option id=Low value=Low >Low</option>
          <option id=Average value=Average>Average</option>
          <option value=High>High</option>";
        foreach($result as $index){    
         echo " < optionvalue =$index[3] selected = 'selected' > $index[3]</option > ";   
       }
     echo "</select>";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.