dream518518518 2014-12-29 20:31
浏览 191
已采纳

将多个复选框值添加到单个行中的多个列

i read other threads but don't think i have found the answer (or maybe just don't know how to implement it...) for this. i have a mysql table with columns for an item size, amongst other things. the user fills in a form in which mulitple sizes may be selected from a series of checkboxes. i want to be able to insert the selected checkbox values in to the proper columns while leaving the other columns either null or if necessary i guess as a 0 value. the columns are tinyint format right now.

'One Size'

<input type="checkbox" name="itemSize[]" value="1" />

Small

<input type="checkbox" name="itemSize[]" value="2" />

Medium

<input type="checkbox" name="itemSize[]" value="3" />

Large

<input type="checkbox" name="itemSize[]" value="4" />

the database table has columns named oneSize, small, medium and large.


HERE IS THE CODE I USED TO TRY TO INSERT A RECORD BASED ON AN ARTICLE IN FOUND IN THE FORUM:

$itemName=$_POST["itemName"];
$allsizes=$_POST["itemSize"];
var_dump($allsizes);
$itemSizing="";
foreach($allsizes as $size){
$itemSizing .=$size .",";
}
$itemSizing=substr($allsizes, 0, -2);
$price=$_POST["price"];
$smallPrice=$_POST["smallPrice"];
$mediumPrice=$_POST["mediumPrice"];
$largePrice=$_POST["largePrice"];
$decsription=$_POST["description"];
$category_id=$_POST["category_id"];

and then in the insert statement i tried listing each column and then the values for the 4 item sizes taken from the checkboxes are listed as the concatenated string:

$SQL="INSERT INTO menuItems (itemName,oneSize,small,medium, large,price,smallPrice,mediumPrice,largePrice,description,category_id) VALUES ('$itemName','$itemSizing','$price','$smallPrice','$mediumPrice','$largePrice','$description','$category_id')";

THIS IS THE ERROR MESSAGE I GOT WHEN ATTEMPTING TO INSERT A RECORD FEATURING A SMALL SIZE AND A LARGE SIZE CHECKED OFF AND THE OTHER TWO CHECKBOXES LEFT UNCHECKED:

array(2) { [0]=> string(1) "2" [1]=> string(1) "4" } 
Warning: substr() expects parameter 1 to be string, array given in menuItems.php on line 162
INSERT INTO menuItems (itemName,oneSize,small,medium,large,price,smallPrice,mediumPrice,largePrice,description,category_id) VALUES ('Garden Salad','','','3.99','','6.99','Fresh greens with lettuce, tomatoes and cucumbers. Served with your choice of dressing.','3') : Error in query. Contact Site Admin

I thought that the string being created was a series of the checkbox values separated out for insertion into the various columns - which is exactly what I want to happen - but clearly it didn't work. I am obviously not very familiar with arrays so I apologize if this is a simple question.

thanks very much for any help anyone can provide...

  • 写回答

4条回答 默认 最新

  • dousigan0499 2014-12-29 20:47
    关注

    Since your values are 1-4, you could use a for loop to check if each value is in the POST array and then create your $itemSizing string -

    //a size array
    $itemSizingArray = array();
    
    //for loop for each size value
    for($i=1;$i<=4;$i++){
        //add either a '1' or 'NULL' to the array, based on if the checkbox value is in the array
        $itemSizingArray[] = (in_array($i,$_POST["itemSize"]) ? 1: 'NULL');
    }
    //implode the array
    $itemSizing = implode(', ',$itemSizingArray);
    

    So with your example of Small and Large checked your string would be -

    NULL, 1, NULL, 1
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏