<small id='RH1e8'></small><noframes id='RH1e8'>

<i id='RH1e8'><tr id='RH1e8'><dt id='RH1e8'><q id='RH1e8'><span id='RH1e8'><b id='RH1e8'><form id='RH1e8'><ins id='RH1e8'></ins><ul id='RH1e8'></ul><sub id='RH1e8'></sub></form><legend id='RH1e8'></legend><bdo id='RH1e8'><pre id='RH1e8'><center id='RH1e8'></center></pre></bdo></b><th id='RH1e8'></th></span></q></dt></tr></i><div id='RH1e8'><tfoot id='RH1e8'></tfoot><dl id='RH1e8'><fieldset id='RH1e8'></fieldset></dl></div>

<legend id='RH1e8'><style id='RH1e8'><dir id='RH1e8'><q id='RH1e8'></q></dir></style></legend>

  1. <tfoot id='RH1e8'></tfoot>
    • <bdo id='RH1e8'></bdo><ul id='RH1e8'></ul>

    1. MySQLi准备好的语句&amp;foreach 循环

      时间:2023-09-25
      • <bdo id='gSZoe'></bdo><ul id='gSZoe'></ul>
        <legend id='gSZoe'><style id='gSZoe'><dir id='gSZoe'><q id='gSZoe'></q></dir></style></legend>
            <tbody id='gSZoe'></tbody>

              <small id='gSZoe'></small><noframes id='gSZoe'>

            1. <i id='gSZoe'><tr id='gSZoe'><dt id='gSZoe'><q id='gSZoe'><span id='gSZoe'><b id='gSZoe'><form id='gSZoe'><ins id='gSZoe'></ins><ul id='gSZoe'></ul><sub id='gSZoe'></sub></form><legend id='gSZoe'></legend><bdo id='gSZoe'><pre id='gSZoe'><center id='gSZoe'></center></pre></bdo></b><th id='gSZoe'></th></span></q></dt></tr></i><div id='gSZoe'><tfoot id='gSZoe'></tfoot><dl id='gSZoe'><fieldset id='gSZoe'></fieldset></dl></div>
            2. <tfoot id='gSZoe'></tfoot>

                本文介绍了MySQLi准备好的语句&amp;foreach 循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我正在努力修改以下(有效)以使用准备好的语句:

                echo "

                <h4>值$_POSTed from *LINE-ITEMS TABLE* in input.php:</h4><表格>";foreach ($_POST['date'] as $i => $value) {$invNum = $_POST['invNum'];$date = $_POST['date'][$i];$hours = $_POST['hours'][$i];$rate = $_POST['rate'][$i];$dateTotal = $_POST['dateTotal'][$i];回声<tr><td>".$i."</td><td>".$date."</td><td>".$hours."</td><td>".$rate."</td><td>".$dateTotal."</td></tr>";$query = "INSERT INTO Invoice_Line_Items SETINVOICE_NUMBER = '$invNum',日期 = '$日期',小时 = '$小时',RATE = '$rate',DATE_TOTAL = '$dateTotal'关于重复密钥更新INVOICE_NUMBER = VALUES(INVOICE_NUMBER),日期 = 值(日期),小时 = 值(小时),费率 = 值(费率),DATE_TOTAL = VALUES(DATE_TOTAL)";}//结束 foreachecho "</table></div>";

                我一直在尝试调整在同一页面中运行在此之上的(工作)准备好的语句/查询,它将单行插入到不同的表中.但是这个第二个查询(到不同的数据库表中)从源表单中的行项目表的多个(动态#)行插入数据.

                我已经研究了几个小时,但我无法完全弄清楚如何使用行项目循环实现准备好的语句.我以为会是这样,但这不是插入.

                echo "

                <h4>值$_POSTed from *LINE-ITEMS TABLE* in input.php:</h4><表格>";//这是表单中的行项目表;我不必在查询之前获取这些值吗?foreach ($_POST['date'] as $i => $value) {$invNum = $_POST['invNum'];$date = $_POST['date'][$i];$hours = $_POST['hours'][$i];$rate = $_POST['rate'][$i];$dateTotal = $_POST['dateTotal'][$i];//确认变量/值echo "<tr><td>".$i."</td><td>".$date."</td><td>".$hours."</td><td>".$rate."</td><td>".$dateTotal."</td></tr>";$stmt = $mysqli->stmt_init();$query = "INSERT INTO Invoice_Line_ItemsINVOICE_NUMBER = '$invNum',日期 = '$日期',小时 = '$小时',RATE = '$rate',DATE_TOTAL = '$dateTotal'关于重复密钥更新INVOICE_NUMBER = VALUES(INVOICE_NUMBER),日期 = 值(日期),小时 = 值(小时),费率 = 值(费率),DATE_TOTAL = VALUES(DATE_TOTAL)";如果 ($stmt->prepare($query)) {$stmt ->bind_param("ssddd", $invNum, $date, $hours, $rate, $dateTotal);$stmt ->执行();$stmt->close();}//如果 $stmt}//结束 foreachecho "</table></div>";

                有人可以透露一些信息吗?非常感谢.

                svs

                解决方案

                你不需要

                $stmt = $mysqli->stmt_init();

                你应该可以打电话

                $stmt = $mysqli->prepare($query);

                另一个问题是您在循环中内部设置查询.你不应该那样做.将 $stmt 移到循环外,并在设置变量后仅在其中运行 execute.最后需要加上?,让MySQL知道参数

                $query = "INSERT INTO Invoice_Line_ItemsINVOICE_NUMBER = ?,日期 = ?,小时 = ?,率 = ?,DATE_TOTAL = ?关于重复密钥更新INVOICE_NUMBER = VALUES(INVOICE_NUMBER),日期 = 值(日期),小时 = 值(小时),费率 = 值(费率),DATE_TOTAL = VALUES(DATE_TOTAL)";$invNum = $date = $hours = $rate = $dateTotal = '';$stmt = $mysqli->prepare($query);$stmt->bind_param("ssddd", $invNum, $date, $hours, $rate, $dateTotal);foreach ($_POST['date'] as $i => $value) {$invNum = $_POST['invNum'];$date = $_POST['date'][$i];$hours = $_POST['hours'][$i];$rate = $_POST['rate'][$i];$dateTotal = $_POST['dateTotal'][$i];$stmt->execute();}

                I'm struggling to revise the following (which works) to use a prepared statement:

                echo "<div class="debug">
                            <h4>values $_POSTed from *LINE-ITEMS TABLE* in input.php:</h4>
                            <table>";
                
                    foreach ($_POST['date'] as $i => $value) {
                        $invNum     = $_POST['invNum'];
                        $date           = $_POST['date'][$i];
                        $hours      = $_POST['hours'][$i];
                        $rate           = $_POST['rate'][$i];
                        $dateTotal  = $_POST['dateTotal'][$i];
                
                        echo "<tr>
                                    <td>".$i."</td>
                                    <td>".$date."</td>
                                    <td>".$hours."</td>
                                    <td>".$rate."</td>
                                    <td>".$dateTotal."</td>
                                </tr>";
                
                            $query = "INSERT INTO Invoice_Line_Items SET
                                INVOICE_NUMBER  = '$invNum',
                                DATE                = '$date',
                                HOURS               = '$hours',
                                RATE                = '$rate',
                                DATE_TOTAL      = '$dateTotal'
                
                                ON DUPLICATE KEY UPDATE
                                INVOICE_NUMBER  = VALUES(INVOICE_NUMBER),
                                DATE                = VALUES(DATE),
                                HOURS               = VALUES(HOURS),
                                RATE                = VALUES(RATE),
                                DATE_TOTAL      = VALUES(DATE_TOTAL)
                                ";
                
                        } // END foreach
                echo "</table></div>";
                

                I've been trying to adapt the (working) prepared statement/query running above this in the same page, which inserts a single row into a different table. But this 2nd query (into a different db table) inserts data from multiple (dynamic # of) rows from a line-items table within the source form.

                I've been hacking at it for hours but I can't quite sort out how to implement a prepared statement with the line-items loop. I thought it would be along these lines, but this is not inserting.

                echo "<div class="debug">
                            <h4>values $_POSTed from *LINE-ITEMS TABLE* in input.php:</h4>
                            <table>";
                
                // this is the line-items table in the form; don't I have to get these values before the query?
                foreach ($_POST['date'] as $i => $value) {
                    $invNum     = $_POST['invNum'];
                    $date           = $_POST['date'][$i];
                    $hours      = $_POST['hours'][$i];
                    $rate           = $_POST['rate'][$i];
                    $dateTotal  = $_POST['dateTotal'][$i];
                    // confirm vars/values
                    echo "<tr><td>".$i."</td><td>".$date."</td><td>".$hours."</td><td>".$rate."</td><td>".$dateTotal."</td></tr>";
                
                    $stmt = $mysqli->stmt_init();
                    $query = "INSERT INTO Invoice_Line_Items
                        INVOICE_NUMBER  = '$invNum',
                        DATE                = '$date',
                        HOURS               = '$hours',
                        RATE                = '$rate',
                        DATE_TOTAL      = '$dateTotal'
                
                        ON DUPLICATE KEY UPDATE
                        INVOICE_NUMBER  = VALUES(INVOICE_NUMBER),
                        DATE                = VALUES(DATE),
                        HOURS               = VALUES(HOURS),
                        RATE                = VALUES(RATE),
                        DATE_TOTAL      = VALUES(DATE_TOTAL)
                        ";
                
                        if ($stmt->prepare($query)) {
                            $stmt -> bind_param("ssddd", $invNum, $date, $hours, $rate, $dateTotal);
                            $stmt -> execute();
                            $stmt->close();
                        } // if $stmt
                } // END foreach
                
                echo "</table></div>";
                

                Can someone please shed some light? Much appreciated.

                svs

                解决方案

                You don't need

                $stmt = $mysqli->stmt_init();
                

                You should be able to just call

                $stmt = $mysqli->prepare($query);
                

                Another problem is you're setting the query up inside your loop. You shouldn't do that. Move $stmt outside the loop and only run execute inside once you've set your variables up. Finally, you need to add ? so MySQL knows the parameters

                $query = "INSERT INTO Invoice_Line_Items
                    INVOICE_NUMBER  = ?,
                    DATE = ?,
                    HOURS = ?,
                    RATE  = ?,
                    DATE_TOTAL = ?
                
                    ON DUPLICATE KEY UPDATE
                    INVOICE_NUMBER  = VALUES(INVOICE_NUMBER),
                    DATE                = VALUES(DATE),
                    HOURS               = VALUES(HOURS),
                    RATE                = VALUES(RATE),
                    DATE_TOTAL      = VALUES(DATE_TOTAL)
                    ";
                 $invNum = $date = $hours = $rate = $dateTotal = '';
                 $stmt = $mysqli->prepare($query);
                 $stmt->bind_param("ssddd", $invNum, $date, $hours, $rate, $dateTotal);
                
                foreach ($_POST['date'] as $i => $value) {
                    $invNum     = $_POST['invNum'];
                    $date           = $_POST['date'][$i];
                    $hours      = $_POST['hours'][$i];
                    $rate           = $_POST['rate'][$i];
                    $dateTotal  = $_POST['dateTotal'][$i];
                    $stmt->execute();
                }
                

                这篇关于MySQLi准备好的语句&amp;foreach 循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:mysqli_insert_id() 是从整个服务器还是从同一用户获 下一篇:在 Zend 框架上通过 SMTP 发送电子邮件

                相关文章

                最新文章

                <legend id='XCtU5'><style id='XCtU5'><dir id='XCtU5'><q id='XCtU5'></q></dir></style></legend>

                1. <tfoot id='XCtU5'></tfoot>
                    <bdo id='XCtU5'></bdo><ul id='XCtU5'></ul>
                  <i id='XCtU5'><tr id='XCtU5'><dt id='XCtU5'><q id='XCtU5'><span id='XCtU5'><b id='XCtU5'><form id='XCtU5'><ins id='XCtU5'></ins><ul id='XCtU5'></ul><sub id='XCtU5'></sub></form><legend id='XCtU5'></legend><bdo id='XCtU5'><pre id='XCtU5'><center id='XCtU5'></center></pre></bdo></b><th id='XCtU5'></th></span></q></dt></tr></i><div id='XCtU5'><tfoot id='XCtU5'></tfoot><dl id='XCtU5'><fieldset id='XCtU5'></fieldset></dl></div>
                2. <small id='XCtU5'></small><noframes id='XCtU5'>