我需要使用表单来查询数据库来解决错误。我遵循了一个教程,它看起来有一些错误
http://bite-hard.com/pitmatch/pm.html
我似乎无法显示结果
形式:
<form name="form1" method="get" action="results.php">
<table width="300" border="0">
<tr>
<td width="130">Age</td>
<td width="170"><input name="name" type="text" id="name" value= "<?php echo $_GET['name'] ?>"></td>
</tr>
<tr>
<td>State</td>
<td><select name="location" id="location">
<option>Select</option>
<option value="Co">Co</option>
<option value="Fl">Fl</option>
<option value="Il">Il</option>
</select></td>
</tr>
<tr>
<td>City</td>
<td><input name="city" type="text" id="city"></td>
</tr>
</table>
</form>
结果页面:
<?
$hostname = "localhost"; // Our DB server.
$username = "bitehard_gdub"; // The username you created for this database.
$password = "reakwon"; // The password you created for the username.
$usertable = "dogs"; // The name of the table you made.
$dbName = "bitehard_pitmatch"; // This is the name of the database you made.
//if there is a search term
if(isset($_GET['name']) && !empty($_GET['name']))
{
//REMEMBER TO CONNECT TO DATABASE!
mysql_connect($hostname, $username, $password) or die(mysql_error());
mysql_select_db("$dbName") or die(mysql_error());
//**EDIT TO YOUR TABLE NAME, ECT.
$t = mysql_query("SELECT city, state, age FROM dogs WHERE name LIKE '%" . $_GET['name'] . "%'");
if(!$t) die(mysql_error());
$a = mysql_fetch_object($t);
$total_items = mysql_num_rows($t);
$limit = $_GET['limit'];
$type = $_GET['type'];
$page = $_GET['page'];
$name = urlencode($_GET['name']);
//set default if: $limit is empty, non numerical, less than 10, greater than 50
if((!$limit) || (is_numeric($limit) == false) || ($limit < 10) || ($limit > 50)) {
$limit = 10; //default
}
//set default if: $page is empty, non numerical, less than zero, greater than total available
if((!$page) || (is_numeric($page) == false) || ($page < 0) || ($page > $total_items)) {
$page = 1; //default
}
//calcuate total pages
$total_pages = ceil($total_items / $limit);
$set_limit = $page * $limit - ($limit);
echo $limit;
//query: **EDIT TO YOUR TABLE NAME, ECT.
$q = mysql_query("SELECT city, state, age FROM dogs WHERE name LIKE '%" . $_GET['name'] . "%' LIMIT $set_limit, $limit");
if(!$q) die(mysql_error());
$err = mysql_num_rows($q);
if($err == 0) die("No matches met your criteria.");
//Results per page: **EDIT LINK PATH**
echo("
<a href=search10.php?cat=$cat&limit=10&page=1&name=".$name.">10</a> |
<a href=search10.php?cat=$cat&limit=25&page=1&name=".$name.">25</a> |
<a href=search10.php?cat=$cat&limit=50&page=1&name=".$name.">50</a>");
//show data matching query:
while($code = mysql_fetch_object($q)) {
echo("<BR>");
echo("<BR>");
echo("name: ".$code->Age."<BR>");
echo("city: ".$code->city."<BR>");
echo("state: ".$code->state."<BR>");
}
$cat = urlencode($cat); //makes browser friendly
//prev. page: **EDIT LINK PATH**
$prev_page = $page - 1;
if($prev_page >= 1) {
echo("<b><<</b> <a href=search10.php?cat=$cat&limit=$limit& amp;page=$prev_page&name=".$name."><b>Prev.</b></a>");
}
//Display middle pages: **EDIT LINK PATH**
for($a = 1; $a <= $total_pages; $a++)
{
if($a == $page) {
echo("<b> $a</b> | "); //no link
} else {
echo(" <a href=search10.php?cat=$cat&limit=$limit&page=$a&name=".$name."> $a </a> | ");
}
}
//next page: **EDIT THIS LINK PATH**
$next_page = $page + 1;
if($next_page <= $total_pages) {
echo("<a href=search10.php?cat=$cat&limit=$limit&page=$next_page& amp;name=".$name."><b>Next</b></a> > >");
}
}//end of the if statement
//all done ?>