wakped
wakped

Reputation: 67

Grab Selected Data using Select Box with PHP accessing a MySql database

How could I grab the selected data with select box from database? I've succeeded in doing this with a radio button, but when grabing with a select box I get stuck.

The theory is, if data in the database are saved with 2007-2009 as the value in the th_arsip field, it will show again the selected data (2007-2009) and show all the options when I want to update the data from update form.

Here's my code:

<?php
require "config.php";
$id_arsip = mysqli_real_escape_string($conn,$_GET['id_arsip']);
$sql      = "SELECT * FROM arsip WHERE id_arsip = '$id_arsip' ";
$result   = mysqli_query($conn, $sql);
$data     = mysqli_fetch_array($result);
?>

<div class="col-xs-4"><label>Year</label>
<select name="th_arsip" id="th_arsip" class="form-control" required>
<?php echo
"<option value='2004-2006' ".($data['th_arsip'] == "2004-2006"?'checked':'')."> 2004-2006 </option> ".
"<option value='2007-2009' ".($data['th_arsip'] == "2007-2009"?'checked':'')."> 2007-2009 </option> ".
"<option value='2010-2012' ".($data['th_arsip'] == "2010-2012"?'checked':'')."> 2010-2012 </option> ".
"<option value='2013-2015' ".($data['th_arsip'] == "2013-2015"?'checked':'')."> 2013-2015 </option> ".
"<option value='2016-2018' ".($data['th_arsip'] == "2016-2018"?'checked':'')."> 2016-2018 </option> ".
"<option value='2019-2021' ".($data['th_arsip'] == "2019-2021"?'checked':'')."> 2019-2021 </option> ";
?>
</select>
</div>

Any help will be appreciated. Thanks

Upvotes: 0

Views: 61

Answers (1)

JRD
JRD

Reputation: 1987

<select> elements work off the keyword selected to indicate the highlighted value.

Upvotes: 1

Related Questions