Storing Multiple Checkbox Data in MySQL Database with PHP

前端 未结 4 847
-上瘾入骨i
-上瘾入骨i 2021-02-10 05:30

I want to have multiple checkbox values be stored into one field in a database. (Ex. 1, 24,56,100). I am wanting to know how I can make this happen, and how does PHP read thes

4条回答
  •  别跟我提以往
    2021-02-10 05:41

    Even though I am not in favor of saving data like that but here is what you can do, if you really want to do it that way. I suggest you have a denormalized table and store your vals there

    in your HTML you can have your checkboxes like this (considering you are storing ids of some sort)

    
    
    
    
    

    On you php side you can use function implode to form ids into a string as shown below (considering you are doing a POST)

    $ids = implode(",",$_POST["ids"]);
    

    Where you read from the database you can transform the value from db to an array like this

    $ids_array = explode(",",$row->ids);
    

    I hope this helps

提交回复
热议问题