duan19805 2017-10-11 10:27
浏览 31
已采纳

Php多种形式,在特定形式上迭代

I have page a with 2 forms

the first part of the form

<form id="A" role="form" enctype="multipart/form-data" method="post">
<input class="form-control" placeholder="Name of the Company" name="ven_name" type="text" autofocus >
<input class="form-control" placeholder="Address" name="address" type="text" autofocus >
<input class="form-control" placeholder="City" name="city" type="text" autofocus >
</form>


<form id="B" role="form" enctype="multipart/form-data" method="post">
<input type="text" name="usr_f_name[]" placeholder="First Name" />
<input type="text" name="usr_l_name[]" placeholder="Last Name" />
<input type="text" name="usr_mobile[]" placeholder="Mobile Number" />
<input type="text" name="usr_email[]" placeholder="Email" />
<input type="password" name="usr_password_1[]" placeholder="Password"/>    
</form>

both of them will be saved on a Database. Form A will be saved on Table A and Form B will be save on Table B. For Form B i'm using a Jquery so User can add multiple Users at a time ( maximum 3 user in one go )

here is the Php for Form B

    $usr_array = array_keys($_POST['']);
    foreach ($id_array as $id) {
        $usr_f_name =  mysqli_real_escape_string ($_POST['usr_f_name'][$id]);
        $usr_l_name =  mysqli_real_escape_string ($_POST['usr_l_name'][$id]);
        $usr_mobile =  mysqli_real_escape_string ($_POST['usr_mobile'][$id]);
        $usr_email =  mysqli_real_escape_string ($_POST['usr_email'][$id]);
        $usr_password_1 =  md5 ($_POST['usr_password_1'][$id]);



$sql = "INSERT INTO table_b SET f_name = '$usr_f_name', l_name = '$usr_l_name', mobile  ='$usr_mobile',username='$usr_email', email='$usr_email', password='$usr_password_1'";
        }
        if(mysqli_query($databaseLink, $sql)){
            echo "Records added successfully.";
        } else{
            echo "ERROR: Could not able to execute $sql. " . mysqli_error($databaseLink);
        }
    }

how do I tell php which form to iterate on?

  • 写回答

2条回答 默认 最新

  • doufangmu9087 2017-10-11 10:44
    关注

    Change naming of your fields:

    <div class="form-B"
      <input class="form-control" placeholder="Name of the Company" name="formA[ven_name]" type="text" autofocus >
      <input class="form-control" placeholder="Address" name="formA[address]" type="text" autofocus >
      <input class="form-control" placeholder="City" name="formA[city]" type="text" autofocus >
    </div>
    
    
    <div class="form-B">
      <input type="text" name="formB[0][usr_f_name]" placeholder="First Name" />
      <input type="text" name="formB[0][usr_l_name]" placeholder="Last Name" />
      <input type="text" name="formB[0][usr_mobile]" placeholder="Mobile Number" />
      <input type="text" name="formB[0][usr_email]" placeholder="Email" />
      <input type="password" name="formB[0][usr_password_1]" placeholder="Password"/>    
    </div>
    

    That way your $_POST will look like this:

    [
        'formA' => [
            'ven_name' => '',
            'address' => '',
            'city' => '',
        ],
        'formB' => [
            [
                'usr_f_name' => '',
                /* ... */
            ],
            [
                'usr_f_name' => '',
                /* ... */
            ]
            /*...*/
        ]
    ]
    

    And you can do:

    $query = 'INSERT INTO formB (usr_f_name, ...) VALUES ';
    $inserts = [];
    $params = [];
    
    foreach ($formData['formB'] as $fields) {
        /* ... */
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证