PHP简单搜索结果

时间:2014-08-19 05:41:00

标签: php ms-access search

我是一个php新手,我在尝试从访问数据库获取结果后遇到搜索结果时遇到问题。任何人都可以通过以下代码发现任何明显的问题吗?我希望它搜索书籍数据库(表中包含作者和标题)并返回作者/书名。任何帮助深表感谢!谢谢!

        <body>
<div id="wrapper">
<div id="content">
<h1>Search for a book</h1>
 <p>You  may search by author or title</p> 
        <form  method="post" action="assignment3.php"  > 
          <input  type="text" name="search"> 
          <input  type="submit" name="submit" value="Search"> 
        </form> 
        <br />

<?php 
        if(isset($_POST['submit'])){
        echo "<h2>Search Results</h2>";
        $author=filter_input (INPUT_POST, 'author');
        $title=filter_input (INPUT_POST, 'title');
        $conn = new COM("ADODB.Connection") or die ("Cannot start ADO");
        $connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data        Source="assignment3.mdb"; //connect to database  

        $conn->open($connString);

        $search_data("SELECT author, title FROM AuthorTitle WHERE author LIKE '%".$_POST['search']."%') OR title LIKE '%".$POST['search']."%'");

        if(($search_data)!=0){
        $rs=$searchquery;
        }

        ?>
<?php if(($searchquery)!=0) {
        do { 
        echo "<p>'.$author.' ',' '.$title.'</p>";

        } while ($rs=($searchquery));
}
        else {
                echo "No results.";
        }
}//end if

?>

</div> <!--end content-->
</div> <!--end wrapper-->

</body>
</html>

2 个答案:

答案 0 :(得分:1)

首先,您没有正确地括起括号。尝试更改

 $search_data("SELECT author, title FROM AuthorTitle WHERE author LIKE '%".$_POST['search']."%') OR title LIKE '%".$POST['search']."%'");

这个

$search_data("SELECT author, title FROM AuthorTitle WHERE author LIKE '%".$_POST['search']."%' OR title LIKE '%".$POST['search']."%'");

另一件事是你没有正确使用引号。改变

 $connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data        Source="assignment3.mdb";

这个

 $connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data        Source='assignment3.mdb'";

希望这有助于你

答案 1 :(得分:1)

伙计 - 这是我用来使其工作的代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Book Search</title>

<link href="assignment2.css" rel="stylesheet" type="text/css" />

</head>

<body>
<div id="wrapper">
<div id="content">
<h1>Search for a book</h1>
 <p>You  may search by author or title</p> 
        <form  method="post" action=""  > 
          <input  type="text" name="search"> 
          <input  type="submit" name="submit" value="Search"> 
        </form> 
        <br />

<?php 


$conn = new COM("ADODB.Connection") or die("Cannot start ADO");

$connString= "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=e:\\assignment3.mdb";

//creates the connection object and define the connection string



$conn->Open($connString);
$searchquery = filter_input(INPUT_POST, 'search');
$selectCommand="SELECT * FROM AuthorTitle WHERE  title LIKE '%$searchquery%' OR author LIKE '%$searchquery%'";

if(isset($_POST['search'])){
$rs = $conn->Execute($selectCommand);

//opens a recordset from the connection object


if (!$rs->EOF){

$selectCommand=$rs->Fields("ProductID");
$author=$rs->Fields("author");
$title=$rs->Fields("title");

echo "<h1>Search Result for '<b>$searchquery</b>':</h1>
<p><b>$title</b>, $author is available for checkout.</p><br />";

}

else

print "No results found.<br /><br />";



$rs->Close;
}
?>



</div> <!--end content-->
</div> <!--end wrapper-->

</body>
</html>