2019-02-13 10:06
浏览 179


I want to insert multiple data into table in single column with same primary key when form is submitting using php sql.

Here is the filled form -

form with data

I want to insert into table using php & sql (ajax if need) like this below link-

[table with data i hope

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dsyo9700 2019-02-13 10:48

    I had same isssue while making API for PHP.

    Try this , declare your Item name , quanity and date as TEXT and insert nested JSON in those fields.


    //while adding to DB json will look like
    "[{ABC},{PQR},{XYZ}]","[{23},{56},{14}]","[{23/05/1900 12:00 GMT},{23/05/1908 12:00 GMT},{23/05/1980 12:00 GMT}]"


    "[{ABC},{PQR},{XYZ}]" go to item_name column,

    "[{23},{56},{14}]" goes to quantity column,

    "[{23/05/1900 12:00 GMT},{23/05/1908 12:00 GMT},{23/05/1980 12:00 GMT}]" goes to date column

    as simple string or text. Here's below is my HTML Code for Table (Ignore style ☺)

        <td>23/05/1900 12:00 GMT</td>
        <td>23/05/1908 12:00 GMT</td>
        <td>23/05/1980 12:00 GMT</td>

    So while adding to Database you need first push data to object then Stringyfy it and then send to database. And while fetching from database and displaying again to UI , you need to Parse those strings back to Objects then you can apply looping like it is an Nested object.

    Happy Codeing ☻

    点赞 打赏 评论
  • doudian6229 2019-02-13 10:15

    You can not use multiple rows with same primary key..For every Item you have to create separate row.

    If you want to implement these then there are 2 way:

    1) create Main Table: which contain all row info other than item info. Create sub table : which contain all item info by using main_table_id as forgin key.
    2) You can create item column & store all item & quantity into in JSON format.(mysql>=5.7 support json format) like :

    id    Item_data (json format)                                        date
    1     {["name":"Pen","quantity":10],["name":"xyz","quantity":1]}     2019/02/13
    2     {["name":"gfdgf","quantity":10],["name":"asa","quantity":1]}   2019/02/13
    点赞 打赏 评论
  • dtkago3208 2019-02-13 10:31

    There are 3 options in your situation:

    1. Serialize data (see the answer below). Notice that it's almost impossible to run queries on this data.

    2. Use an extra table for your items. Then use a foreign key that connect this item to your main table

    3. Use a no-sql database (for example MongoDB). In no-sql databases it is possible to store complete "objects" (called documents) with multiple child-objects

    点赞 打赏 评论

相关推荐 更多相似问题