我是PHP的新手并且不确定如何使用preg_match(),你能帮我修改我的代码,当用户输入*显示它会按字母升序显示整个表吗?我感谢任何帮助。
<html>
<head>
<meta charset="utf-8">
</head>
<body id="main_body" onload="FP_preloadImgs(/*url*/'images/button16.jpg', /*url*/'images/button19.jpg')" >
<img id="top" src="images/top.png" alt="">
<div id="form_container">
<h1><a href="">Lab</a></h1>
<div align="right">
<table border="0" cellpadding="0" cellspacing="0" width="25%">
<tr>
<td width="10" align="center"></td>
<td width="10" align="center"><a href="search.php">
<img border="0" id="img1" src="images/button14.jpg" height="20" width="100" alt="Search" onmousedown="FP_swapImg(1,0,/*id*/'img1',/*url*/'images/button16.jpg')" onmouseup="FP_swapImg(0,0,/*id*/'img1',/*url*/'images/button14.jpg')" fp-style="fp-btn: Linked Row 8; fp-img-hover: 0" fp-title="Search"></a></td>
</tr>
</table>
<div id="form_container" style="align-content: center ;height: min-height:50%px">
<ul >
<?php
$ID = $_POST["ID"];
$FIRST_NAME = $_POST["FIRST_NAME"];
$LAST_NAME = $_POST["LAST_NAME"];
$URL = $_POST["URL"];
$DESCRIPTION = $_POST["DESCRIPTION"];
$search = $_POST ['search'];
// Connect to MySQL
if(!($sqlCon= mysql_connect("***********","*********","******")))
die( "<p>Could not connect to database</p>" );
// open database
if (! ($dbCon = mysql_select_db("database29", $sqlCon)))
die( "<p>Could not open database</p>" );
if(preg_match("/[A-Z | a-z ]+/", $_POST ['search']))
{
$searchQuery = "SELECT * From USER_PROFILE WHERE FIRST_NAME LIKE '%".$search."%' OR LAST_NAME LIKE '%".$search."%' OR URL LIKE '%".$search."%' OR DESCRIPTION LIKE '%".$search."%'";
// query database
$searchResult= mysql_query ($searchQuery);
if (($searchResult == false))
{
user_error("Query failed: " . mysql_error() . "<br />\n$searchQuery");
}
elseif (mysql_num_rows($searchResult) == 0)
{
echo "<p>Sorry , your request could not be found.</p>\n";
}
else{
//Displaying fetched records to HTML table
echo "<table border='1' id='tableResult'>";
echo "<tr> <th>ID</th> <th>FIRST_NAME</th>
<th>LAST_NAME</th> <th>URL</th><th>DESCRIPTION</th></tr>";
// Using mysql_fetch_array() to get the next row until end of table rows
while($row = mysql_fetch_array( $searchResult )) {
// Print out the contents of each row into a table
echo "<tr><td id='#tdResult'>";
echo "<a href='updateQuery.php?id={$row['ID']}'>Update</a>";
echo "</td><td id='#tdResult'>";
echo $row['FIRST_NAME'];
echo "</td><td id='#tdResult'>";
echo $row['LAST_NAME'];
echo "</td><td id='#tdResult'>";
echo $row['URL'];
echo "</td><td id='#tdResult'>";
echo $row['DESCRIPTION'];
echo "</td></tr>";
}
}
mysql_close($sqlCon);
}
?>
</ul>
</div>
<p>
<?php print(mysql_num_rows($searchResult))?> results was found.
</p>
</div>
</div>
</body></html>
答案 0 :(得分:2)
测试搜索字符串是否为*
,然后从SQL中省略WHERE
子句。
if(preg_match('/^[A-Z |]+$|^\*$/i', $search))
{
if ($search == '*') {
$searchQuery = "SELECT * From USER_PROFILE";
} else {
$searchQuery = "SELECT * From USER_PROFILE WHERE FIRST_NAME LIKE '%".$search."%' OR LAST_NAME LIKE '%".$search."%' OR URL LIKE '%".$search."%' OR DESCRIPTION LIKE '%".$search."%'";
}
...
}
顺便说一下,你真的想在搜索字符串中允许|
吗?
答案 1 :(得分:0)
SELECT *
From USER_PROFILE
WHERE
$search='*' OR
(FIRST_NAME LIKE '%".$search."%' OR LAST_NAME LIKE '%".$search."%' OR URL LIKE '%".$search."%' OR DESCRIPTION LIKE '%".$search."%')
ORDER BY FIRST_NAME, LAST_NAME, URL, DESCRIPTION