我正在尝试在我的数据库中找到与我的查询匹配的结果并将其放在我制作的表格中,但是我得不到匹配的结果。我在phpmyadmin中运行了我的查询并获得了所需的输出。我在$ result变量上使用了var_dump并得到以下内容:resource(4)类型(mysql结果)。这是我的代码:
<?php
function renderSearchForm($search, $search_by, $search_by, $error){
?>
<!DOCTYPE HTML5>
<head>
<title>Search Query</title>
</head>
<body>
<?php
//display errors
if($error != ''){
echo '<div style="padding:4px; border:1px solid red; color:red;">' . $error . '</div>';
}
?>
<form action="" method="post">
<div>
<p>Search For:</p>
<strong>Name: *</strong> <input type="text" name="search" value="<?php echo $search; ?>" /><br/>
<p>Search by:</p>
<input type="radio" name="search_by"
<?php if (isset($search_by) && $search_by=="firstname") echo "checked";?>
value="firstname"/>Firstname*
<input type="radio" name="search_by"
<?php if (isset($search_by) && $search_by=="surname") echo "checked";?>
value="surname"/>Surname*
<br><br>
<input type="submit" name="submit" value="Submit">
<p>* required</p>
</div>
</form>
</body>
</html>
<?php
}
//connect to db
include('connect_db.php');
//check if submitted
if (isset($_POST['submit'])) {
$search = mysql_real_escape_string(htmlspecialchars($_POST['search']));
$search_by = mysql_real_escape_string(htmlspecialchars($_POST['search_by']));
//check if search is empty
if (empty($_POST["search"])) {
$error = "Error: Name is required";
//error, display form
renderSearchForm($search, $search_by, $search_by, $error);
}
elseif
// check if name only contains letters and whitespace
(!preg_match("/^[a-zA-Z ]*$/",$search)) {
$error = "Error: Only letters and white space allowed";
//error, display form
renderSearchForm($search, $search_by, $search_by, $error);
}
//check if radio button selected
elseif (empty($_POST["search_by"])) {
$error = "Error: Search_by is required";
//error, display form
renderSearchForm($search, $search_by, $search_by, $error);
}else{
//save data
$query = "SELECT * FROM members WHERE '$search_by' LIKE '%$search%'";
$result = mysql_query($query)
or die(mysql_error());
var_dump($result);
//display data from db
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>ID</th> <th>Firstname</th> <th>Surname</th> <th>Telephone</th> <th>Cell</th> <th>Address</th> <th></th> <th></th> </tr>";
//loop through results of db and display in table
while ($row = mysql_fetch_array($result)) {
//echo contents in table
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['surname'] . "</td>";
echo "<td>" . $row['telephone'] . "</td>";
echo "<td>" . $row['cellphone'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td><a href='edit.php?id=" . $row['id'] . "'>Edit</a></td>";
echo "<td><a href='delete.php?id=" . $row['id'] . "'>Delete</a></td>";
echo "</tr>";
}
echo "</table>";
//This counts the number or results
$anymatches=mysql_num_rows($result);
if ($anymatches == 0) {
echo "Sorry, but we can not find an entry to match your query<br><br>";
}
//And we remind them what they searched for
echo "<b>Searched For:</b> " .$search. "<b> In: </b>" .$search_by;
}
}else{
//if form not submitted, display form
renderSearchForm('','','','');
}
?>
答案 0 :(得分:0)
您需要删除$ search_by之间的单引号。试试这个:
"SELECT * FROM members WHERE $search_by LIKE '%$search%'"