Storing Multiple Checkbox Data in MySQL Database with PHP

前端 未结 4 848
-上瘾入骨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:53

    The perfect solution for this is task the creation of normalized table as commented by @OMG and @Michael.

    But here is the answer for what you just asked

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

    Store this in MySQL table. You can use LIKE command to query the table and use the explode to get back the ids in array.

    $query = "SELECT * FROM  WHERE ids LIKE '%,2, %'"; // get posts having id 2
    
    $id = explode(", ", $result->ids);
    

    提交回复
    热议问题