0

我需要使用表单来查询数据库来解决错误。我遵循了一个教程,它看起来有一些错误

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&amp;limit=10&amp;page=1&amp;name=".$name.">10</a> |
 <a href=search10.php?cat=$cat&amp;limit=25&amp;page=1&amp;name=".$name.">25</a> |
 <a href=search10.php?cat=$cat&amp;limit=50&amp;page=1&amp;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>&lt;&lt;</b> <a href=search10.php?cat=$cat&amp;limit=$limit& amp;page=$prev_page&amp;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&amp;limit=$limit&amp;page=$a&amp;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&amp;limit=$limit&amp;page=$next_page&        amp;name=".$name."><b>Next</b></a> &gt; &gt;");
  }

  }//end of the if statement

  //all done ?>
4

1 回答 1

0

首先,您不能在 HTML 表单中使用 $_GET['']。

<td width="170"><input name="name" type="text" id="name" value= "<?php echo $_GET['name'] ?>"></td>

$_GET 仅在选择“提交”按钮并运行 results.php 后才相关。这就是您的字段值显示的原因: "?php echo $_GET['name'] ?"

要连接到数据库,请尝试以下语法:

$con = mysql_connect($hostname, $username, $password);

if (!$con){die('Could not connect:' . mysql_error());}

mysql_select_db($dbName, $con);

看看那里的基本语法 - 很多示例代码都缺少大括号 { & } - 特别是在 IF 语句周围。检查这些的 PHP 手册。

您还可能希望将下一部分包含在“else”语句中:

if(!$t){die(mysql_error());}
else { $a....etc }

我也看不到任何名称为“限制”、“类型”或“页面”的字段 - 所以我不确定您希望从哪里获得这些结果。

还有很多事情要做,所以我暂时先讲到这里——阅读与 mysql_query() 相关的其他问题,看看你是否能找到一些答案。

最后一点让我感到困惑:您在 IF 语句中使用以下方法测试数字是否为数字:

...is_numeric($limit) == false || ($page < 0)...

您正在测试一个不是数字的变量,然后测试它是否小于零 - 您肯定想要 is_numeric($limit) == true 吗?

让我知道我的建议是如何发挥作用的,以及您是否在其他地方找到了您需要的答案。

于 2012-09-04T00:51:24.147 回答