Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the value from database in selecte tag HTML PHP MySQL [duplicate]

Tags:

html

php

mysql

I'm beginner in PHP MySQL. I successfully get the value from database in INPUT type but I can't get the data from database in SELECT type:

Here is my sample edit form where Gender & User Type can't output the value from my database:

enter image description here

and here is data from the table I created: enter image description here

I'm calling the data in my input box by using this code:

<?php $userRow['agentFname']; ?>  //$userRow['ROW NAME IN TABLE'];

But I can't call the data where In select box, like Gender: and User Type here is my code of Select box.

   <select class="form-control" name="aGender" >
           <option selected disabled>*Gender</option>
          <option>Male</option>
          <option>Female</option>

   </select> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

   <select class="form-control" name="utype" >
           <option selected disabled>*User Type</option>
          <option>Agent</option>
          <option>Team Leader</option>
          <option>Admin</option>
        </select>
like image 263
Edmhar Avatar asked Dec 21 '25 21:12

Edmhar


1 Answers

@Edmhar has a good answer, but it is hard coded and isn't good if you want to add more genders (transgenders and what not).

What I do is something like this:

<?php
$array = array("male", "female", "other");

echo "<select class='form-control' name='aGender' >";
foreach ($array as $gender) {
    if ($gender == $databaseValue) {
       echo "<option selected>$gender</option>";
    } else {
       echo "<option>$gender</option>";
    }
}
echo "</select>";
?>

Also, don't use disabled on form elements; use read-only. It does the same thing as disabled visually, but disabled does what it says. It blocks the value from being submitted to the database. read-only just prevents editing, but doesn't cause form submission problems. User type will follow the same suit.

like image 90
Mitch Mullvain Avatar answered Dec 23 '25 09:12

Mitch Mullvain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!