Jump to content

help! trying to create PHP search form and it's not working


kelseyirene

Recommended Posts

I've been struggling with this all afternoon. The code seems correct, no problems are turning up, I'm connecting to the database, but the search isn't yielding results that I know are there. My table is fairly simple, it's basically a list of the books I own... my columns are 'num' 'title' 'author_first' 'author_last' 'isbn' and 'read'. Ideally, I'd like to be able to search for any keyword in any of the columns, but for now I just want to figure out how to do the search form.

 

Here's the code I used for the form:

 

<form method="post" action="search.php">

<div align="center">

<table border="0" cellpadding="0" cellspacing="0">

<tr>

<td bordercolor="#000000">

<p align="center">

<select name="num" size="1">

<option value="title">Title</option>

<option value="telephone">Author Last</option>

<option value="birthday">ISBN</option>

</select> <input type="text" name="search" size="25">  <br>

Search database: <input type="submit"></p>

</td>

</tr>

</table>

</div>

</form>

 

--

 

and this is the form I used for search.php:

 

<center>

<table border="1" cellpadding="5" cellspacing="0" bordercolor="#000000">

<tr>

<td width="60"><b>num</b></td>

<td width="100"><b>title</b></td>

<td width="70"><b>author last</b></td>

<td width="150"><b>isbn</b></td>

</tr>

<tr>

<td>

<?

$con = mysql_connect("localhost","username","password") or die(mysql_error());

 

mysql_select_db("my_db",$con);

 

?>

 

<?

//error message (not found message)begins

$XX = "No Record Found, to search again please close this window";

//query details table begins

$query = mysql_query("SELECT * FROM TitleInfo WHERE $num LIKE '%$search%' LIMIT 0, 50");

while ($row = @mysql_fetch_array($query))

{

$variable1=$row["num"];

$variable2=$row["title"];

$variable3=$row["author_last"];

$variable4=$row["isbn"];

//table layout for results

 

print ("<tr>");

print ("<td>$variable1</td>");

print ("<td>$variable2</td>");

print ("<td>$variable3</td>");

print ("<td>$variable4</td>");

print ("</tr>");

}

//below this is the function for no record!!

if (!$variable1)

{

print ("$XX");

}

//end

?>

</table>

</center>

 

 

</html>

 

--

 

help!! what am I doing wrong?

As harkly says you don't manually assign a value to $search. Since your form has an input that has a name attribute of 'search' it would seem you are relying on register_globals being enabled, which it may not be.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.