使用FOR LOOP从一行插入多行

时间:2018-11-22 09:31:38

标签: php mysql

我可以在MySQL中使用FOR LOOP从一排php插入多行mySQL吗?

我有这样的表格:

<form action="" method="POST" name="inputRecord" id="inputRecord">

<input type="text" name="member" id="member" value="Member A">
<input type="text" name="debit" id="debit" value="5000">
<input type="text" name="credit" id="credit" value="1000">

<input type="submit" name="save" id="save" value="OK">

</form>

如果我提交到mySQL时是这样的:

id |  Member  |  Debit  |  Credit
---------------------------------
1  | Member A |   5000  |   1000
2  | Member A |   5000  |   1000
3  | Member A |   5000  |   1000
4  | Member A |   5000  |   1000
5  | Member A |   5000  |   1000
6  | Member A |   5000  |   1000

这可能是代码:

$var_i = 10;
if(isset($_POST['member'])) {
    $var_detailMember = $_POST['member'];
}
if(isset($_POST['debit'])) {
    $var_detailDebit = $_POST['debit'];
}
if(isset($_POST['credit'])) {
    $var_detailCredit = $_POST['credit'];
}
if (isset($_POST['save'])) {
    for($i=0;$i<=$var_i;$i++) {
        $insertSQL = "INSERT INTO transaction (member, debit, credit) VALUES ('$var_detailMember', '$var_detailDebit', '$var_detailCredit')";
        mysql_select_db($database, $connection);
        mysql_query($insertSQL, $connection) or die(mysql_error());
    }
}

感谢您对我的帮助

1 个答案:

答案 0 :(得分:0)

使用以下代码,将在此处插入10条记录

to