通过表单将多行插入数据库 [英] insert multiple rows into database via form

查看:60
本文介绍了通过表单将多行插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 php 中,我可以使用下面的方法将多行添加到我的数据库中

In php I can add multiple rows into my database using the below

$sql = 'INSERT INTO 'tablename' ('column1', 'column2') VALUES
    ('data1', 'data2'),
    ('data3', 'data4'),
    ('data5', 'data6'),
    ('data7', 'data8');
';

但我不知道如何创建一个允许我将多行添加到数据库中的表单.

But I can't work out how to create a form that will allow me to add multiple rows into the database.

过去,当我只想一次添加一行时,我已经能够在表单中执行类似的操作.

In the past when I just want to add in one row at a time I have been able to do something like this in my form.

<input type="text" name="name" value="">

以及我的 php 中的以下内容

and the below in my php

$_POST['name']

但是当您要插入多行时,这不起作用.有人可以在这里指出我正确的方向吗?

but this doesn't work when you want to insert multiple rows. Can someone please point me in the right direction here?

推荐答案

你可以使用 php 数组,所以你这样做

You can use php arrays, so you do

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

注意名称后添加的方括号.您可以在表单中有多个这样的实例,然后在 php 中将其引用为:

note the square brackets added after name. You can have multiple instances of this in your form and then refer to it in the php as:

$_POST['name'][$i]

其中 $i 是一个变量索引数组中的值.

where $i is a variable indexing the value in the array.

这篇关于通过表单将多行插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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