我试图让结果页面根据表单字段中的内容显示数据库信息

时间:2012-09-03 23:53:50

标签: php mysql database

我需要使用表单来查询数据库。我按照教程,看起来有一些错误

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 ?>

1 个答案:

答案 0 :(得分: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);

看看那里的基本语法 - 大括号{&amp;许多示例代码中都缺少这些代码 - 特别是在IF语句周围。 Check the PHP manual on these.

您也可以将下一部分括在“其他”声明中:

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

我也看不到任何名称为'limit','type'或'page'的字段 - 所以我不确定你希望从哪里获得这些结果。

还有很多事要做,所以我现在就把它留在那里 - 阅读其他与mysql_query()相关的问题,看看你是否能找到一些答案。

让我困惑的最后一点:你在IF语句中测试一个数字是否是数字使用:

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

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

让我知道我的建议是如何解决的,以及您是否在其他地方找到了所需的答案。