如何使用php逐行从动态表中获取数据

时间:2022-11-24 22:42:08

So basically I am looking for a way to take data from a dynamic table that is filled by the user and put it in a mysql database. The only part I am finding hard is reading row by row. Since this is a responsive dynamic table that lets the user add and delete rows, the name of the input tags remains the same row by row. Here is a sample of my code.

所以基本上我正在寻找一种从用户填充的动态表中获取数据并将其放入mysql数据库的方法。我发现努力的唯一部分是逐行阅读。由于这是一个响应式动态表,允许用户添加和删除行,因此输入标记的名称将逐行保持不变。这是我的代码示例。

    <table>
    <tr>
    <td><input type="text" name="foo"> </td>
    <td><input type="text" name="bar"></td>
    </tr>
    <tr>
    <td><input type="text" name="foo"> </td>
    <td><input type="text" name="bar"></td>
    </tr>
    <tr>
    <td><input type="text" name="foo"> </td>
    <td><input type="text" name="bar"></td>
    </tr>
    </table>

Thank you

谢谢

1 个解决方案

#1


1  

You can use array-notation, and this way when you post the values to the server - php will treat them as array:

您可以使用数组表示法,这样当您将值发布到服务器时 - php会将它们视为数组:

<table>
<tr>
<td><input type="text" name="foo[]"> </td>
<td><input type="text" name="bar[]"></td>
</tr>
<tr>
<td><input type="text" name="foo[]"> </td>
<td><input type="text" name="bar[]"></td>
</tr>
<tr>
<td><input type="text" name="foo[]"> </td>
<td><input type="text" name="bar[]"></td>
</tr>
</table>

In your php code you will get

在你的PHP代码中你会得到

$_POST['foo'][0] = 'text1';
$_POST['foo'][1] = 'text2';
$_POST['foo'][2] = 'text3';

And this way you can have as many values as you want.

通过这种方式,您可以拥有任意数量的值。

#1


1  

You can use array-notation, and this way when you post the values to the server - php will treat them as array:

您可以使用数组表示法,这样当您将值发布到服务器时 - php会将它们视为数组:

<table>
<tr>
<td><input type="text" name="foo[]"> </td>
<td><input type="text" name="bar[]"></td>
</tr>
<tr>
<td><input type="text" name="foo[]"> </td>
<td><input type="text" name="bar[]"></td>
</tr>
<tr>
<td><input type="text" name="foo[]"> </td>
<td><input type="text" name="bar[]"></td>
</tr>
</table>

In your php code you will get

在你的PHP代码中你会得到

$_POST['foo'][0] = 'text1';
$_POST['foo'][1] = 'text2';
$_POST['foo'][2] = 'text3';

And this way you can have as many values as you want.

通过这种方式,您可以拥有任意数量的值。