dongruo0909 2013-02-25 16:50
浏览 40
已采纳

使用一个PHP表单在MySQL中插入多个记录

INSERT multiple records in MySQL with one PHP form.

Simple 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

<?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>
");
print_r($_POST);
echo("</pre>
");




mysql_close($connection);
?> 

Array results

 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
)

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.

  • 写回答

3条回答 默认 最新

  • doyp9057 2013-03-03 15:56
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?