luv2code
luv2code

Reputation: 1296

Getting value from two checkboxes and inserting into mysql

I have run into a problem... I have a pair of checkboxes that I have named and numbered. I need to get the checkboxes marked and insert them into my database. Here is how I am creating the checkboxes:

<div class="check"><input type="checkbox" name="acharge1" value="1"> <span style="color: #0C0">$$</span><input type="checkbox" name="amenities1" value="1">Amenity 1</div>
<div class="check"><input type="checkbox" name="acharge2" value="1"> <span style="color: #0C0">$$</span><input type="checkbox" name="amenities2" value="1">Amenity 2</div>
<div class="check"><input type="checkbox" name="acharge3" value="1"> <span style="color: #0C0">$$</span><input type="checkbox" name="amenities3" value="1">Amenity 3</div>
<div class="check"><input type="checkbox" name="acharge4" value="1"> <span style="color: #0C0">$$</span><input type="checkbox" name="amenities4" value="1">Amenity 4</div>

Some of these will be checked and some wont. Also they will not always be checked in pairs, but I have to upload them in pairs to my database. So in other words: acharge1 and amenities1 will not be checked but acharge2 and amenities2 will be. So I tried a foreach loop and that did not work. I also tried a while loop, but I did not know what to check. So this is what I have now:

$aa = 1;
    echo "Amenity: ".$_POST['amenities'.$aa];
    $aa++;
    while(isset($_POST['amenities'.$aa])){
        $amen = $_POST['amenities'.$aa];
        if(isset($_POST['acharge'.$aa])){
        $acharge = $_POST['acharge'.$aa];
        }else{
            $acharge = "0";
            }
        echo "Amen: ". $amen." charge: ".$acharge;  
        mysql_query("INSERT INTO hotel_amenities (amenity_code, hotel_id, charge) VALUES ('$amen','$hotel_id','$acharge')") or die(mysql_error());
        echo $aa;
        $aa++;
        }

But as you can see this does not work, because if amenities1 is not checked it will not do the while loop through the others. Anyway ANY help on how I can achieve this would be greatly appreciated!

EDIT** This is the concept, so as you can see that sometimes they money box will be checked with the amenity and sometimes it will not. http://www.cancunelitetravel.com/ale/Capture.PNG

Upvotes: 0

Views: 198

Answers (1)

Milad Naseri
Milad Naseri

Reputation: 4118

The way you are going about this is entirely wrong. If you are unsure of the number of options available, you should write it like this:

$cnt = 0;
foeach ($_POST as $option => $value) {
    if (preg_match("/(?:acharge|amenities)(\\d+)/", $option, $matches)) {
        $cnt = max($cnt, intval($matches[1]));
    }
}
for ($i = 0; $i < $cnt; $i ++) {
    $amenity = $_POST['amenities' . $i];
    $value = "0";
    if (array_key_exists("acharge" . $i, $_POST)) {
        $value = "1";
    }
    mysql_query("INSERT INTO hotel_amenities (amenity_code, hotel_id, charge) VALUES ('$amenity','$hotel_id','$value')") or die(mysql_error());
}

I think this would do it for you. That is assuming you have established the connection previously and taken care of the value for $hotel_id.

Upvotes: 3

Related Questions