php在MYSQL数据库中插入多行 [英] php insert multiple rows in MYSQL database

查看:101
本文介绍了php在MYSQL数据库中插入多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个HTML表单,该表单从数据库中填充了"foreach"标签.循环,因此名称中的字段具有相同的名称.我要发回数据库的数据来自数组变量.此时,当我插入数据库时​​,我得到了正确的插入时间,只有插入的值(在这种情况下为4次)是相同的值,就像HTML表单的最后一行一样.

I have a HTML form that i populate from database with a "foreach" loop, so the fields in the name have the same name. The data that i want to post back into the database comes from variables that are arrays. At this moment when i insert into database, i get the right TIMES of insert, only the values that are inserted (in this case 4 times) are the same values, as in the last row of the HTML form.

我花了几天时间搜索互联网并重建代码,但找不到解决方案.我试过内爆,甚​​至从VAR中提取值,但请确保我走错了路.我是一个初学者,只是要求回到正确的轨道上.非常感谢...

I have spent days searching the internet, and rebuilding code, but cant find the solution. I tried implode, even extract values from the VAR, but am sure i am on the wrong track. I am a beginner, just asking to be put back on the right track. Thanks so much...

<?php   
        if(isset($_GET['submit']))
        {    
        $client_id = ($value->ID);
        $qry = "INSERT INTO salesorder (client_id)
                VALUES ('$client_id')";
        $result=mysqli_query($mysql,$qry) or die(mysqli_error($mysql));
        $order_id =  mysqli_insert_id($mysql);
        foreach ( $results as $result ) :
                                      
                $food_id = $_GET['foodid']; print_r($food_id);
                $qty = $_GET['qty'];
                $qry="INSERT INTO orderline (order_id, food_id, qty) VALUES ($order_id, '$food_id', '$qty') ";
                $result=mysqli_query($mysql,$qry) ;

                endforeach; 
    }               

    ?>      

    <tbody>
            <form action="" method="GET">
        <?php foreach ( $results as $result ) : ?>
            <tr><td><input name="qty" size="2" type="number"></td>
            <td><?php print($result->food_type); ?></td>
            <td><input name="foodid[]" size="4" type="number" value=<?php print($result->food_id); ?>></td><tr>
            
        <?php
            endforeach;
        ?>
            </tbody>

这是表格的样子...

this is what the form looks like...

因此,首先从数据库动态加载表单.可能是2行或50行...但是我的问题是,在客户填写要下订单的数量后,要读取"订单内容.整个表单,然后将其加载回数据库

So the form is first dynamic loaded, from the database. could be 2 lines, or 50 lines... but my problem is, after the client fills out qty where he wants to place order off, to "read" the whole form, and load it back into the database

推荐答案

如果要从HTML表单中获取数组,则需要循环访问此数组并将每一行分别插入DB.为此,您需要使用准备好的语句和循环.

If you are getting an array from your HTML form then you need to loop on this array and insert each row separately into DB. To do this you need to use prepared statement and a loop.

if (isset($_GET['submit'])) {
    $client_id = $value->ID; // Wherever this value comes from...

    // Insert new sales order
    $stmt = $mysql->prepare('INSERT INTO salesorder (client_id) VALUES (?)');
    $stmt->bind_param('s', $client_id);
    $stmt->execute();
    $stmt->store_result();

    $order_id = $mysql->insert_id;

    // prepare the SQL statement
    $orderline_stmt = $mysql->prepare('INSERT INTO orderline (order_id, food_id, qty) VALUES (?,?,?)');

    // loop on each element from HTML form 
    // i.e. <input name="foodid[]" >
    foreach ($_GET['foodid'] as $key => $food_id) {
        $qty = $_GET['qty']; // should this be an array too?
        // $qty = $_GET['qty'][$key]; <-- if it's also an array

        $orderline_stmt->bind_param('sss', $order_id, $food_id, $qty);
        $orderline_stmt->execute();
    }
}

这篇关于php在MYSQL数据库中插入多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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