Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update data from MYSQL, PHP to drop down button

Tags:

php

mysql

get

if(isset($_GET["id"])){
        $sql=mysql_query("SELECT * FROM aMovie WHERE aName= '{$_GET['id']}'"); 
        $row=mysql_fetch_object($sql);  
}

<input type = "text" name = "name" value = "<?php echo $row->aC; ?>"/> 
<select name = "name" >
        <option value = "" <?php echo ($row->aC== "Deadpool") ? 'selected = "selected"': '';?>">Deadpool</option>
        <option value = "" <?php echo ($row->aC == "BATMAN VS SUPERMAN") ? 'selected = "selected"': '';?>">BATMAN VS SUPERMAN</option>
</select>

Assume that aMovie is my table name, and in my table there are aName and and aC. However, I would want to display aName which matches aC ["Deadpool" or "Batman Vs Superman"] and display it in the drop down button. It only works for the input type but not the drop down button.

like image 490
ikon Avatar asked Apr 12 '16 09:04

ikon


People also ask

How get value from dropdown in database in PHP?

This may help you. ? php $sql = "select * from mine where username = '$user' "; $res = mysql_query($sql); while($list = mysql_fetch_assoc($res)) { $category = $list['category']; $username = $list['username']; $options = $list['options']; ?> <input type="text" name="category" value="<?


3 Answers

Your <select> should be like:

<select name = "name" >
<option value="Deadpool" <?=($rows->aC == "Deadpool" ? 'selected="selected"': '')?>>Deadpool</option>
<option value="BATMAN VS SUPERMAN" <?=($rows->aC == "BATMAN VS SUPERMAN" ? 'selected="selected"': '')?>>BATMAN VS SUPERMAN</option>
</select>

selected="selected" will use outside the value attribute.

UPDATE:

As @Maninderpreet-Singh mentioned, you also need to change $row to $rows.

like image 72
devpro Avatar answered Oct 03 '22 10:10

devpro


try to change

<option <?php echo($row->aC== "Deadpool") ? 'selected = "selected"': '';?>  value="<?php echo $row->aC;?>">Deadpool</option>
like image 31
Ranjeet Singh Avatar answered Oct 03 '22 11:10

Ranjeet Singh


try with this and you are using different variable in input

<input type = "text" name = "name" value = "<?php echo $rows->aC; ?>"/> 

$row and $rows are different

  <option value = "<?php echo $row->aC; ?>" <?php echo ($row->aC == "Deadpool") ? 'selected':'';?>">Deadpool</option>
  <option value = "<?php echo $row->aC; ?>" <?php echo ($row->aC == "BATMAN VS SUPERMAN") ? 'selected': '';?>">BATMAN VS SUPERMAN</option>
like image 36
Maninderpreet Singh Avatar answered Oct 03 '22 10:10

Maninderpreet Singh