2
votes

I'm a php newbie, and I am having issues getting a search result to return after when trying to get results from an access database. Can anyone spot any glaring issues with the following code? I want it to search the database of books (with author and title in the table) and return the author/book name. Any help is much appreciated! Thanks!

        <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
$search_data("SELECT author, title FROM AuthorTitle WHERE author LIKE '%".$_POST['search']."%') OR title LIKE '%".$POST['search']."%'"); What exactly are you trying to do here? This seems totally wrong to me. And this here $connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="assignment3.mdb"; //connect to database Is wrong too since you forgot to delimiter the string properly. If you check the syntax highlighting here, you will see the problem.Realitätsverlust
Thanks - finally got it to work!Joe
Please select an answer in order to close this threadUtkarsh Dixit

2 Answers

1
votes

First thing is that You are not enclosing the brackets properly. Try Changing

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

this to

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

And the other thing is that you are not using quotes properly. Change

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

this to

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

Hope this helps you

1
votes

Folks - this is the code I used to get it to work:

<!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>