2017-08-25 134 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'];} 

并确保您已连接到您的数据库。

相关问题