未定义的索引"产品"第6行

时间:2017-12-06 00:41:07

标签: php html indexing undefined

所以我正在建立一个在线商店。 index.php和view.php文件很好。当我点击index.php中的产品时,它会在view.php文件中正确显示其信息。但是当我在我的view.php文件上点击立即购买时,它应该打开带有产品信息的cart.php文件。 但是我收到以下错误: 注意:未定义的索引:第6行的/storage/ssd3/036/3764036/public_html/carrillo/cart.php中的产品 我怎样才能解决它,为什么会这样呢? 这是我的cart.php代码:



<!doctype html>
<html>
<head>
<?php
include('config.php');
$productid=$_GET['product'];
?>
<meta charset="utf-8">
<title>Cart</title>
<link rel="stylesheet" href="style.css" type="text/css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.min.css">
<link href="https://fonts.googleapis.com/css?family=Roboto+Condensed|Rubik" rel="stylesheet">
<style>
#undr{width:100%; height:580px; position:absolute; top:75px; left:0px;}
.bdimg{width:100%; height:auto}
.big-outer{ width:80%; height:100%; background:rgba(255,255,255,0.7); margin:auto}
.big-outer p{ font-size:60px; text-align:center; margin:0px;}
.upper-details{background:#EFEFEF;}
.upper-details td{text-align:center;}
td{text-align: center;}
#emptycart{font-size:20px;margin-bottom:15px;color:#111; float:right}
#emptycart:hover{ color:#fff}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>

<body>
	<header>
		<?php
			include('head.php');
		?>
	</header>
	
<div id="undr">
		
	</div>
<?php
if (isset($_POST['pid'])) {
    $pid = $_POST['pid'];
	$wasFound = false;
	$i = 0;
	if (!isset($_SESSION["cartshop"]) || count($_SESSION["cartshop"]) < 1) { 
		$_SESSION["cartshop"] = array(0 => array("item_id" => $pid, "quantity" => 1));
	} else {
		foreach ($_SESSION["cartshop"] as $each_item) { 
		      $i++;
		      while (list($key, $value) = each($each_item)) {
				  if ($key == "item_id" && $value == $pid) {
					  array_splice($_SESSION["cartshop"], $i-1, 1, array(array("item_id" => $pid, "quantity" => $each_item['quantity'] + 1)));
					  $wasFound = true;
				  }
		      }
	       }
		   if ($wasFound == false) {
			   array_push($_SESSION["cartshop"], array("item_id" => $pid, "quantity" => 1));
		   }
	}
	header("location: cart.php"); 
    exit();
}
?>
<?php
if (isset($_GET['cmd']) && $_GET['cmd'] == "emptycart") {
    unset($_SESSION["cartshop"]);
}
?>
<?php
if (isset($_POST['item_to_adjust']) && $_POST['item_to_adjust'] != "") {
	$item_to_adjust = $_POST['item_to_adjust'];
	$quantity = $_POST['quantity'];
	$quantity = preg_replace('#[^0-9]#i', '', $quantity);
	if ($quantity >= 11) { $quantity = 10; }
	if ($quantity < 1) { $quantity = 1; }
	if ($quantity == "") { $quantity = 1; }
	$i = 0;
	foreach ($_SESSION["cartshop"] as $each_item) { 
		      $i++;
		      while (list($key, $value) = each($each_item)) {
				  if ($key == "item_id" && $value == $item_to_adjust) {
					  array_splice($_SESSION["cartshop"], $i-1, 1, array(array("item_id" => $item_to_adjust, "quantity" => $quantity)));
				  }
		      }
	}
}
?>
<?php
if (isset($_POST['index_to_remove']) && $_POST['index_to_remove'] != "") {
 	$key_to_remove = $_POST['index_to_remove'];
	if (count($_SESSION["cartshop"]) <= 1) {
		unset($_SESSION["cartshop"]);
		header('Location: ' . $_SERVER['HTTP_REFERER']);
	} else {
		unset($_SESSION["cartshop"]["$key_to_remove"]);
		sort($_SESSION["cartshop"]);
		header('Location: ' . $_SERVER['HTTP_REFERER']);
	}
}
?>
    <div id="lowrbdy">
	  <div class="big-outer">
   		<p style="text-decoration:underline">Cart</p>
<table width="100%" border="0" style="border-collapse:collapse">
<?php
if(isset($_SESSION['cartshop'])==!NULL){
?>
  <tbody>
    <tr class="upper-details">
      <td height="36" colspan="2" style="border-right: 1px solid #000;">Item</td>
      <td width="16%" style="border-right: 1px solid #000;">Quantity</td>
      <td width="16%" style="border-right: 1px solid #000;">Unit Price</td>
      <td width="21%">Sub Total</td>
    </tr>
<?php
}
?>
<?php 
$cartTotal = "";
if (!isset($_SESSION["cartshop"]) || count($_SESSION["cartshop"]) < 1) {
    echo '<div class="empty-cart"><h2 class="crta">Your Shopping Cart Is Empty</h2>';
	echo '<br><a href="index.php"><h2 class="alink">Continue Shopping</h2></a></div>';
} else {
$i = 0; 
    foreach ($_SESSION["cartshop"] as $each_item) { 
		$item_id = $each_item['item_id'];
		$sql = mysql_query("SELECT * FROM product WHERE id='$item_id' LIMIT 1");
		while ($row = $row = $sql->fetch_assoc()) {
			$productname = $row["name"];
			$producttotalprice = $row["price"];
			$productcode = $row["id"];
			$pic=$row['pro_image'];
			$pdelc=$row['pdelc'];
			$pr=$row['price'];
		}
		$producttotalpricetotal = $producttotalprice * $each_item['quantity'];
		$cartTotal = $producttotalpricetotal + $cartTotal;
echo'<tr>
      <td width="7%" rowspan="3" style="border-bottom: 2px solid #000; height:100px"><img src="imeg/'.$pic.'"/></td>
      <td width="29%" height="21" style="border-right: 1px solid #000;">&nbsp;</td>
      <td rowspan="2" style="border-right: 1px solid #000;">
		  <form action="cart.php" method="post">
		  	<input name="quantity" id="quantity" type="text" value="' . $each_item['quantity'] . '" size="1" maxlength="2" class="qnttxt"/></br>
			<input id="adjustBtn" name="adjustBtn' . $item_id . '" type="submit" value="Update" class="qntbtn"/>
			<input name="item_to_adjust" type="hidden" value="' . $item_id . '" />
		  </form>
	  </td>
      <td style="border-right: 1px solid #000;">&nbsp;</td>
      <td>&nbsp;</td>
      </tr>
    <tr style="border-bottom: 2px solid #000;">
      <td style="border-right: 1px solid #000;">'.$productname.'</td>
      <td style="border-right: 1px solid #000;">Rs. '.number_format($producttotalprice).'</td>
      <td><p style="float:left;margin:0px 0px 0px 20px;font-size:18px;text-decoration:none">Rs. '.number_format($producttotalprice*$each_item['quantity']).'</p>
	  	<form action="cart.php" method="post">
			<input name="deleteBtn' . $item_id . '" type="submit" value="X" class="removebtn"/>
			<input name="index_to_remove" type="hidden" value="' . $i . '" />
        </form>
	  </td>
    </tr>
    <tr>';
	$i++;
	}
echo'<div style="width:400px; height:40px; background:rgba(100,190,255,1.00); margin:auto; margin-bottom:6px;margin-top:10px">
<p style="font-size:20px;text-align:center; color:#fff; line-height:2em">Cart Total: <strong>Rs. '.number_format($cartTotal).' /-</strong></p></div>
<a href="clear-session.php"><div style="width:55%; height:22px;"><p id="emptycart">( Empty Cart )</p></div></a>
';
}
?>
  </tbody>
</table>
        </div>
    </div>
</body>
</html>
&#13;
&#13;
&#13; 这是我的view.php代码:

&#13;
&#13;
<!doctype html>
<html>
<head>
<?php

$mysqli = new mysqli("localhost", "id3764036_alan", "agro12345", "id3764036_agrotienda");


$productid=$_GET['product'];
?>
<meta charset="utf-8">
<title>View Product <?=$productid?></title>
<link rel="stylesheet" href="style.css" type="text/css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.min.css">
<link href="https://fonts.googleapis.com/css?family=Roboto+Condensed|Rubik" rel="stylesheet">
<style>
#undr{width:100%; height:580px; position:absolute; top:75px; left:0px;}
.bdimg{width:100%; height:100%}
.big-outer{ width:80%; height:100%; background:rgba(255,255,255,0.7); margin:auto}
.big-outer p{text-align:center; font-size:40px; margin:10px auto}
.outer{ width:270px; height:310px;  margin:auto;}
.outer img {width:88% !important;}
.price{text-align:center; margin:20px auto; background:#16B472; color:#fff; width:30%; padding:5px 0px;}
.price p{margin:0px; font-size:26px;}
.buy{text-align:center; margin:20px auto; background:#3E8BDC; width:34%; padding:5px 0px; cursor:pointer}
.buy:hover{ background:#2E5AE4;transition:all 0.4s ease-in-out}
#subaz{border:none; background-color:transparent; font-size:32px; color:#fff; font-weight:bold; cursor:pointer}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>

<body>
	<header>
		<?php
			include('head.php');
		?>
	</header>
	
    <div id="undr">
		
	</div>
<?php
$select_query="select * from product where id='$productid'";
$sql=mysqli_query($mysqli,"select * from product where id='$productid'");

$row=$row = $sql->fetch_assoc();
?>
<form id="form1" name="form1" method="post" action="cart.php">
<input type="hidden" name="pid" id="pid" value="<?= $productid ?>" />
    <div id="lowrbdy">
		<div class="big-outer">
        	<div class="outer">
            	<img src="imeg/<?=$row["pro_image"]?>"/>
            </div>
            <p><?=$row["name"]?></p>
            <div class="price"><p>₡ <?=$row["price"]?></p></div>
            <div class="buy"><input type="submit" id="subaz" value="Buy Now"/></div>
        </div>
    </div>
</form>
</body>
</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:-1)

Cart.php:

$productid=$_GET['product'];

但请求提供了 pid &#39;参数。

View.php:

<input type="hidden" name="pid" id="pid" value="<?= $productid ?>" />

所以你有2个解决方案:

1-在cart.php中编辑第6行以匹配参数 pid ,如

$productid=$_POST['pid'];

2-或在view.php中编辑输入的名称,如:

<input type="hidden" name="product" id="pid" value="<?= $productid ?>" />

在view.php中将方法更改为GET,或者在cart.php中使用$ _POST []而不是$ _GET []