繁体   English   中英

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

[英]I am trying to get the results page to display the database information based on what is in the form fields

我需要使用一种形式来查询数据库查询错误。 我遵循了一个教程,它看起来有一些错误

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

首先,您不能在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 }

我也看不到任何名称为“ limit”,“ type”或“ page”的字段-因此,我不确定您希望从何处获得这些结果。

还有很多事情要做,所以我暂时就不说了-阅读与mysql_query()有关的其他问题,看看是否能找到一些答案。

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

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

您正在测试变量不是数字,但是然后测试以查看它是否小于零-您肯定希望is_numeric($ limit)== true吗?

让我知道我的建议是如何产生的,以及您是否在其他地方找到需要的答案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM