简体   繁体   中英

Multiple Checkbox Values PHP

I've got this code, anyone help on making it work...

while($fetch = mysql_fetch_assoc($query)){

    $image_name = $fetch['image_name'];
    $image_location = $fetch['image_location'];


    <img src="'.$image_location.'"/>

    <input name="image['.$image_id.'][6x7]" type="checkbox" value="'.$image_name.'" /> 6x4
    <br />
    <input name="image['.$image_id.'][8x9]" type="checkbox" value="'.$image_name.'" /> 7x5

}

Display on different page...

foreach($_POST['image'] as $image_id => $name) {

    $uploaded[] = $name;

    echo''.$name.'<br />';


    foreach($name as $size => $size) {

        $uploaded[] = $size;

        echo''.$size.'<br />';

    }
}

Input Script

mysql_query("INSERT INTO table VALUES('', '$name', 'size')");

I am uploading both image name and size (from the checkboxes 6x4, 7x5) I am able to input the size into the database but image name just says Array .

I know this is an obvious fix but I just can't get my head around the multiple arrays is the checkbox values!

Any help of feedback is welcome! HELP!

I assume your $image_location and $image_name are fine so you have correct values on your checkboxes? Otherwise we would need some more details..

Try below to display that correctly then (you're using multidimensional array):

foreach($_POST['image'] as $image_id => $image) {

    foreach($image as $size => $name) {


        $uploaded[] = $name;
        echo''.$name.'<br />';


        $uploaded[] = $size;
        echo''.$size.'<br />';
    }
}
<input name="image['.$image_id.'][]" type="checkbox" value="6x7" /> 6x4
<br />
<input name="image['.$image_id.'][]" type="checkbox" value="8x9" /> 7x5

The value should not be the image name, but the size you specified. You already have the image ID, so why include the filename? You can just extract that from your database again...

Also:

foreach($name as $size => $size) {

... is a bit weird: Using the $size var for key and value? That's asking for trouble. ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM