我有一个HTML页面,人们可以输入信息来搜索数据库。选项包括:作者,标题,类别,年份和ISBN。
一个人可以输入作者姓名来搜索他们所写的书籍。但是,我希望他们能够将作者姓名+类别放入检索记录中。代码下划线无法正常运行。
任何帮助将不胜感激。
mySocket_ClientSide.close();
答案 0 :(得分:1)
这似乎是一个更合适的解决方案。
<?php
$fields=array('Author','Title','Category','Year','ISBN');
$query="SELECT * FROM Test WHERE ";
$first=true;
//unset fields that were not provided. Could be done inside of the next foreach
//but for simplicity reasons we will just do it here
foreach($fields as $key=>$val)
{
if(!isset($_POST[$val]))
unset($fields[$key]);
}
//now we only have fields that have data
foreach($fields as $field)
{
if($first)
{
$query.="`$field`='".$con->real_escape_string($_POST[$field])."' ";
$first=false;
}
else
{
$query.=" AND `$field`='".$con->real_escape_string($_POST[$field])."'";
}
}
$result = mysqli_query($con, $sql);
echo "<table border=1>";
echo "<tr><th>Author</th><th>Title</th><th>Category</th><th>Year</th><th>ISBN</th></tr>";
while ($row = mysqli_fetch_assoc($result))
{
echo "<tr><td>" . $row['Author'] . "</td><td>" . $row['Title'] . "</td><td>" . $row['Category'] . "</td><td>" . $row['Year'] . "</td><td>" . $row['ISBN'] . "</td></tr>";
}
echo "</table><br><br>";