dongwen7423 2015-01-05 11: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 12: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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 srs-sip外部服务 webrtc支持H265格式
  • ¥15 在使用abaqus软件中,继承到assembly里的surfaces怎么使用python批量调动
  • ¥15 大一C语言期末考试,求帮助🙏🙏
  • ¥15 ch340驱动未分配COM
  • ¥15 Converting circular structure to JSON
  • ¥30 Hyper-v虚拟机相关问题,求解答。
  • ¥15 TSM320F2808PZA芯片 Bootloader
  • ¥45 谷歌浏览器出现开发者工具无法显示已创建的,但您可以调试已部署的代码。 状态代码 404, net::ERR HTTP RESPONSE CODE FAILURE
  • ¥15 如何解决蓝牙通话音频突发失真问题
  • ¥15 安装opengauss数据库报错