dongwen7423 2015-01-05 03:51
浏览 94
已采纳

在foreach循环php中存储数据

I have created a form that allows a user to enter data, they also have the option to dynamically add more text boxes using JavaScript to provide more information if necessary. e.g. First Name, Last Name, Course. A user may only have one course or they may have many. For every course a user includes I want to be able to store an entire new row in the database. Is there a way I can do this?

  • 写回答

1条回答 默认 最新

  • ds2010630 2015-01-05 04:20
    关注

    Welcome to StackOverflow :)

    You can do this for sure, but you need to pass every dataset to php. There you can store the data.

    <input type="text" name="name1"><input type="text" name="gender1">
    <input type="text" name="name2"><input type="text" name="gender2">
    

    And write it to the db in php

    for($i = 1; isset($_POST['name' . $i]); $i++)
        {
            $name = $_POST['name' . $i];
            $gender = $_POST['gender' . $i];
            // update DB with input values
        }
    

    After that you have several new rows inserted.

    Alternatively you could create an array for all data:

    <input type="text" name="name[]">
    <input type="text" name="gender[]">
    

    In PHP, you have to do following

    $cnt = count($_POST['name']);
    for($i=0;$i<$cnt;$i++){
       // do any update with database
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部