多行批量输入

时间:2016-05-24 05:30:04

标签: php

代码: 我有3个多行文本框,每个包含一些数据,将进一步用于生成INSERT QUERY,DB包含3列。

所以这些将用于批量条目

<form method="post">    
<textarea name="code" id="code" rows="20" cols="20" placeholder="code"></textarea>
<textarea name="name" id="name" rows="20" cols="20" placeholder="name"></textarea>
<textarea name="pack" id="pack" rows="20" cols="20" placeholder="package"></textarea><Br>
<input type="submit" name="submit">
</form>

<?php
if (isset($_POST['submit'])){
    $code = explode("\n", $_REQUEST['code']);
    $name = explode("\n", $_REQUEST['name']);
    $pack = explode("\n", $_REQUEST['pack']);
    /*foreach (array_combine($code, $name) as $mid => $mid1){
        echo "<br>";
        echo $mid;
        echo $mid1;
    }*/

    foreach ($code as $mid){
        foreach ($name as $mid1){
            foreach ($pack as $mid2){
            echo "INSERT INTO medi_medicines (`cc1`,`nn1`,`pp1`) VALUES('".$mid."','".$mid1."','".$mid2."')";
            echo "<br>";
            }
        }
    }
}
?>

输入::

Input image, here

输出::

INSERT INTO medi_medicines (`cc1`,`nn1`,`pp1`) VALUES('demo ','1 ','a ')
INSERT INTO medi_medicines (`cc1`,`nn1`,`pp1`) VALUES('demo ','1 ','b')
INSERT INTO medi_medicines (`cc1`,`nn1`,`pp1`) VALUES('demo ','2','a ')
INSERT INTO medi_medicines (`cc1`,`nn1`,`pp1`) VALUES('demo ','2','b')
INSERT INTO medi_medicines (`cc1`,`nn1`,`pp1`) VALUES('demo1','1 ','a ')
INSERT INTO medi_medicines (`cc1`,`nn1`,`pp1`) VALUES('demo1','1 ','b')
INSERT INTO medi_medicines (`cc1`,`nn1`,`pp1`) VALUES('demo1','2','a ')
INSERT INTO medi_medicines (`cc1`,`nn1`,`pp1`) VALUES('demo1','2','b')

期待:

    INSERT INTO medi_medicines (`cc1`,`nn1`,`pp1`) VALUES('demo ','1 ','a ')
    INSERT INTO medi_medicines (`cc1`,`nn1`,`pp1`) VALUES('demo1 ','2 ','b ')

1 个答案:

答案 0 :(得分:0)

是的,因为你嵌套循环3次。这就像2x2x2。

您应该执行以下操作:

$queries = count($code); //This should be = 2
for($i=0;$i<$queries;$i++) {
    echo "...VALUES('".$code[$i]."','".$name[$i]."','".$pack[$i]."')";
}

//我还没有测试过这段代码 //对不起英语