我有一个表单可以更新单个表行中的多个字段。其中一个字段需要是一个选择/选项框,第一个选项是现有条目。
我试过这个: -
while ($row=mysql_fetch_object($sql)) {
$repairRqd = "'.$row->repair_required.'";
//print out table contents and add id into an array and email into an array
echo '<tr>
<td class="hidden"><input name="id3[]" value='.$row->id.' type="hidden" readonly> </td>
<td align="center">'.$row->id.'</td>
<td align="center">'.$row->repaired_by.'</td>
<td align="center">'.date("l",strtotime($row->received_date)).'</br>'.$row->received_date.'</td>
<td align="center"><input name="customer_name[]" value="'.$row->customer_name.'" maxlength = "50" size = "10"</td>
<td align="center"><input name="repair_required[]" value="'.$row->repair_required.'" maxlength="249" size="40"></td>
<td align="centre"><select name="status">
<option value="'.$row->status.'">'.$row->status.'</option>
<option value="Received">Received</option>
<option value="In Progress">In Progress</option>
<option value="On Hold">On Hold</option>
<option value="Written Off">Written Off</option>
<option value="Cancelled">Cancelled</option>
<option value="Complete">Complete</option>
<option value="Dispatched">Dispatched</option>
<option value="Collected">Collected</option>
</select></td>
</tr>';
使用PHP: -
//get data from form
$id3 = $_POST['id3'];
$repairRequired = $_POST['repair_required'];
$status1 = $_POST['status'];
$customerName = $_POST['customer_name'];
// set queries for each item in the row
foreach ($id3 as $key => $value){
$query = "UPDATE repair SET repair_required = '$repairRequired[$key]' WHERE id = $value";
$query2 = "UPDATE repair SET status = '$status1[$key]' WHERE id = $value";
$query7 = "UPDATE repair SET customer_name = '$customerName[$key]' WHERE id = $value";
//execute query
mysql_query($query);
mysql_query($query2);
mysql_query($query7);
}
奇怪的是,我将现有内容显示在选项列表中,但是如果我更改它(通过选择其他选项),所有写入数据库的内容都是所选选项的第一个字母。
我在POST ['status']和变量$ status1上都做了var_dump,两者都显示了我所期望的选择中的完整文本。
我看不出为什么没有将完整的字符串写入数据库的任何理由,并且非常感谢您的任何帮助?
答案 0 :(得分:0)
将选择名称更改为数组
<select name="status[]">
更新代码:
while ($row=mysql_fetch_object($sql)) {
$repairRqd = "'.$row->repair_required.'";
//print out table contents and add id into an array and email into an array
echo '<tr>
<td class="hidden"><input name="id3[]" value='.$row->id.' type="hidden" readonly> </td>
<td align="center">'.$row->id.'</td>
<td align="center">'.$row->repaired_by.'</td>
<td align="center">'.date("l",strtotime($row->received_date)).'</br>'.$row->received_date.'</td>
<td align="center"><input name="customer_name[]" value="'.$row->customer_name.'" maxlength = "50" size = "10"</td>
<td align="center"><input name="repair_required[]" value="'.$row->repair_required.'" maxlength="249" size="40"></td>
<td align="centre"><select name="status[]">
<option value="'.$row->status.'">'.$row->status.'</option>
<option value="Received">Received</option>
<option value="In Progress">In Progress</option>
<option value="On Hold">On Hold</option>
<option value="Written Off">Written Off</option>
<option value="Cancelled">Cancelled</option>
<option value="Complete">Complete</option>
<option value="Dispatched">Dispatched</option>
<option value="Collected">Collected</option>
</select></td>
</tr>';