在php内部形成while循环而不发布数据

时间:2015-10-30 15:25:48

标签: php html

我正在尝试为网上商店制作购物车系统。该表单应该将信息发布到addtocart.php,但它不会发布数据,我不知道为什么。

有问题的表单:

<?php
$query = "SELECT * FROM parts ORDER BY RAND() LIMIT 6"; 
$result = mysqli_query ($connect, $query);                                                  

while( $record=mysqli_fetch_array($result) ) {

$partnr=$record['partnr'];                                  
$specs=$record['specs'];
$price=$record['price'];

echo    '<div class="col-sm-4 col-lg-4 col-md-4">';
echo    '<div class="thumbnail">';
echo    '<img src="afb/'.$partnr.'.png" alt="">';
echo    '<div class="caption">';
echo    '<h4 class="pull-right">€'.$price.'</h4>';
echo    "<h4><a href='item.php?id=".$partnr."'>".$partnr."</a>";
echo    '</h4>';
echo    '<p> '.$specs.'';

echo    "<form class='form-signin' action='addtocart.php' method='POST' autocomplete='off'>";
echo    "<input type='hidden' name=".$partnr." value=".$partnr.">";
echo    "<input type='hidden' name=".$specs." value=".$specs.">";
echo    "<input type='hidden' name=".$price." value=".$price.">";                       
echo    "<input type='submit' value='Add to cart' class='btn btn-default'>";    

echo    "</form>";  
echo    '</div>';
echo    '</div>';
echo    '</div>';                       
}                                           
?>      

addtocart.php的内容

<?php

session_start();    
require_once('includes/connect.php');

$id = $_SESSION['id'];
$partnr = $_POST['partnr'];
$specs = $_POST['specs'];
$price = $_POST['price'];

echo $id;
echo $partnr;
echo $specs;
echo $price;
echo $_POST['partnr'];
echo $_POST['specs'];
echo $_POST['price'];

$query = "INSERT INTO cart (customer_id, partnr, specs, price, quantity) VALUES ('$id', '$partnr', '$specs', '$price', 1)";
mysqli_query ($connect, $query) or die ("Insert Failed"); 

echo 'window.location = "cart.php";';
?>

addtocart.php的结果

1Insert Failed

1 个答案:

答案 0 :(得分:1)

您正在使用变量的值作为表单中的名称。 将其更改为名称,您将能够正确阅读它们。

echo    "<input type='hidden' name='partnr' value='".$partnr."'>";
echo    "<input type='hidden' name='specs' value='".$specs."'>";
echo    "<input type='hidden' name='price' value='".$price."'>";