【问题标题】:Error insert data into table向表中插入数据时出错
【发布时间】:2014-07-15 09:45:17
【问题描述】:

我有一个关于如何创建购物车的说明,我想进行修改,以便将购物车系统用作餐厅系统,供员工记录客户订单,因此系统不需要记录顾客信息。当我尝试将所有选择的数据插入表中时出现错误,它是 echo sucess,但没有数据插入到数据库中。错误发生在 cart.php 中。

这是错误:

注意:未定义索引:C:\xampp\htdocs\emakengku\cart.php 中的名称在第 7 行

注意:未定义索引:第 8 行的数量 inC:\xampp\htdocs\emakengku\cart.php

注意:未定义索引:第 9 行 C:\xampp\htdocs\emakengku\cart.php 中的价格

代码如下:

index2.php

<?
session_start();
error_reporting(E_ALL);
require("connection.php");
if(isset($_GET['page'])){

    $pages=array("products", "cart");
    if(in_array($_GET['page'], $pages)) {

        $_page=$_GET['page'];

    }else{

        $_page="products";

    }

}else{

$_page="products";

}
?>



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>


<body>
</body>
</html>

<link rel="stylesheet" href="style2.css" />

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js"></script>

<script type="text/javascript">
$(function() {

});
</script> 
</head>

<body>
<div id="container">

<div id="main">
<?php require($_page.".php");     ?>

</div>

<div id="sidebar">
<h1>Cart</h1>

<?php 
if(isset($_SESSION['cart'])){

$sql="SELECT * FROM products WHERE id_product IN (";

foreach($_SESSION['cart'] as $id => $value) {
$sql.=$id.",";
}
$sql=substr($sql, 0, -1).") ORDER BY name ASC";
$query=mysql_query($sql);
while($row=mysql_fetch_array($query)){

?>
<p><?php echo $row['name'] ?> x <?php echo $_SESSION['cart'][$row['id_product']]['quantity'] ?></p>
<?php }

?>

<hr />
<a href="index2.php?page=cart">Go to Cart</a>


<?php





}else{

echo "<p>Your Cart is empty</p>";

}
?>
</div>

</div>
</body>

Products.php

<?php

    if(isset($_GET['action']) && $_GET['action']=="add"){

        $id=intval($_GET['id']);

        if(isset($_SESSION['cart'][$id])){

            $_SESSION['cart'][$id]['quantity']++;

        }else{

            $sql_s="SELECT * FROM products
                WHERE id_product={$id}";
            $query_s=mysql_query($sql_s);
            if(mysql_num_rows($query_s)!=0){
                $row_s=mysql_fetch_array($query_s);



                    $_SESSION['cart'][$row_s['id_product']]=array(
                            "quantity" => 1,
                            "price" => $row_s['price']

                            );


                }else{

                    $message="This product id it's invalid!";


        }
        }
        }




?>


<h1>Product List</h1>
<?php 

    if(isset($message)){
    echo "<h2>$message</h2>";
    }
?>
<table>
<tr>
    <th>Name</th>
    <th>Description</th>
    <th>Price</th>
    <th>Action</th>
</tr>
<?php
$sql="SELECT * FROM products ORDER BY name ASC";

$query=mysql_query($sql);

while ($row=mysql_fetch_array($query)) {

?>
<tr>
    <td><?php echo $row['name'] ?></td>
    <td><?php echo $row['description'] ?></td>
    <td>RM <?php echo $row['price'] ?></td>
    <td><a href="index2.php?page=products&action=add&id=<?php echo $row['id_product'] ?>">Add To Cart</a></td>
</tr>
<?php
}
?>

</table>

cart.php

<?php
error_reporting(E_ALL);

require("connection.php");
if(isset($_POST['submit2'])) {

    $name=$_POST["name"];
    $quantity=$_POST["quantity"];
    $price=$_POST["price"];
$sql_insert = "INSERT INTO order (name, quantity, price)  
values('$name', '$quantity', '$price')"; 
mysql_query($sql_insert);  

echo "sucess!";

}

 if(isset($_POST['submit'])){
 
 foreach($_POST['quantity'] as $key => $val) {
    if($val==0) {
    unset($_SESSION['cart'][$key]);
    }else{
    $_SESSION['cart'][$key]['quantity']=$val;
    


    }
    }
 

 }

?>



<a href="index2.php?page=products">Go back to product page</a>
<h1>View Cart</h1>

<form method="post" action"index2.php?page=cart">

<table>
<tr>
    <th>Name</th>
    <th>Quantity</th>
    <th>Price</th>
    <th>Item Price</th>



</tr>
<?php

    $sql="SELECT * FROM products WHERE id_product IN (";

foreach($_SESSION['cart'] as $id => $value) {
$sql.=$id.",";
}
$sql=substr($sql, 0, -1).") ORDER BY name ASC";
$query=mysql_query($sql);
$totalprice=0;
while($row=mysql_fetch_array($query)){
    $subtotal=$_SESSION['cart'][$row['id_product']]['quantity']*$row['price'];
    $totalprice+=$subtotal;

?>
<tr>
<td><?php echo $row['name'] ?></td>
<td><input type="text" name="quantity[<?php echo $row['id_product'] ?>]" size="5" value="<?php echo $_SESSION['cart'][$row['id_product']]['quantity'] ?>"</td>
<td>RM <?php echo $row['price'] ?></td>
<td>RM <?php echo $_SESSION['cart'][$row['id_product']]['quantity']*$row['price'] ?></td>



</tr>



<?php 
}

?>
<tr>
<td>Total Price: <?php echo $totalprice  ?></td>


</tr>

</table>
<button type="submit" name="submit"> Update Cart</button>
<button type="submit" name="submit2"> Update Cart</button>


</form>
<br />
<p> To remove an item,set the quantity to 0</p>

【问题讨论】:

标签: php


【解决方案1】:

您的表单已损坏,您缺少稍后尝试在脚本($_POST['name'] 等)中使用的名称、数量等输入 - 在文件 cart.php 中。

很难找到它,因为代码非常混乱,但我没有找到任何具有这些名称的输入。

此外,除了使用已弃用的mysql_* 函数等明显缺陷外,您真的不应该混合逻辑并以这种方式查看。在呈现表单的过程中进行数据库查询是不好的。

如果你使用一些 MVC 框架,它会干净得多。

【讨论】:

  • 我遵循的指令刚刚完成,直到更新购物车,但它没有教如何提交确认的数据,所以我自己尝试创建它。我还在学习,这是我第一次创建购物车。对不起
  • 您可能应该寻找一些高质量的教程,而不是您迄今为止所遵循的垃圾。您所拥有的是质量非常差的代码,并且使用了过时的功能。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-03
  • 2014-02-07
  • 2012-10-26
  • 2014-12-10
  • 2023-01-23
相关资源
最近更新 更多