简体   繁体   English

遍历行并将INSERT多行插入MySQL

[英]Loop through rows and INSERT multiple rows into MySQL

In a nutshell, I'm looping and trying to INSERT all the rows but it only inserts the last row for as many times that it loops. 简而言之,我正在循环并尝试插入所有行,但它只会将最后一行插入的次数与循环的次数相同。 So, for two loops, it will loop through twice, and insert the last row twice (not inserting the first row at all). 因此,对于两个循环,它将循环两次,并插入最后一行两次(根本不插入第一行)。

I'm using this as the row / items counter: 我将其用作行/项目计数器:

$countRecords;

This is a basic counter that tells me how many rows are included in the previous invoice. 这是一个基本计数器,可以告诉我上一张发票中包含多少行。 This works. 这可行。 In my example, it displays "2" as two items (rows) are in the previous invoice. 在我的示例中,它显示“ 2”,因为前一个发票中有两个项目(行)。

I'm using the first part of the MySQL outside of the loop: 我在循环外使用MySQL的第一部分:

// the MySQL statement out of the loop
$stmt="INSERT INTO o70vm_invoices_items
    (`id`, `invoice_id`, `name`, `desc`, `value`, `amount`, `discount`, `ordering` ) VALUES ";

I'm then running the loop: 然后运行循环:

// loop through the rows
for($rowCounter = 0; $rowCounter < $countRecords; $rowCounter++) 
    {


    // assign variables based on each loop iteration
    $invoiceID = htmlentities($_POST['invoice_id'], ENT_QUOTES);
    $program = htmlentities($_POST['name'], ENT_QUOTES);
    $forWeek = htmlentities($_POST['desc'], ENT_QUOTES);
    $value = htmlentities($_POST['value'], ENT_QUOTES);
    $qty = htmlentities($_POST['amount'], ENT_QUOTES);


    // put the VALUES array string for each row as such
    $ValuesAddToQuery[] ="(NULL, '$invoiceID', '$program', '$forWeek', '$value', '$qty','0.00','1')";

    }

After the loop, I implode the $ValuesAddToQuery array to insert as such: 循环之后,我将$ ValuesAddToQuery数组分解为这样插入:

// add all the values into the statement
$stmt .= implode(',',$ValuesAddToQuery);

echo $stmt;

// execute statement
//mysql_query($stmt) or exit(mysql_error());

As I said, this loop only inserts the last row the same number of times as it loops through the code. 就像我说的那样,此循环仅将最后一行插入与循环遍历代码的次数相同。

My var_dump shows that I have the last row added twice in the array also. 我的var_dump显示我在数组的最后一行也添加了两次。

So much appreciate any help on this. 非常感谢任何帮助。

For an uncut version of my loop, here it is: 对于我的循环的完整版本,这里是:

                $stmt="INSERT INTO o70vm_invoices_items 
                (`id`, `invoice_id`, `name`, `desc`, `value`, `amount`,`discount`,`ordering` ) VALUES ";

                // loop through the rows
                for($rowCounter = 0; $rowCounter < $countRecords; $rowCounter++) 
                    {


                    // assign variables based on each loop iteration
                    $invoiceID = htmlentities($_POST['invoice_id'], ENT_QUOTES);
                    $program = htmlentities($_POST['name'], ENT_QUOTES);
                    $forWeek = htmlentities($_POST['desc'], ENT_QUOTES);
                    $value = htmlentities($_POST['value'], ENT_QUOTES);
                    $qty = htmlentities($_POST['amount'], ENT_QUOTES);


                    // put the VALUES array string for each row as such
                    $ValuesAddToQuery[] ="(NULL, '$invoiceID', '$program', '$forWeek', '$value', '$qty','0.00','1')";


                    }

                    // add all the values into the statement
                    $stmt .= implode(',',$ValuesAddToQuery);

                    echo $stmt."<br /><br />";

                    var_dump($ValuesAddToQuery);

                    // execute statement
                    //mysql_query($stmt) or exit(mysql_error());

For a little more detail, I'm getting the data from the previous invoice here: 有关更多细节,我从这里的前一张发票中获取数据:

    <td>
    <input type="number" title="'.$row->invoice_id_on_items.'" name="invoice_id" size="2" id="invoice_id" value="' . $row->invoice_id_on_items. '" />
</td>

<td>(<a href="getIndItems.php?id=' .$row->item_id. '" target="_blank">EDIT</a>)<br>'.$row->item_id.'</td>';

$_SESSION['itemIDSelected']=$row->item_id;

echo'

<input type="hidden" title="'.$row->item_id.'" name="id" size="13" id="id" value="'.$row->item_id. '" />

<td>
    <input type="text" title="'.$row->forweek.'" name="desc" size="15" id="desc" value="' . $row->forweek. '" />
</td>
<td>
    <input type="text" title="'.$row->program.'" name="name" size="50" id="name" value="' . $row->program. '" />
</td>
<td>
    <input type="number" title="'.$row->fee.'" name="value" size="3" id="value" value="' . $row->fee. '" />
</td>
<td>
    <input type="number" title="'.$row->qty.'" name="amount" size="3" id="amount" value="' . $row->qty. '" />
</td>
';
$Fee = floatval($row->fee);
$Qty = floatval($row->qty);
$ItemFee=$Fee*$Qty;
echo '
<td>
    <input type="text" title="'.$ItemFee.'" name="total_fee" size="3" id="total_fee" value="' . $ItemFee. '" />
</td>

Then, I'm using the already illustrated above loop code to get the data. 然后,我使用上面已经说明的循环代码来获取数据。 I am able to retrieve the past invoice data without issue but wanted to include it in my question so you would have all the necessary information to provide guidance. 我能够毫无问题地检索过去的发票数据,但希望将其包括在我的问题中,以便您获得所有必要的信息以提供指导。

This: 这个:

$invoiceID = htmlentities($_POST['invoice_id'], ENT_QUOTES);
$program = htmlentities($_POST['name'], ENT_QUOTES);
$forWeek = htmlentities($_POST['desc'], ENT_QUOTES);
$value = htmlentities($_POST['value'], ENT_QUOTES);
$qty = htmlentities($_POST['amount'], ENT_QUOTES);

You're getting the same values from $_POST each time through the loop. 每次通过循环,您都将从$_POST获得相同的值 (You are looping through this code multiple times, but each time you are getting "invoice_id", "name", "desc", etc.) (您多次遍历此代码,但是每次都获得“ invoice_id”,“ name”,“ desc”等)

If you are getting multiple values from the same form, each must have a different name which would then be passed via POST to your script. 如果要从同一表单中获取多个值,则每个值必须具有不同的名称,然后将其通过POST传递给脚本。

For example you could add a number after each field and use the loop counter variable. 例如,您可以在每个字段之后添加一个数字,然后使用循环计数器变量。

HTML: HTML:

<input name="invoice_id0" ... />
<input name="name0" ... />
<input name="desc0" ... />

<input name="invoice_id1" ... />
<input name="name1" ... />
<input name="desc1" ... />

PHP - in your loop: PHP-在您的循环中:

$invoiceID = htmlentities($_POST['invoice_id' . $rowCounter], ENT_QUOTES);
$program = htmlentities($_POST['name' . $rowCounter], ENT_QUOTES);
$forWeek = htmlentities($_POST['desc' . $rowCounter], ENT_QUOTES);

But in reality, you really, really, really shouldn't inject user-supplied values into a SQL query like you are doing. 但实际上,您确实不应该像您所做的那样将用户提供的值注入SQL查询中。 Use parameters instead. 请改用参数。 (The logic of getting the appropriate values for each loop iteration from above still applies though. Just don't concatenate it into a VALUES statement.) (尽管仍然适用从上面为每个循环迭代获取适当值的逻辑。只是不要将其连接到VALUES语句中。)

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

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