我正在处理填充下拉菜单的页面,从sql查询中显示属于某个用户的车辆。一旦选择了车辆,它就会运行一个功能,打开一张带有与该车辆相关的发票的桌子。问题是发票的数量相当大,我想使用分页来前进和后退。到目前为止,它提出了第一页就好了。当我点击下一步它给我一个错误说未定义的索引q它也失去了我所有的HTML格式,很可能是因为转发到我的getuser.php页面。请看看我的代码并告诉我我做错了什么,我一直试图让它工作几个小时
securedpage.php的源代码
<?php
include('config.inc');
include('func.inc.php');
include('db.inc.php');
$hostname = 'localhost'; // Your MySQL hostname. Usualy named as 'localhost', so you're NOT necessary to change this even this script has already online on the internet.
$dbname = 'ServiceHistoryDB'; // Your database name.
$username = 'root'; // Your database username.
$password = ''; // Your database password. If your database has no password, leave it empty.
// Let's connect to host
mysql_connect($hostname, $username, $password) or DIE('Connection to host is failed, perhaps the service is down!');
// Select the database
mysql_select_db($dbname) or DIE('Database name is not available!');
// This could be supplied by a user, for example
$username = $_SESSION['username'];
// Formulate Query
// This is the best way to perform an SQL query
// For more examples, see mysql_real_escape_string()
$query = sprintf("SELECT * FROM customers
WHERE username='%s'",
mysql_real_escape_string($username));
// Perform Query
$result = mysql_query($query);
// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
// Use result
// Attempting to print $result won't allow access to information in the resource
// One of the mysql result functions must be used
// See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc.
while ($row = mysql_fetch_assoc($result)) {
echo $row['CustNum'];
$custnum = $row['CustNum'];
}
// Free the resources associated with the result set
// This is done automatically at the end of the script
mysql_free_result($result);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Arndt Automotive</title>
<style type="text/css">
body {
background-image: url(Background.png);
}
</style>
</head>
<body>
<table width="1050" border="0" align="center">
<tr align="center">
<td><img src="Banner.png" width="1050" height="275" alt="Banner" /></td>
</tr>
</table>
<table width="1200" height="638" border="0" align="center" background="backgroundmaybe2.png" " style="background-repeat: no-repeat; background- position: center;>
<tr align="center">
<td><table width="1090" height="638" border="0" align="center">
<tr align="left">
<td width="225"><table width="225" height="638" border="0" id="Left">
<tr>
<td align="center" valign="top" background="backgroundtable1.png"><p> </p>
<p><a href="index.php"><img src="Home.png" width="170" height="30" alt="Home" /></a></p>
<p><a href="AboutUs.php"><img src="About Us.png" width="170" height="30" alt="About Us" /></a></p>
<p><a href="Services.php"><img src="Services.png" width="170" height="30" alt="Services" /></a></p>
<p><a href="RecentWork.php"><img src="Recent Work.png" width="170" height="30" alt="Recent Work" /></a></p>
<p><a href="Login.php"><img src="My Account.png" width="170" height="30" alt="My Account" /></a></p>
<p><a href="ContactUs.php"><img src="Contact Us.png" width="170" height="30" alt="Contact Us" /></a></p>
<p><a href="Promotions.php"><img src="Promotions.png" width="170" height="30" alt="Promotions" /></a></p>
<p><a href="Location.php"><img src="Location.png" width="170" height="30" alt="Location" /></a></p>
<p><img src="Hours.png" width="170" height="150" alt="Hours" /></p></td>
</tr>
</table></td>
<td width="125"> </td>
<td width="850"><table width="850" height="638" border="0" background="backgroundtable2.png" >
<tr>
<td align="center"><html>
<head>
<title></title>
<script>
function showUser(str)
{
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
</script>
<body>
<?php echo $username?></b>
<?php echo $custnum?></b>
<form>
<select name="dropdown" onChange="showUser(this.value)">
<?php query() ;
?>
</select>
<?php close() ?>
</form>
<div id="txtHint"><b>Invoice info will be listed here.</b></div></td>
<p><a href="logout.php">Logout</a></p>
</tr>
</table></td>
</tr>
</table></td>
</tr>
</table>
<table width="1050" height="275" border="0" align="center" background="Footer.png">
<tr>
<td><table width="950" border="0" align="center">
<tr>
<td width="256" align="left"><a href="http://www.acdelco.com/"><img src="AC Delco Logo.png" width="245" height="47" alt="AC Delco" /></a></td>
<td width="265" align="center"><a href="http://www.asashop.org/"><img src="ASA Logo.png" width="248" height="115" alt="ASA" /></a></td>
<td width="141" align="center"><a href="https://www.ase.com/Home.aspx"><img src="ASE Logo.png" width="112" height="115" alt="ASE" /></a></td>
<td width="270"><a href="http://www.jasperengines.com/index.php"><img src="Jasper Logo.png" width="252" height="81" alt="Jasper" /></a></td>
</tr>
</table></td>
</tr>
</table>
<p> </p>
</body>
</html>
这是getuser.php的源代码
<?PHP
// Connects to your Database
mysql_connect('localhost', 'root') or die(mysql_error());
mysql_select_db("ServiceHistoryDB") or die(mysql_error());
$q=$_GET["q"];
//This checks to see if there is a page number. If not, it will set it to page 1
if (!(isset($pagenum)))
{
$pagenum = 1;
}
//Here we count the number of results
//Edit $data to be your query
$data = mysql_query("SELECT * FROM invoices") or die(mysql_error());
$rows = mysql_num_rows($data);
//This is the number of results displayed per page
$page_rows = 10;
//This tells us the page number of our last page
$last = ceil($rows/$page_rows);
//this makes sure the page number isn't below one, or more than our maximum pages
if ($pagenum < 1)
{
$pagenum = 1;
}
elseif ($pagenum > $last)
{
$pagenum = $last;
}
//This sets the range to display in our query
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;
//This is your query again, the same one... the only difference is we add $max into it
$data_p = mysql_query("SELECT * FROM invoices WHERE CarNum = '".$q."' $max") or die(mysql_error());
//This is where you display your query results
echo "<table border='1'>
<tr>
<th>Date</th>
<th> Service </th>
</tr>";
while($info = mysql_fetch_array( $data_p ))
{ echo "<tr>";
echo "<td>" .date('M d Y', strtotime($info['Date'])) . "</td>";
echo "<td>" . $info['BriefDescription'] . "</td>";
echo "<br>";
echo "</tr>";
}
echo "</table>";
// This shows the user what page they are on, and the total number of pages
// First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page.
if ($pagenum == 1)
{
}
else
{
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
echo " ";
$previous = $pagenum-1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
}
//just a spacer
echo " ---- ";
//This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
if ($pagenum == $last)
{
}
else {
$next = $pagenum+1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
echo " ";
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
}
?>