如何使数组以相同的形式在sql表中添加多行 [英] How to make array for add multiple row in a sql table by same form

查看:81
本文介绍了如何使数组以相同的形式在sql表中添加多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过表格在表格中添加多行.

I want to add multiple row in a table by a form.

我知道如何只制作一个数据数组,但是如果是全名数组,所有明细数组,所有联系人数组,我将无法做到这一点.

I know how to a make only one data array but in case of all name array, all detail array, all contact array I can't do this.

我也在这里阅读了很多Google文章,但未取得任何结果.

I also read here and google many article but I failed to make any result.

所以请帮助我创建它?

所以我的来信是

<form action="" method="POST">
Name:    <input type="text" name="name[]" value="">
Detail:  <input type="text" name="detail[]" value="">
Address: <input type="text" name="add[]" value="">
<br />
Name:    <input type="text" name="name[]" value="">
Detail:  <input type="text" name="detail[]" value="">
Address: <input type="text" name="add[]" value="">
<br />
Name:    <input type="text" name="name[]" value="">
Detail:  <input type="text" name="detail[]" value="">
Address: <input type="text" name="add[]" value="">
<input type="submit" name="submit" id="submit" value="Submit">

然后将我的php插入sql

And my php for insert at sql

include("../db.php");
global $dbh;
if(isset($_POST['submit'])){
$data = array(); // now how to do all single post array?
$name = mysqli_real_escape_string($_POST['name']);
$detail = mysqli_real_escape_string($_POST['detail']);
$add = mysqli_real_escape_string($_POST['add']);

$result=mysqli_query($dbh,"INSERT INTO varify (id,name,detail,add) VALUES('','$name','$detail','$add')");

推荐答案

当然,首先您需要循环它们,可以使用foreach.然后,您可以使用foreach内的键来访问表单上的其他排队索引:

Of course first off you need to loop them, you can use foreach. Then you can use the key inside the foreach to access the other lined up indices on the form:

一个粗略的例子:

if(isset($_POST['submit'])) {

    foreach($_POST['name'] as $k => $name) {
        $name = mysqli_real_escape_string($dbh, $name);
        $detail = mysqli_real_escape_string($dbh, $_POST['detail'][$k]);
        $add = mysqli_real_escape_string($dbh, $_POST['add'][$k]);

        // continue insertion
    }
}

或使用预备语句:

if(isset($_POST['submit'])) {
    foreach($_POST['name'] as $k => $name) {
        $insert = $db->prepare('INSERT INTO varify (id, name, detail, add) VALUES('', ?, ?, ?)');
        $insert->bind_value('sss', $name, $_POST['detail'][$k], $_POST['add'][$k]);
        $insert->execute();
    }
}

另一种选择是在html标记表单内创建分组结构.

Another alternative would be to create the grouping structure inside your html markup form.

使用名称分组内的键对其进行分组.因此标记看起来像这样:

Use the keys inside the name grouping to group them. So the markup would look something like this:

<form action="" method="POST">
Name:    <input type="text" name="input[0][name]" value="">
Detail:  <input type="text" name="input[0][detail]" value="">
Address: <input type="text" name="input[0][add]" value="">
<br />
Name:    <input type="text" name="input[1][name]" value="">
Detail:  <input type="text" name="input[1][detail]" value="">
Address: <input type="text" name="input[1][add]" value="">
<br />
Name:    <input type="text" name="input[2][name]" value="">
Detail:  <input type="text" name="input[2][detail]" value="">
Address: <input type="text" name="input[2][add]" value="">
<br />
<input type="submit" name="submit" id="submit" value="Submit">

在PHP中进行处理时,它们现在按行分组,可以像这样访问:

When you handle it in PHP, now they are grouped by row and can be accessed like this:

<?php

if(isset($_POST['submit'])) {
    $input = $_POST['input'];

    foreach($input as $values) {
        if(!empty($values['name']) && !empty($values['detail']) && !empty($values['add'])) {
            $insert = $db->prepare('INSERT INTO varify (id, name, detail, add) VALUES('', ?, ?, ?)');
            $insert->bind_value('sss', $values['name'], $values['detail'], $values['add']);
            $insert->execute();
        }   
    }
}

这篇关于如何使数组以相同的形式在sql表中添加多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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