用PHP&插入多行的MySQL [英] Inserting Multiple Rows with PHP & MySQL

查看:74
本文介绍了用PHP&插入多行的MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个可以让人们下订单的网站,这是我第一次必须一次插入多行并且迷路了.我知道我需要一个FOR循环来执行此操作,但是我对如何构造循环一无所知.我正在使用PHP,MySQL(显然)和jQuery.我正在使用jQuery将新的选择框.append()添加到表单中,以允许客户端选择其他项.

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. I'm using PHP, MySQL (obviously) with jQuery. I'm using the jQuery to .append() a new select box into the form to allow the client to choose another item.

这通常是我构造代码以允许用户插入数据库的方式.我的问题是,如何以及在哪里插入循环,这样就可以一次提交所有多个行,而不必一个接一个地插入它们.一切都会有帮助的,谢谢.

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
}
?>

我的困惑是在哪里放置循环……我知道它应该是一个FOR循环,但是我永远都无法工作.再次感谢您的帮助.

My confusion is where to put the loop... I know it should be a FOR loop, but I could never get one to work. 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[]" />

这样,您可以使用for循环通过PHP访问列.

That way you can access the columns via PHP using a for loop.

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

要在MySQL数据库中插入多行,请使用以下语法(另请参见:

To insert multiple rows into your mySQL database use the following syntax (also: see here).

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

现在您应该设置为建立SQL查询.

Now you should be set to build your SQL query.

这篇关于用PHP&amp;插入多行的MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆