0

I am working on a hotel feature page.

the peice of code I am stuck on is the following:

<?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
}
?>

I want to save the data by using the array, but i cannot get the name to save into the database

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 Answer 1

1

You can use the foreach, is a better option in this case:

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')");
    }
}

Your error is when make a for you dont use the index of array ($result) that create in this, try with:

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] . "')"
        );
    }

}

I recomend the foreach

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.