dow56114 2014-07-11 03:39
浏览 26

将数据插入表中时出错

I have follow an instruction on how to create a shopping cart, I want to make a modification so the shopping cart system can be use as a restaurant system for staff to record customer order, so the system do not need to record the customer details. I have error when I try to insert all the chosen data into table, the error happens in cart.php.

Here is the error:

Notice: Undefined index: name in C:\xampp\htdocs\emakengku\cart.phpon line 7

Notice: Undefined index: quantity inC:\xampp\htdocs\emakengku\cart.php on line 8

Notice: Undefined index: price in C:\xampp\htdocs\emakengku\cart.phpon line 9

Here is the code for index.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);
 ini_set('display_errors', '1');
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')";
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>
  • 写回答

1条回答 默认 最新

  • duanpo6079 2014-07-11 04:05
    关注

    Change the sql query like this and try. ie remove the single quotes for the column names.

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

    And make sure that the following values is there. ie, You might have specified the cart.php as the action page of any html form page and the form should use the method "POST".

    $name=$_POST["name"];
    $quantity=$_POST["quantity"];
    $price=$_POST["price"];
    
    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP