发布具有多个相同名称的多个输入 [英] Post multiple Inputs with multiple same name

查看:58
本文介绍了发布具有多个相同名称的多个输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何将这种类型的输入发布到php并使用mysqli将其插入到mysql

How can i post this type of inputs to php and insert him to mysql with mysqli

<tr>
 <td>
  <input type="text" name="fii[]">
  <input type="text" name="foo[]">
  <input type="text" name="faa[]">
 </td>
 <td>
  <input type="text" name="fii[]">
  <input type="text" name="foo[]">
  <input type="text" name="faa[]">
 </td>
</tr>

表的结构| fii | foo | faa |

the structure of the table |fii|foo|faa|

推荐答案

注意:

1)下面是逻辑,请尝试使用您的编码方法和字段

1) Below is the logic, Try with your coding method and fields

2)已编辑:它将完美地与name="field1[]"而不是name="field1['.$i.']"一起使用. 但是,如果要编辑保存的数据,可以将$i替换为数据库ID,或者需要在for循环中传递数据库ID为<input type="hidden" name="database_id['.$unique_id.']">的隐藏输入字段. (取决于您的逻辑和实现)

2) Edited: It will be work perfectly with name="field1[]" instead name="field1['.$i.']". However If you want to edit the saved data, $i can be replace with database id OR you need to pass an hidden input field with the database id <input type="hidden" name="database_id['.$unique_id.']"> in for loop. (is upto your logic and implementations)

表单页面

<form name="form_insert" action="your_action_page.php" method="post">
<table border="0">

<?php
/* 
If dynamic via page load, 
$total_rows = isset($_GET['total_rows']) ? $_GET['total_rows'] : 1;
*/
$total_rows = 2;

for ($i=1; $i<=$total_rows; $i++)
{
echo '<tr>
      <td><input type="text" name="field1['.$i.']"></td>
      <td><input type="text" name="field2['.$i.']"></td>
      <td><input type="text" name="field3['.$i.']"></td>
      </tr>
     ';
}
?>
</table>
<input type="submit" name="submit" value="Submit" />
</form>

your_action_page.php

<?php
if (isset($_POST))
{
    $field1_array = isset($_POST['field1']) ? $_POST['field1'] : array();
    $field2_array = isset($_POST['field2']) ? $_POST['field2'] : array();
    $field3_array = isset($_POST['field3']) ? $_POST['field3'] : array();

    $total_rows = count($field1_array);

    if ($total_rows > 0)
    {
        for ($i=1; $i<=$total_rows; $i++)
        {
        $field1_val = $field1_array[$i];
        $field2_val = $field2_array[$i];
        $field3_val = $field3_array[$i];

        $sql = "INSERT INTO your_table (first, second, third) VALUES ('".$field1_val."', '".$field2_val."', '".$field3_val."')";
        $query = mysqli_query($connect, $sql) or die("Error: ".mysqli_error($connect));
        //if ($query)       
        //echo 'data inserted successfully';            
        }
    }
}
?>    

这篇关于发布具有多个相同名称的多个输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆