-1

我在 PHP 代码中遇到问题。我从电子书中得到了一些代码,但是当我尝试运行系统时,它给出了一些错误。我通过给出适当的引号修复了一些错误,但现在我遇到了一些其他错误。

如果有人帮助我解决以下错误,我会非常高兴:

注意:未定义的索引:第 4 行 C:\xampp\htdocs\auction\index.php 中的 id

这是我的 index.php 代码:

<?php
require("config.php");
require("functions.php");
$validid = pf_validate_number($_GET['id'], "value", $config_basedir);
require("header.php");
if($validid == 0) {
$sql = "SELECT items.* FROM items WHERE dateends > NOW()";
}
else {
$sql = "SELECT * FROM items WHERE dateends > NOW()
AND cat_id = " . $validid . ";";
}
$result = mysql_query($sql);
$numrows = mysql_num_rows($result);

echo "<h1>Items available</h1>";
echo "<table cellpadding='5'>";
echo "<tr>";
echo "<th>Image</th>";
echo "<th>Item</th>";
echo "<th>Bids</th>";
echo "<th>Price</th>";
echo "</tr>";

if($numrows == 0) {
echo "<tr><td colspan=4>No items!</td></tr>";
}

else {
while($row = mysql_fetch_assoc($result)) {
$imagesql = "SELECT * FROM images WHERE
item_id = " . $row['id'] . " LIMIT 1";
$imageresult = mysql_query($imagesql);
$imagenumrows = mysql_num_rows($imageresult);
echo "<tr>";
if($imagenumrows == 0) {
echo "<td>No image</td>";
}
else {
$imagerow = mysql_fetch_assoc($imageresult);
echo "<td><img src='./images/". $imagerow['name'] . "' width='100'></td>";
}
echo "<td>";
echo "<a href='itemdetails.php?id="
. $row['id'] . "'>" . $row['name'] . "</a>";
if($_SESSION['USERID'] == $row['user_id']) {
echo " - [<a href='edititem.php?id="
. $row['id'] . "'>edit</a>]";
}
echo "</td>";

$bidsql = "SELECT item_id, MAX(amount) AS
highestbid, COUNT(id) AS numberofbids FROM bids
WHERE item_id=" . $row['id'] . " GROUP BY item_id;";
$bidresult = mysql_query($bidsql);
$bidrow = mysql_fetch_assoc($bidresult);
$bidnumrows = mysql_num_rows($bidresult);
echo "<td>";
if($bidnumrows == 0) {
echo "0";
}
else {
echo $bidrow['numberofbids'] . "</td>";
}
echo "<td>" . $config_currency;
if($bidnumrows == 0) {
echo sprintf('%.2f', $row['startingprice']);
}
else {
echo sprintf('%.2f', $bidrow['highestbid']);
}

echo "</td>";
echo "<td>" . date("D jS F Y g.iA",
strtotime($row['dateends'])) . "</td>";
echo "</tr>";
}
}
echo "</table>";
require("footer.php");
?>

你们中的一些人问,id 是从哪里来的。我不知道,但我在其他文件中搜索过,它们是:

bar.php:

<?php
require("header.php");
$catsql = "SELECT * FROM categories ORDER BY category ASC;";
$catresult = mysql_query($catsql);
echo "<h1>Categories</h1>";
echo "<ul>";
echo "<li><a href='index.php'>View All</a></li>";
while($catrow = mysql_fetch_assoc($catresult)) {
echo "<li><a href='index.php?id=". $catrow['id'] . "'>" . $catrow['category']. "</a>                    </li>";
   }
echo "</ul>";

?>

header.php:

<?php
session_start();
require("config.php");
$db = mysql_connect($dbhost, $dbuser, $dbpassword);
mysql_select_db($dbdatabase, $db);
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title><?php echo $config_forumsname; ?></title>
<link rel="stylesheet" href="stylesheet.css" type="text/css" />
</head>
<body>
<div id="header">
<h1>BidTastic Auctions</h1>
<div id="menu">
<a href="index.php">Home</a>
<?php
if(isset($_SESSION['USERNAME']) == TRUE) {
echo "<a href='logout.php'>Logout</a>";
}
else {
echo "<a href='login.php'>Login</a>";
}
?>
<a href="newitem.php">New Item</a>
</div>
<div id="container">
<div id="bar">
<?php require("bar.php"); ?>
</div>
<div id="main">

itemdetails.php

<?php
session_start();
include(“config.php”);
include(“functions.php”);
$db = mysql_connect($dbhost, $dbuser, $dbpassword);
mysql_select_db($dbdatabase, $db);
$validid = pf_validate_number($_GET[‘id’], “redirect”, $config_basedir);

if($_POST[‘submit’]) {
if(is_numeric($_POST[‘bid’]) == FALSE) {
header(“Location: “ . $config_basedir
. “itemdetails.php?id=” . $validid . “&amp;error=letter”);
}

$theitemsql = “SELECT * FROM items WHERE id = “ . $validid . “;”;
$theitemresult = mysql_query($theitemsql);
$theitemrow = mysql_fetch_assoc($theitemresult);
$checkbidsql = “SELECT item_id, max(amount) AS
highestbid, count(id) AS number_of_bids FROM
bids WHERE item_id=” . $validid . “ GROUP BY item_id;”;
$checkbidresult = mysql_query($checkbidsql);
$checkbidnumrows = mysql_num_rows($checkbidresult);

if($checkbidnumrows == 0) {
if($theitemrow[‘startingprice’] > $_POST[‘bid’]) {
header(“Location: “ . $config_basedir
. “itemdetails.php?id=” . $validid . “&amp;error=lowprice#bidbox”);
}
}

else {
$checkbidrow = mysql_fetch_assoc($checkbidresult);
if($checkbidrow[‘highestbid’] > $_POST[‘bid’]) {
header(“Location: “ . $config_basedir . “itemdetails.php?id=” .
$validid . “&amp;error=lowprice#bidbox”);
}
}

$inssql = “INSERT INTO bids(item_id, amount, user_id) VALUES(“
. $validid
. “, “ . $_POST[‘bid’]
. “, “ . $_SESSION[‘USERID’]
. “);”;
mysql_query($inssql);

header(“Location: “ . $config_basedir
. “itemdetails.php?id=” . $validid);
}
else {



$validid = pf_validate_number($_GET[‘id’], “redirect”,
$config_basedir);
require(“header.php”);
$itemsql = “SELECT UNIX_TIMESTAMP(dateends) AS dateepoch,
items.* FROM items WHERE id = “ . $validid . “;”;
$itemresult = mysql_query($itemsql);
$itemrow = mysql_fetch_assoc($itemresult);
$nowepoch = mktime();
$rowepoch = $itemrow[‘dateepoch’];
if($rowepoch > $nowepoch) {
$VALIDAUCTION = 1;
}
echo “&lt;h2>” . $itemrow[‘name’] . “&lt;/h2>”;

$imagesql = “SELECT * FROM images WHERE item_id = “ . $validid . “;”;
$imageresult = mysql_query($imagesql);
$imagenumrows = mysql_num_rows($imageresult);

$bidsql = “SELECT item_id, MAX(amount) AS highestbid,
COUNT(id) AS number_of_bids FROM bids WHERE item_id=”
. $validid . “ GROUP BY item_id;”;
$bidresult = mysql_query($bidsql);
$bidnumrows = mysql_num_rows($bidresult);

echo “&lt;p>”;
if($bidnumrows == 0) {
echo “&lt;strong>This item has had no bids</strong>
- <strong>Starting Price</strong>: “ . $config_currency
. sprintf(‘%.2f’, $itemrow[‘startingprice’]);
}
else {
$bidrow = mysql_fetch_assoc($bidresult);

echo “&lt;strong>Number Of Bids</strong>: “
. $bidrow[‘number_of_bids’] . “
- <strong>Current Price</strong>: “ . $config_currency
. sprintf(‘%.2f’, $bidrow[‘highestbid’]);
}

echo “ - <strong>Auction ends</strong>: “
. date(“D jS F Y g.iA”, $rowepoch);

echo “&lt;/p>”;
if($imagenumrows == 0) {
echo “No images.”;
}
else {
while($imagerow = mysql_fetch_assoc($imageresult)) {
echo “&lt;img src=’./images/” . $imagerow[‘name’] .”’ width=’200’&gt;”;
}
}

echo “&lt;p>” . nl2br($itemrow[‘description’]) . “&lt;/p>”;

echo “&lt;a name=’bidbox’&gt;”;
echo “&lt;h2>Bid for this item</h2>”;

if(isset($_SESSION[‘USERNAME’]) == FALSE) {
echo “To bid, you need to log in. Login
<a href=’login.php?id=” . $validid . “&amp;ref=addbid’&gt;here</a>.”;
}

else {
if($VALIDAUCTION == 1) {
echo “Enter the bid amount into the box below.”;
echo “&lt;p>”;
switch($_GET[‘error’]) {
case “lowprice”:
echo “The bid entered is too low.
Please enter another price.”;
break;
case “letter”:
echo “The value entered is not a number.”;
break;
}
?>

<form action=”&lt;?php echo pf_script_with_get($SCRIPT_NAME);
?>” method=”post”&gt;
<table>
<tr>
<td><input type=”text” name=”bid”&gt;</td>
<td><input type=”submit” name=”submit” value=”Bid!”&gt;</td>
</tr>
</table>
</form>

<?php
}
else {
echo “This auction has now ended.”;
}

$historysql = “SELECT bids.amount, users.username FROM bids,
users WHERE bids.user_id = users.id AND item_id = “
. $validid . “ ORDER BY amount DESC”;
$historyresult = mysql_query($historysql);
$historynumrows = mysql_num_rows($historyresult);
if($historynumrows >= 1) {
echo “&lt;h2>Bid History</h2>”;
echo “&lt;ul>”;
while($historyrow = mysql_fetch_assoc($historyresult)) {
echo “&lt;li>” . $historyrow[‘username’] . “ - “ .
$config_currency . sprintf(‘%.2f’, $historyrow[‘amount’]) . “&lt;/li>”;
}
echo “&lt;/ul>”;
    }
}
}
require(“footer.php”);
?>

函数.php

<?php
function pf_script_with_get($script) {
$page = $script;
$page = $page . "?";
foreach($_GET as $key => $val) {
$page = $page . $key . "=" . $val . "&";
}

return substr($page, 0, strlen($page)-1);
}

function pf_validate_number($value, $function, $redirect) {
if(isset($value) == TRUE) {
if(is_numeric($value) == FALSE) {
$error = 1;
}
if($error == 1) {
header("Location: " . $redirect);
}
else {
$final = $value;
}
}
else {
if($function == 'redirect') {
header("Location: " . $redirect);
}
if($function == "value") {
$final = 0;
}
}
return $final;
}
?>
4

2 回答 2

3

您是否将查询字符串参数传递id给您的页面?像index.php?id=xxx什么?如果没有,$_GET['id']将不会被定义,并且您不能只是访问它。您必须先检查它是否存在,例如array_key_exists.

PHP 在其错误消息中非常非常清楚。当你访问一个数组时,你给它一个“索引”(在 之间的部分[])并且该索引映射到一个值。PHP 告诉您索引"id"不存在于您在第 4 行访问的任何数组中。如果您查看第 4 行,您会看到只有一个数组在该行的一个位置被访问$_GET,并且您可以查看您使用的索引是id.

于 2013-04-19T13:28:55.357 回答
2

你有问题

$_GET['id']

你是怎么得到这个id的,它来自查询字符串吗?不要这么想。放置isset函数来检查id是否在url中。因此,要解决您的问题,请执行此操作

$validid = 0;
if (isset($_GET['id'])) {
    $validid = pf_validate_number($_GET['id'], "value", $config_basedir);
}

把它放在你的 index.php 中

于 2013-04-19T13:28:39.617 回答