2017-09-17 56 views
0

我正在酒店功能頁面上工作。PHP Array數據庫輸入名稱

的代碼,我被困在peice的是:

<?php 
$result = mysqli_query($conn, "select * from facilities_type"); 
while($row = mysqli_fetch_assoc($result)) 
{ 
?> 
<tr> 
    <th scope="row"> 
     <input class="form-check-input" type="checkbox" id="blankCheckbox" name="room_feature_cb[]" value="<?php echo $row["facilitiestype_id"]; ?>"> 
    </th> 
    <td> 
     <?php echo $row["room_facilities"] ?> 
    </td> 
    <td> 
     <img src="upload-img/icon/<?php echo $row["facilities_icon"]; ?>" width="25"> 
    </td> 
</tr> 
<?php 
} 
?> 

我想利用數組保存數據,但我不能得到名稱保存到數據庫中

if (isset($_POST["room_feature_savebtn"])) 
{ 
    $feature = $_POST['room_feature_cb']; 
    for($result=0;$result>$feature;$result++) 
    { 
    mysqli_query($conn,"insert into facilities_details(facilitiestype_id) value ('$feature')"); 
    } 
} 

回答

1

可以使用foreach,在這種情況下一個更好的選擇:

if (isset($_POST["room_feature_savebtn"])) 
{ 
    $features = $_POST['room_feature_cb']; 
    foreach ($features as $feature) { 
     mysqli_query($conn, "insert into facilities_details(facilitiestype_id) value ('$feature')"); 
    } 
} 

你錯誤是讓你不使用,在此創建陣列($result)的指數for時,嘗試用:

if (isset($_POST["room_feature_savebtn"])) 
{ 
    $feature = $_POST['room_feature_cb']; 
    for($result=0; $result < $feature; $result++) { 
     mysqli_query(
      $conn, 
      "insert into facilities_details(facilitiestype_id) value ('" . $feature[$result] . "')" 
     ); 
    } 

}

我建議的foreach