如何使多个mySQL数据库更新从PHP表单与一个表单提交


How to make multiple mySQL database update from PHP form with one form submit?

我正在努力解决这个问题:在我的functions.php文件

中有这个函数
function list_cart()
{   
    global $con;
    $ip = take_ip();
    $query = "select * from cart where ip_adr='$ip'";
    $result = mysqli_query($con, $query);
    $count = mysqli_num_rows($result);
    if($count==0)
        {
            echo "<h2>No items in shopping cart!</h2>";
        }
    while ($rows = mysqli_fetch_array($result)) 
        {
            $id = $rows["art_id"];
            $total = $rows["total"];
            $qty = $rows["qty"];
        echo"    
             <h3>Quantity: <input type='text' name='quantity' size='1' value='$qty' />
             Price: $total € <input type='checkbox' name='remove[]' value='$id'/></h3>";
        }         
}

然后我在我的cart_content.php文件中调用该函数,连同一些打印项目列表的代码,在文本框中显示它们的数量,总价和用于选择要删除的项目的复选框(所有数据都在mySQL表中)。就像它应该的那样。我想添加的是,如果我手动更改这些框中的数量,并按下"更新购物车"按钮,那些标记为删除的项目将被删除,而那些更改数量的项目将被写入mySQL表中的新数量。剩下的代码在这里:

 <?php if(isset($_POST["update"]))
                $ip = take_ip ();
                foreach ($_POST["remove"] as $remove_id) 
                {
                    $query = "delete from cart where art_id='$remove_id' and ip_adr='$ip'";
                    $result = mysqli_query($con, $query);                  
                }
if($result)
{
    header("Location: cart_content.php");
}

我已经尝试了一些我在网上找到的东西,但在这个过程中完全迷路了。我很感激你的帮助。对于那些好奇的人来说,不,这不是生产代码,只是我想做的一些东西。

只需编写程序来执行所需的更新和删除函数,一个接一个。这是一种很常见的做法。

如果您在执行其他操作时执行Select操作,并且没有首先读取整个结果集,则可能会遇到麻烦。

同样,如果您碰巧从查询中获得假的$result值,那意味着您有一个错误。在这种情况下,您需要使用mysqli_error查看错误是什么

生活的例子,

HTML:

  <form id="cartProducts" method="post" action="../../../catalog/modules/cart/?ccsForm=CartProducts" autocomplete="off">
    <div class="Caption">
        <div class="name">Produkt</div>
        <div class="quantity">Ilość</div>
        <div class="price_unit">Cena</div>
        <div class="remove">Usuń</div>
    </div>
    <table width="100%" class="cart">
        <tr>
            <td class="image"><img src="../../../catalog/gfx/A/1140855796_83551.png" >&nbsp;</td>
            <td class="product"><a href="../../../catalog/modules/product/?p=1140855796{}{26}">ACTIVLAB 6 Protein Blend 2000g</a><span class="packet"><br>Smak: wiśnia</span>&nbsp;</td>
            <td class="quantity">
                <a class="minus" href="#"></a>
                <input class="qty" name="qty[1140855796{}{26}]" value="2">
                <a class="plus" href="#"></a>
            </td>
            <td class="price_unit">139,98 zł&nbsp;</td>
            <td ><a href="../../../catalog/modules/cart/?a=delete&p=1140855796{}{26}"><span class="delete_icon"></span></a></td>
        </tr>
        <tr class="separator">
            <td colspan="5"></td>
        </tr>

        <tr class="Buttons">
            <td colspan="5" >
 ....
</form>
PHP快照:

$cart = clsCart::getInstance();
/* @var $cart clsCart */
if (is_array($_POST['qty'])) {
    foreach ($_POST['qty'] as $key => $value) {
    $cart->UpdateQuantity($key, $value);
    }
}

/**
 * updates cart quantity
 * @param type $product_key
 * @param type $new_qty 
 */
public function UpdateQuantity($product_key, $new_qty) {
    $this->_Products[$product_key]['quantity'] = 0;
    $this->AddProduct($new_qty, $product_key);
    $this->Recalculate();
}