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条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀