Save the selected value in the database as an identifier
I have two tables as shown, when an employee edits their data the department_id stores as 0 in the database
employee department
id| name|department_id id|department_name
1 |abc |1 1|HR
2 |xyz |4 2|Finance
3 |asd |3 3|Developer
and this part of the view
<tr>
<th> Department </th>
<td>
<SELECT name="department_id" style="width:180px;" >
<?php
foreach($departments as $row): ?>
<option value="<?php $employee['department_id'] ;?>"
<?php if ($row['id']== $stakehholder['department_id'])
{
echo 'selected';
}
?>>
<?php echo $row['department_name'];?>
</option>
<?php endforeach ?>
</SELECT>
</td>
<td> <?php echo form_error('department_id'); ?> </td>
</tr>
after edit 0 value get storage in database
+3
source to share
3 answers
forget the echo <option value="<?php $employee['department_id']; ?>"
<SELECT name="department_id" style="width:180px;" >
<?php foreach($departments as $row): ?>
<option value="<?php echo $row['department_id']; ?>"
<?php if ($row['department_name']== $stakehholder['department_id']) {
echo 'selected';
}
?>>
<?php echo $row['department_name']; ?>
</option>
<?php endforeach ?>
</SELECT>
+4
source to share
the updated line is below
<option value="<?php echo $employee['department_id'] ;?>
<tr>
<th> Department </th>
<td>
<SELECT name="department_id" style="width:180px;" >
<?php
foreach($departments as $row): ?>
<option value="<?php echo $employee['department_id'] ;?>"
<?php if ($row['department_name']== $stakehholder['department_id'])
{
echo 'selected';
}
?>>
<?php echo $row['department_name'];?>
</option>
<?php endforeach ?>
</SELECT>
</td>
<td> <?php echo form_error('department_id'); ?> </td>
</tr>
+1
source to share
you forgot the echo department_id in the parameter value attribute, and secondly, you are comparing the name with id which never match if id is int and name not.
try this updated code:
<SELECT name="department_id" style="width:180px;" >
<?php
foreach($departments as $row): ?>
<option value="<?php echo $row['department_id'] ;?>"
<?php if ($row['department_id']== $stakehholder['department_id'])
{
echo 'selected';
}
?>>
<?php echo $row['department_name'];?>
</option>
<?php endforeach ?>
</SELECT>
+1
source to share