dourao1968 2017-07-03 17:36
浏览 31
已采纳

当用户没有选择选项时,$ _POST ['status'] [$ index]为空

I have an availability tracker that has many rows representing users with select/option boxes in each row (7 to be exact). This gives me a possibility of 7*num_of_users select boxes.

My UI looks like this:

       Monday  Tuesday  Wednesday Thursday
------------------------------------------
|Name 'status' 'status' 'status' 'status'|
------------------------------------------
|Name 'status' 'status' 'status' 'status'|
------------------------------------------
|Name 'status' 'status' 'status' 'status'|
------------------------------------------

Where each Name is a label and 'status' is an select option box.

<table>
   <tr>
     <th>
     </th>
     <th>
       Monday
       <br/> 
       Jul 03, 2017
     </th>
     <th>
       Tuesday 
       <br/> 
       Jul 04, 2017
     </th>
   </tr>
   <tr>
     <td>
       Name
    </td>
     <td>
       <select name=status[] style = "color: ; background-color: ""onChange="setColor(this)" >
         <option disabled selected value> -- select an option -- </option>
         <option value='Available' style = "color: #000000; background-color: #b6d7a8">Available</option>
         <option value='Unavailable' style = "color: #ffffff; background-color: #000000">Unavailable</option>
       </select>
     </td>
     <td>
      <select name=status[] style = "color: ; background-color: " onChange="setColor(this)" >
        <option value='Available' style = "color: #000000; background-color: #b6d7a8">Available</option>
        <option value='Unavailable' style = "color: #ffffff; background-color: #000000">Unavailable</option>
      </select>
    </td>
  </tr>
</table>

The issue I'm having is when the user updates the form via button press, if the user does not select an option for every select box then the values do not exist in $_POST['status'][$index] where I'm using index to loop through the 7*num_of_users select boxes.

If only 4 options are selected then the array has 4 elements. How do I go about getting these NULL/empty values?

if(isset($_POST['update']))
{
    $query = "SELECT id "
           . "FROM pilots "
           . "ORDER BY last_name, first_name, id";

    $result = mysqli_query($db, $query) or die ('Error querying database');

    $index = 0;
    while ($row = mysqli_fetch_array($result))
    {
        for ($day = 0; $day < 7; $day++)
        {
            $status = $_POST['status'][$index];

            $query = "INSERT INTO availability (pilot_id, date, status) 
                      VALUES ('" . $row['id'] . "', '2017-06-26', '" . $status . "') 
                      ON DUPLICATE KEY 
                      UPDATE status='". $status . "'";

            mysqli_query($db, $query) or die ('Error querying database');
            $index++;
        }
    }
}
  • 写回答

2条回答 默认 最新

  • dongtan7639 2017-07-03 18:45
    关注

    First give the status of each box a fixed index, eg.:

    <select name="status[3]" ...>
    

    Now loop as before, but check if that index is present:

    for ($day = 0; $day < 7; $day++) {
      if (!isset($_POST['status'][$index])) {
        // value was not selected, maybe use a default
      } else {
        $status = $_POST['status'][$index];
      }
    
      // ...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 ARIMA模型时间序列预测用pathon解决
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序,怎么查看客户esp32板子上程序及烧录地址
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)