PHP的内爆代码无法正常工作

时间:2019-06-21 08:50:10

标签: php

我正在使用PHP 7.2 MySQLi添加到购物车。插入产品,例如访客用户详细信息。而产品详细信息将插入所有数据,但不插入与产品相关的数据。我要插入数据产品详细信息。插入的数据,例如

  

(productImage,productName,product_qty,price)通过在数据库中获取foreach循环内爆函数进行插入

请检查一下。帮我...

  <?php   
        $msg='';
        if(isset($_POST['submit'])) {
        $address = mysqli_real_escape_string($conn, $_POST['address']);
        $total = mysqli_real_escape_string($conn, $_POST['total']);
        <!-----------------issue code -------------------->
    foreach($_SESSION["products"] as $product){
        $productName = $product["productName"]; 
        $productPrice = $product["productPrice"];
        $product_qty = $product["product_qty"];
        $productImage1 = $product["productImage1"];                 
     }
        $productImage=implode(',', $productImage);
        $productName=implode(',', $productName);
        $product_qty=implode(',', $product_qty);
        $price=implode(',', $price);
      <!---------------------------------------------->            
        $query = "INSERT INTO orders 
              (address,productImage,productName,product_qty,total,price,) 
        VALUES( '$address','$productImage',
        '$productName','$product_qty','$total','$price')";                              
        $run= mysqli_query($conn, $query);
        unset ($_SESSION["products"]);
        echo "<script>window.location.assign('thankyou.php')</script>";
        } 
  ?>

2 个答案:

答案 0 :(得分:0)

您的查询是一个字符串,因此您实际上在数据库中放置了$ first_name,$ last_name,$ phone_number...。而且您可能为您的列指定了一种类型,因此将int类型的邮政编码获取一个字符串。

答案 1 :(得分:0)

要进行调试,请尝试

<?php   
        $msg='';
        if(isset($_POST['submit'])) {
        $address = mysqli_real_escape_string($conn, $_POST['address']);
        $total = mysqli_real_escape_string($conn, $_POST['total']);
        <!-----------------issue code -------------------->
    foreach($_SESSION["products"] as $product){
        $productName = $product["productName"]; 
        $productPrice = $product["productPrice"];
        $product_qty = $product["product_qty"];
        $productImage1 = $product["productImage1"];                 
     }
        $productImage=implode(',', $productImage);
        $productName=implode(',', $productName);
        $product_qty=implode(',', $product_qty);
        $price=implode(',', $price);
      //add this few lines for debug purpose
      var_dump($productImage);
      var_dump($productName);
      var_dump($product_qty);
      var_dump($price);
      die();
      <!---------------------------------------------->            
        $query = "INSERT INTO orders 
              (address,productImage,productName,product_qty,total,price,) 
        VALUES( '$address','$productImage',
        '$productName','$product_qty','$total','$price')";                              
        $run= mysqli_query($conn, $query);
        unset ($_SESSION["products"]);
        echo "<script>window.location.assign('thankyou.php')</script>";
        } 
  ?>

上面的语句使您可以清楚地了解变量类型及其内部内容。

据我所知,问题可能出在您的$variable未设置为数组,因此php假定它是一个字符串,因此您可能只会得到最后一条记录。试试这个:

<?php   
        $msg='';
        if(isset($_POST['submit'])) {
        $address = mysqli_real_escape_string($conn, $_POST['address']);
        $total = mysqli_real_escape_string($conn, $_POST['total']);
        <!-----------------issue code -------------------->
    $productName = [];//or $productName = array();
    $productPrice = [];
    $product_qty = [];
    $productImage = [];
    foreach($_SESSION["products"] as $product){
        $productName[] = $product["productName"];// or 
        //array_push($productName,$product["productName"]);
        $productPrice[] = $product["productPrice"];
        $product_qty[] = $product["product_qty"];
        $productImage1[] = $product["productImage1"];                 
     }
        $productImage=implode(',', $productImage);
        $productName=implode(',', $productName);
        $product_qty=implode(',', $product_qty);
        $price=implode(',', $price);
      <!---------------------------------------------->            
        $query = "INSERT INTO orders 
              (address,productImage,productName,product_qty,total,price,) 
        VALUES( '$address','$productImage',
        '$productName','$product_qty','$total','$price')";                              
        $run= mysqli_query($conn, $query);
        unset ($_SESSION["products"]);
        echo "<script>window.location.assign('thankyou.php')</script>";
        } 
  ?>

希望有帮助。