doumei8258 2014-03-13 11:38
浏览 98
已采纳

从现有表条目中选择带有第一个选项的选项

I have a form that updates multiple fields in single table row. One of the fields needs to be a select / option box with the first option being the existing entry.

I've tried this:-

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>';

With PHP of:-

//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);
}

Curiously, I get the existing contents show up in the option list, however if I change it (by selecting another option) all that gets written to the database is the first letter of the selected option.

I've done a var_dump on both the POST['status'] and the variable $status1, and both are showing the complete text from the selection as I'd expect.

I can't see any reason why the complete string is not being written to the database and would be very grateful of any help please?

  • 写回答

1条回答 默认 最新

  • doudian6229 2014-03-13 11:45
    关注

    change the select name as array

    <select name="status[]">
    

    UPDATED CODE:

     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>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?