用一种PHP形式在MySQL中插入多条记录

用一种PHP形式在MySQL中插入多条记录

本文介绍了用一种PHP形式在MySQL中插入多条记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用一种PHP形式在MySQL中插入多个记录。

INSERT multiple records in MySQL with one PHP form.

简单表单

<form action="process.php" method="post">
<p><label>Beamline ID</label>
<input type="text" name="bline_id[][bline_id]" />
<label>Flow</label>
<input type="text" name="flow[][flow]" />
</p>
<p><label>Beamline ID</label>
<input type="text" name="bline_id[][bline_id]" />
<label>Flow</label>
<input type="text" name="flow[][flow]" />
</p>
<p><label>Beamline ID</label>
<input type="text" name="bline_id[][bline_id]" />
<label>Flow</label>
<input type="text" name="flow[][flow]" />
</p>
<p><label>Beamline ID</label>
<input type="text" name="bline_id[][bline_id]" />
<label>Flow</label>
<input type="text" name="flow[][flow]" />
</p>
<p><label>Beamline ID</label>
<input type="text" name="bline_id[][bline_id]" />
<label>Flow</label>
<input type="text" name="flow[][flow]" />
</p>
<input name="Submit" type="submit" />
</form>

// process.php

//process.php

<?php
// connect to the database
include('connect-db.php');


$cnt = count($_POST['bline_id']);
$cnt2 = count($_POST['flow']);

if ($cnt > 0 && $cnt == $cnt2) {
    $insertArr = array();
    for ($i=0; $i<$cnt; $i++) {
        $insertArr[] = "('" . mysql_real_escape_string($_POST['bline_id'][$i]) . "', '" . mysql_real_escape_string($_POST['flow'][$i]) . "')";
}

 $query = "INSERT INTO bltest (bline_id, flow) VALUES " . implode(", ", $insertArr);
 mysql_query($query) or trigger_error("Insert failed: " . mysql_error());
}

echo("<pre>\n");
print_r($_POST);
echo("</pre>\n");




mysql_close($connection);
?>

数组结果

 Array
 (
 [bline_id] => Array
    (
        [0] => Array
            (
                [bline_id] => 1
            )

        [1] => Array
            (
                [bline_id] => 2
            )

        [2] => Array
            (
                [bline_id] => 3
            )

        [3] => Array
            (
                [bline_id] => 4
            )

        [4] => Array
            (
                [bline_id] => 5
            )

    )

[flow] => Array
    (
        [0] => Array
            (
                [flow] => 11
            )

        [1] => Array
            (
                [flow] => 22
            )

        [2] => Array
            (
                [flow] => 33
            )

        [3] => Array
            (
                [flow] => 44
            )

        [4] => Array
            (
                [flow] => 55
            )

    )

[Submit] => Submit Query
)

INSERT结果当然是5行,但没有为$插入数据bline_id或$ flow。但是查看数组,那是正确的数据。

the INSERT result is of course 5 rows but no data inserted for $bline_id or $flow. But looking at the array, that is the correct data.

推荐答案

OMG !!!!我终于明白了!这是我的愚蠢表格!

OMG!!!! I finally got it! It was my stupid form!

经以下校正-------

corrected below-------

<form action="process.php" method="post">
<p><label>Beamline ID</label>
<input type="text" name="bline_id[]" value="<?php echo $bline_id; ?>" />
<label>Flow</label>
<input type="text" name="flow[]"  value="<?php echo $flow; ?>" />
</p>
<p><label>Beamline ID</label>
<input type="text" name="bline_id[]" value="<?php echo $bline_id; ?>" />
<label>Flow</label>
<input type="text" name="flow[]"  value="<?php echo $flow; ?>" />
</p>
<p><label>Beamline ID</label>
<input type="text" name="bline_id[]"  value="<?php echo $bline_id; ?>"/>
<label>Flow</label>
<input type="text" name="flow[]" value="<?php echo $flow; ?>" />
</p>
<p><label>Beamline ID</label>
<input type="text" name="bline_id[]" value="<?php echo $bline_id; ?>" />
<label>Flow</label>
<input type="text" name="flow[]" value="<?php echo $flow; ?>" />
</p>
<p><label>Beamline ID</label>
<input type="text" name="bline_id[]" value="<?php echo $bline_id; ?>" />
<label>Flow</label>
<input type="text" name="flow[]" value="<?php echo $flow; ?>" />
</p>
<input name="Submit" type="submit" />
</form>

这篇关于用一种PHP形式在MySQL中插入多条记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 05:52