好的,这很难解释,但我现在有一个网站,我使用PHP和MySQL将表格拉入网页并显示它们。我已经能够创建搜索功能来查找表中的特定值。我会告诉你我的代码。
<!DOCTYPE php>
<html>
<head>
<link rel="stylesheet" href="style.php" media="screen">
</head>
<body>
<a id="TableButton" href="/Test.php">Items Table</a>
<a id="Clear" href="index.php">Clear Search</a>
<center>
<form method="GET" id="SearchPerson">
<select name="Drop" id="Select">
<option value="FirstName">First Name</option>
<option value="Surname">Surname</option>
<option value="MobileNumber">Mobile Number</option>
<option value="Code">Code</option>
<option value="TeamGroup">Team Group</option>
<option value="Home">Home</option>
</select>
<input type="text" name="Box">
<input type="submit" value="Search">
</form>
</center>
<?php
$host = "localhost";
$user = "root";
$pass = "password";
$db = "Database";
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
mysql_select_db($db) or die ("Unable to select database!");
if($_REQUEST['Drop']=='MobileNumber') {
$MobileNumber = $_REQUEST['Box'];
$query = "SELECT * From Person WHERE MobileNumber = $MobileNumber";
}
elseif($_REQUEST['Drop']=='Code') {
$Code = $_REQUEST['Box'];
$query = "SELECT * From Person WHERE Code = '$Code'";
}
elseif($_REQUEST['Drop']=='TeamGroup') {
$TeamGroup = $_REQUEST['Box'];
$query = "SELECT * From Person WHERE TeamGroup = '$TeamGroup'";
}
elseif($_GET['Drop']=='FirstName') {
$FirstName = $_REQUEST['Box'];
$query = "SELECT * From Person WHERE FirstName = '$FirstName'";
}
elseif($_GET['Drop']=='Surname') {
$Surname = $_REQUEST['Box'];
$query = "SELECT * From Person WHERE Surname = '$Surname'";
}
elseif($_REQUEST['Drop']=='Home') {
$Home = $_REQUEST['Box'];
$query = "SELECT * From Person WHERE Home = '$Home'";
}
else{
$query = "SELECT * FROM Person";
}
print "<center id=Title>Person Table</center>";
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
if (mysql_num_rows($result) > 0) {
$row = mysql_fetch_assoc($result);
print '<center><table><tr>';
foreach($row as $name => $value) {
print "<th>$name</th>";
}
print '</tr>';
while($row) {
print '<tr>';
foreach($row as $key=>$value) {
if($key=='MobileNumber'){print "<td><a href='/Test.php?MobileNumber=$value'>$value</a></td>";}
elseif($key=='TeamGroup'){print "<td><a href='/Test.php?TeamGroup=$value'>$value</a></td>";}
elseif($key=='Group'){print "<td><a href='/Test.php?Home=$value'>$value</a></td>";}
else{print "<td>$value</td>";}
}
print '</tr>';
$row = mysql_fetch_assoc($result);
}
print '</table></center>';
}
else {
echo "No People found!";
}
mysql_free_result($result);
mysql_close($connection);
?>
</body>
</html>
问题是即使我已经让搜索工作,但只有当我拥有完整的值时它才有效,而我想要它,所以说我把某个移动号码的一部分放在那里它会显示那些包含这些部分的手机号码它的价值。例如,有些人有一个从0783开始的手机号码,我在搜索框中输入了这个号码,我想让它以手机号码显示所有0783的人。
答案 0 :(得分:0)
您正在寻找的是"SELECT * FROM Person WHERE MobileNumber LIKE '%".$_POST['MobileNumber']."%'"
,它将查找包含您想要的字符串的任何字符串,但两侧也可能更长。例如,"foo"
将返回值为"foobar"
,"barfoo"
,"barfoobar"
且当然为"foo"
的行,但不会返回"fo"
。
此处%
是一个通配符,这意味着如果您特别想要使用字符串返回值开始的行,您也可以执行"SELECT * FROM Person WHERE MobileNumber LIKE '".$_POST['MobileNumber']."%'"
之类的操作("foobar"
{1}},但不是上面示例中的"barfoo"
。
另请注意"
和'
。
此外:
mysql_real_escape_string
,因为它非常面向mysql并具体。