嗨,从我的表单中搜索不会带来MySQL数据库中表格中的数据。
e.g。我想搜索邮政编码并带回:姓名,地址,联系电话,邮政编码。
任何人都可以帮助我找到我的代码中的错误,因为我不熟悉PHP和MySQL
这是phpmyadmin的表格条目
参考,名称,Line1,Line2,Line3,Line4,Line5,邮政编码,电话,手机,传真,电子邮件
表格
<td><form action="searchresults.php" method="post" name="form1" id="form1">
<table width="100%" border="0" cellspacing="1" cellpadding="3">
<tr>
<td colspan="3"><strong>Find a Active Physio</strong></td>
</tr>
<tr>
<td width="100">Physio Reference</td>
<td width="301"><input name="PhysioReference" type="text" id="PhysioReference" /></td>
</tr>
<tr>
<td>Name of Physio</td>
<td><input name="Physio" type="text" id="Physio" /></td>
</tr>
<tr>
<td>Contact Number</td>
<td><input name="Number" type="text" id="Number" /></td>
</tr>
<tr>
<td>Address</td>
<td><input name="PhysiosAddress" type="text" id="PhysiosAddress" /></td>
</tr>
<tr>
<td>Postcode</td>
<td><input name="postcode" value="" type="text" id="postcode" />
<input type="submit" name="submit" value="Search" /></td>
</tr>
<tr>
<td>Physios Email</td>
<td><input name="PhysiosEmail" type="text" id="PhysiosEmail" /></td>
</tr>
<tr>
<td colspan="3" align="center"> </td>
</tr>
</table>
</form></td>
搜索结果
<?php
require_once('auth.php');
$host=""; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name
$tbl_name="Physio"; // Table name
// Connect to server and select database.
mysql_connect($host, $username, $password)or die("cannot connect");
mysql_select_db($db_name)or die("cannot select DB");
if(!isset($_POST['postcode'])) {
header ("location:index.php");
}
$search_sql="SELECT * FROM `Physio` WHERE Postcode like '%".$_POST['postcode']."%'";
$search_query=mysql_query($search_sql);
$search_rs= mysql_num_rows($search_query) ;
echo "<p> Results </p>" ;
if ($search_rs > 0)
{
echo "<p>".$search_rs['Postcode'] ."</p>" ;
} else {
echo "NO Results found";
}
?>
答案 0 :(得分:1)
您只收到大量结果(请参阅mysql_num_rows()手册页),而非结果本身。请改用mysql_fetch_array():
$search_sql = "SELECT * FROM `Physio` WHERE Postcode like '%" . $_POST['postcode'] . "%'";
$search_query = mysql_query($search_sql);
$search_rs = mysql_fetch_array($search_query);
在此之后,您可以通过以下方式检查是否返回了某些内容:
if (!empty($search_rs))
{
// Results found
}
else
{
// Nothing found...
}
顺便说一下,将$_POST
值直接传递给SQL代码是一个巨大的安全问题,至少可以用mysql_real_escape_string()来逃避它。
编辑:要收到多个结果:
$search_sql = "SELECT * FROM `Physio` WHERE Postcode like '%" . $_POST['postcode'] . "%'";
$search_query = mysql_query($search_sql);
while ($search_rs = mysql_fetch_array($search_query))
{
echo $search_rs['Postcode'] . '<br>';
}
答案 1 :(得分:0)
你应该使用mysql_fetch_assoc()(或mysql_fetch_array或mysql_result())获取值:
if ($search_rs > 0)
{
$search_row = mysql_fetch_assoc($search_query);
echo "<p>".$search_row['Postcode'] ."</p>" ;
} else {
echo "NO Results found";
}
此行还有语法错误:
echo $_POST['{postcode'] ;
应为echo $_POST['postcode'] ;
答案 2 :(得分:0)
尝试更改此内容: echo $ _POST [&#39; {postcode&#39;]; 对此: echo $ _POST [&#39; postcode&#39;];
我真的不知道{应该在你的代码中做什么:)