如何使用一个表单提交从PHP表单进行多个mySQL数据库更新?

时间:2015-09-19 20:56:00

标签: php mysql

我正在努力解决这个问题: 我在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表)。它的工作原理应该如此。我想添加的是如果我手动更改这些框中的数量并按下&#34;更新购物车&#34;按钮标记为删除的项目将被删除,而那些数量已更改的项目将获取在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");
}

我已经尝试了一些我在网上找到但在这个过程中完全丢失的东西。我很感激一些帮助。对于那些想知道的人,不,这不是生产代码,只是我想做的事情。

1 个答案:

答案 0 :(得分:0)

只需编写程序,一个接一个地执行更新和删除所需的功能。这是一件很常见的事情。

如果您在其他操作中执行选择操作,则可能会遇到麻烦,并且不会先读取整个结果集。

此外,如果您碰巧从查询中获得错误的$ result值,则表示您遇到了错误。在这种情况下,你需要使用mysqli_error来查看错误是什么。