简体   繁体   English

使用PHP和MySQL插入多行

[英]Inserting Multiple Rows with PHP & MySQL

I'm building a website where people can place orders and this is the first time I've had to insert multiple rows at a time and I'm lost. 我正在建立一个人们可以下订单的网站,这是我第一次不得不一次插入多行而且我迷路了。 I know that I need a FOR loop to perform this, but I'm lost as to how to construct the loop. 我知道我需要一个FOR循环来执行此操作,但我对如何构造循环感到很遗憾。 I'm using PHP, MySQL (obviously) with jQuery. 我正在使用PHP,MySQL(很明显)和jQuery。 I'm using the jQuery to .append() a new select box into the form to allow the client to choose another item. 我正在使用jQuery将.append()一个新的选择框放入表单中,以允许客户端选择另一个项目。

This is how I usually construct my code to allow users to insert into the database. 这就是我通常构建代码以允许用户插入数据库的方式。 My question is how and where would I insert a loop that way multiples rows can be submitted all at once without having to insert them one by one. 我的问题是如何以及在何处插入一个循环,这样就可以一次性提交多个行,而无需逐个插入。 Anything would be helpful, thank you. 什么都有帮助,谢谢。

<?php

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

    if (!$_POST['col1'] | !$_POST['col2'] | !$_POST['col3']) { die ("error"); }

    if (!get_magic_quotes_gpc()) {
        $_POST['col1'] = addslashes ($_POST['col1']);
        $_POST['col2'] = addslashes ($_POST['col2']);
        $_POST['col3'] = addslashes ($_POST['col3']);
    }

    $insert = "insert into table (col1, col2, col3) values ('".$_POST['col1']."', '".$_POST['col2']."', '".$_POST['col3']."')";
    mysql_query ($insert);

} else {
    ?>

<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<table>
  <tr>
    <td><input type="text" name="col1"></td>
    <td><input type="text" name="col2"></td>
    <td><input type="text" name="col3"></td>

 //I'm using jQuery .append() to insert more text boxes with names (col1, col2, col3) here 

  </tr>
</table>
<input type="submit" name="submit" value="Submit">
</form>

    <?php
}
?>

My confusion is where to put the loop... I know it should be a FOR loop, but I could never get one to work. 我的困惑在于放置循环...我知道它应该是一个FOR循环,但我永远无法让它工作。 Thanks again for any help. 再次感谢任何帮助。

Be sure you name your inputs uniquely. 确保您唯一地命名您的输入。 But you can name every column like this (see here for example): 但是你可以像这样命名每一列(例如见这里 ):

<input type="text" name="column1[]" />
<input type="text" name="column2[]" />
<input type="text" name="column3[]" />

That way you can access the columns via PHP using a for loop. 这样,您可以使用for循环通过PHP访问列。

for($i = 0; $i < $n; $i++) // If you have $n rows
{
    echo($_POST["column1"][$i]);
    echo($_POST["column2"][$i]);
    echo($_POST["column3"][$i]);
}

To insert multiple rows into your mySQL database use the following syntax (also: see here ). 要在mySQL数据库中插入多行,请使用以下语法(另请参阅: 此处 )。

INSERT INTO
    tbl_name (column1, column2, column3)
VALUES
    (1,2,3),
    (4,5,6),
    (7,8,9);

Now you should be set to build your SQL query. 现在您应该设置为构建SQL查询。

First thing you want to avoid is use same set of names. 您要避免的第一件事是使用相同的名称集。 You may want to name them rowNcolM and then extract them where you check post variables. 您可能希望将它们命名为rowNcolM,然后在检查发布变量的位置提取它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM