PHP を使用して MySQL データベースを検索するフォームがあります。現在、ユーザーが 2 つのフィールドのいずれかに検索を入力すると、データベースの内容全体が表示されます。また、ユーザーが両方のフィールドを空白のままにすると、データベースの内容全体が表示されます。
ただし、ユーザーが両方のフィールドにランダムな情報を入力すると、結果ページは空白になります。
このフォームの想定される使用法は、ユーザーが記事のタイトル、記事の著者または組織、または記事のタイトルとその著者または組織に基づいて、フィールドの 1 つまたは両方に入力して記事を検索できることです。
私が理解しようとしているのは次のとおりです。
結果ページにすべてのデータベース コンテンツが表示され続ける理由。
と
コーディング エラーによって単にダンプされるのではなく、データベースが実際にクエリされていることを確認する方法。
コードは以下のとおりです。
search.php:
<div class="content">
<form id="form1" name="form1" method="post" action="searchdb.php">
<table width="100%" border="0" cellpadding="6">
<tr>
<td width="29%" align="right">Article Title:</td>
<td width="71%" align="left"><input name="articletitle" type="text" id="articletitle" size="50" /></td>
</tr>
<tr>
<td align="right">Author or Organization:</td>
<td align="left"><input name="articleorganization" type="text" id="articleorganization" size="50" /></td>
</tr>
</table>
<table width="100%" border="0" cellpadding="6">
<tr>
<td><input type="submit" name="submit" value="Submit" /></td>
</tr>
</table>
</form>
</div>
searchdb.php
<?php
include('settings.php');
$query = "select * from articles";
$where = array();
if (!empty($_POST['articletitle'])) {
$where[] = "articletitle LIKE '%".mysql_real_escape_string($_POST['articletitle'])."%'";
}
if (!empty($_POST['articleorganization'])) {
$where[] = "articleorganization LIKE '%".mysql_real_escape_string($_POST['articleorganization'])."%'";
}
if (!empty($where)) {
$query .= " WHERE " . implode(" OR ", $where);
$sql = mysql_query($query);
} else {
// No results
}
while ($row = mysql_fetch_array($sql)){
echo '<br/> Article Title: '.$row['articletitle'];
echo '<br/> Article Organization: '.$row['articleorganization'];
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 '<td><a href="entry.php?id=' . $row['id'] . '">View Full Entry</a></td>';
echo '<br/><br/>';
}
?>