2014-07-15 2 views
0

장바구니를 만드는 방법에 대한 지침을 따르고 있으므로 장바구니 시스템을 고객 주문을 기록 할 직원 용 레스토랑 시스템으로 사용할 수 있도록 수정하려고합니다. 고객 세부 정보를 기록 할 필요가 없습니다. 선택한 데이터를 테이블에 모두 삽입하려고하면 오류가 발생하며 성공이지만 데이터베이스에 데이터가 삽입되지 않습니다. cart.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

012,351,641

index2.php : 코드 여기

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

된다 : 여기

오류이며 파일 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> 
+0

[PHP : "알림 : 정의되지 않은 변수"및 "공지 : 정의되지 않은 색인"] 복제본 (http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined- 색인) – Jocelyn

답변

0

양식이 깨진, 당신은 스크립트 ($_POST['name'] 등)에 사용하려고 나중에 입력 등의 이름, 수량을 놓치고있어.

코드가 너무 복잡하기 때문에 찾기가 어렵지만 그 이름을 가진 입력을 찾을 수 없었습니다.

또한 더 이상 사용되지 않는 mysql_* 기능을 사용하는 것과 같은 명백한 결함 외에도 논리를 혼합해서 보면 안됩니다. 양식 렌더링 중 데이터베이스 쿼리를 수행하는 것이 좋지 않습니다.

MVC 프레임 워크를 사용했다면 훨씬 더 깨끗합니다.

+0

지침은 내가 업데이트 카트까지 끝내기 만했으나 확인 된 데이터를 제출하는 방법을 가르쳐주지 못했습니다. 그래서 나는 그것을 배우기 위해 스스로 시도합니다. 아직 학습 중이며 쇼핑 카트를 만드는 것은 처음입니다 .sorry –

+0

당신은 아마도 당신이 지금까지 따라 왔던 쓰레기 대신에 약간의 자습서를 찾아야 할 것입니다. 당신이 가진 것은 매우 품질이 좋지 않은 코드이며 구식 기능을 사용하는 것입니다. – MightyPork

+0

이것은 매우 슬픈 일입니다. –

관련 문제