2017-08-25 127 views
0

我正在製作購物車,當用戶向購物車中添加商品時,它將保存到數據庫中。我已將所有需要的信息放在標籤中。一些信息被重複爲純文本和隱藏輸入。問題是,當我添加一個項目到購物車時,它確實會說「添加項目」但實際上並沒有向數據庫添加任何信息。我已經測試過它是由圖像還是價格等引起的。它只在我離開order_quantity時才起作用。無法將信息從表單存儲到數據庫中

這裏是商店頁面:

<!DOCTYPE html> 
<html> 

    <head> 
     <title>Store</title> 
     <link href="../styles/main.css" rel="stylesheet" type="text/css"/> 
    </head> 

    <body> 
     <header> 
      <center><img src="../images/logo.png"></a></center> 

      <nav> 
       <ul> 
        <center><h3><li>Store</li> 
        <li><a href="cart.php">Cart</a></li> 
        <li><a href="userorders.php">My orders</a></li> 
        <li><a href="logout.php">Logout</a></li></h3></center> 
       </ul> 
      </nav> 
     </header> 

     <div class="left"> 
      <ul> 
       <h4><li>Completes</li> 
       <li><a href="decksstore.php">Decks</li> 
       <li><a href="wheelsstore.php">Wheels</a></li> 
       <li><a href="bearingsstore.php">Bearings</a></li> 
       <li><a href="trucksstore.php">Trucks</a></li> 
       <li><a href="hardwarestore.php">Hardware</a></li></h4> 
      </ul> 
     </div> 


     <div class="main"> 
     <br> 
      <?php 
       require_once('../../connect.php'); 

       $select = "SELECT * FROM tblcompletes"; 
       $response = mysqli_query($dbc, $select); 

       while ($row = mysqli_fetch_array($response)) 
       { 

        echo '<div class="buyproducts"> 
         <form method="post" action="addtocart.php"> 
          <div style="border:1px solid white; background-color:white; border-radius:20px; padding:20px;" align="center"> 
           <img src="data:image/jpeg;base64,'.base64_encode($row['product_image']).'" class="img-responsive"> 
           <h4 class="text-info">'.$row['product_name'].'</h4> 
           <h4 class="text-danger">$'.$row['product_price'].'</h4> 
           <h4>'.$row['product_quantity'].' in stock</h4> 
           Quantity: <input type="text" name="order_quantity" class="form-control" value="1"> 
           <input type="hidden" name="hidden_id" value="'.$row["product_ID"].'"> 
           <input type="hidden" name="hidden_image" value="data:image/jpeg;base64,'.base64_encode($row['product_image']).'" class="img-responsive"> 
           <input type="hidden" name="hidden_name" value="'.$row["product_name"].'"> 
           <input type="hidden" name="hidden_price" value="'.$row["product_price"].'"> 
           <input type="submit" style="margin-top:5px;" class="btn-success" value="Add to cart"> 
          </div> 
         </form> 
        </div>'; 

       } 
      ?> 
     </div> 
    </body> 

</html> 

下面是操作文件:

<?php 
    require_once('../../connect.php'); 

    $hidden_id = $_POST['product_ID']; 
    $hidden_image = $_POST['product_image']; 
    $hidden_name = $_POST['product_name']; 
    $hidden_price = $_POST['product_price']; 
    $order_quantity = $_POST['order_quantity']; 
    $user_email = $_COOKIE['user_email']; 

    if ($order_quantity == NULL) 
    { 
     echo '<script type="text/javascript"> alert("Please enter a valid value"); window.location="completesstore.php"; </script>'; 
    } 

    else 
    { 
     $addtocart = "INSERT INTO tblorders (product_ID, product_iamge, product_name, product_price, order_quantity, user_email) VALUES (?, ?, ?, ?, ?, ?)"; 
     $stmt = mysqli_prepare($dbc, $addtocart); 
     mysqli_stmt_bind_param($stmt, "issdis", $hidden_id, $hidden_image, $hidden_name, $hidden_price, $order_quantity, $user_email); 
     mysqli_stmt_execute($stmt); 

     echo '<script type="text/javascript"> alert("Item added"); window.location="completesstore.php"; </script>'; 

    } 

    mysqli_st_close($stmt); 
    mysqli_close($dbc); 
?> 

在此先感謝。

+0

你的錯誤是什麼? –

+0

沒有錯誤;當表單提交時(使用添加到購物車按鈕),我無法將信息保存到數據庫。 – Dave

+0

NSERT INTO商標(product_ID,product_iamge,product_name,product_price,order_quantity,user_email)VALUES(?,?,?,?,?,?) ----'''是給 ? – tousif

回答

0

變化

$hidden_id = $_POST['product_ID']; 
$hidden_image = $_POST['product_image']; 
$hidden_name = $_POST['product_name']; 
$hidden_price = $_POST['product_price']; 
$order_quantity = $_POST['order_quantity']; 
$user_email = $_COOKIE['user_email']; 

form使用
$hidden_id = $_POST['hidden_id']; 
$hidden_image = $_POST['hidden_image']; 
$hidden_name = $_POST['hidden_name']; 
$hidden_price = $_POST['hidden_price']; 
$order_quantity = $_POST['order_quantity']; 
$user_email = $_COOKIE['user_email']; 

name屬性在addtocart.php文件不同。

+0

謝謝,但那並沒有解決問題。 – Dave

+0

這是您將它保存到** addtocart.php **文件時所犯的錯誤。我不知道如何檢索'$ user_email = $ _COOKIE ['user_email'];'。並且,請解釋「* ..沒有解決問題*」。 @Dave –

+0

更新:我再次測試了您的答案,但未發送圖像信息。這已經解決了這個問題。如果您好奇,我會將用戶電子郵件設置爲登錄時的cookie,然後用於識別訂購該項目的用戶。除此之外,非常感謝您指出我的錯誤,我非常感謝您的意見。 – Dave

0

更新與下面的代碼你的行動文件:

$hidden_id = $_POST['product_ID']; 
$hidden_image = $_POST['product_image']; 
$hidden_name = $_POST['product_name']; 
$hidden_price = $_POST['product_price']; 
$order_quantity = $_POST['order_quantity']; 
$user_email = $_COOKIE['user_email']; 

替換上面的代碼:

$hidden_id = $_POST['hidden_id']; 
$hidden_image = $_POST['hidden_image']; 
$hidden_name = $_POST['hidden_name']; 
$hidden_price = $_POST['hidden_price']; 
$order_quantity = $_POST['order_quantity']; 
$user_email = $_COOKIE['user_email']; 
+0

你的回答@bharat中有什麼新東西。它與以下答案有什麼不同? –

+0

對不起,但沒有刷新頁面,當我回答問題 –

+1

謝謝你。 – Dave

0

添加一些名字進入你的按鈕例如:

<input type="submit" style="margin-top:5px;" class="btn-success" value="Add to cart" name="Submit"> 

然後你addtocart .php加上:

if(isset($_Submit['Submit'])){ 
$hidden_id = $_POST['product_ID']; 
$hidden_image = $_POST['product_image']; 
$hidden_name = $_POST['product_name']; 
$hidden_price = $_POST['product_price']; 
$order_quantity = $_POST['order_quantity']; 
$user_email = $_COOKIE['user_email'];} 

並確保您已連接到您的數據庫。

相關問題