PHP代码中未定义的索引? [英] Undefined index in PHP code?
本文介绍了PHP代码中未定义的索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的PHP代码存在一些问题。我对变量'$ total'有一个未定义的索引错误。我需要一些帮助来解决这个问题。这是我的整个PHP代码。
I'm having a few problems with my PHP code. I have an undefined index error for a variable, '$total'. I need some help with fixing this. Here is my whole PHP code.
<?php
include('inc/connect.php');
session_start();
?>
<html>
<head>
<title>Cart</title>
<link rel='stylesheet' href='css/main.css' />
</head>
<body>
<?php
$page = 'index.php';
if(isset($_GET['add'])){
$add_id = $_GET['add'];
$quantity = mysql_query("SELECT id, quantity FROM products WHERE id='$add_id'");
while($quantity_row = mysql_fetch_assoc($quantity)){
if($quantity_row['quantity'] !=@$_SESSION['cart_'.$add_id]){
@$_SESSION['cart_'.$_GET['add']]+='1';
header('Location: index.php');
}
else{
header('Location: index.php?err=max');
}
}
}
if(isset($_GET['remove'])){
$_SESSION['cart_'.(int)$_GET['remove']]--;
header("Location: index.php");
}
if(isset($_GET['delete'])){
$_SESSION['cart_'.(int)$_GET['delete']]='0';
header('Location: index.php');
}
function products(){
$get = mysql_query("SELECT id, name, description, price FROM products WHERE quantity > 0 ORDER BY id DESC");
if(mysql_num_rows($get) == 0){
echo "There are no products to display.";
}
else{
while($get_row = mysql_fetch_assoc($get)){
echo '<p>'.$get_row['name'].'<br />'.$get_row['description'].'<br />$'.$get_row['price'].' <a href="cart.php?add='.$get_row['id'].'">Add</a></p>';
}
}
}
function cart(){
foreach($_SESSION as $name => $value){
if($value>0){
if(substr($name, 0, 5)=='cart_'){
$id = substr($name, 5, strlen($name)-5);
$get = mysql_query("SELECT id, name, price FROM products WHERE id='$id'");
while($get_row = mysql_fetch_assoc($get)){
$sub = $get_row['price']*$value;
echo $get_row['name'].' x '.$value.' @ $'.number_format($get_row['price'], 2).' = $'.$sub.' <a href="cart.php?add='.$id.'">[+]</a> <a href="cart.php?remove='.$id.'">[-]</a> <a href="cart.php?delete='.$id.'">[Delete]</a>';
}
}
$total += $sub;
}
}
echo '<br />'.$total;
}
?>
</body>
预先感谢您提供任何帮助。我可能在不久的将来需要更多帮助。
Thanks in advance for any help. I will probably need more help in the near future.
推荐答案
代码第一次尝试 $ total + = $ sub
, $ total
未定义。
The very first time that the code tries to do $total+=$sub
, $total
is not defined.
你需要在 cart()
函数的顶部设置 $ total
:
You need to set $total
at the top of the cart()
function:
function cart(){
$total = 0;
/// rest of code
}
这篇关于PHP代码中未定义的索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文