Jump to content

Simple database search not working, help please!


Recommended Posts

Here's my search field:

 

<form name="search" method="post" action="<?=$PHP_SELF?>">
<Select NAME="field" id="field">
<Option VALUE="title">Title</option>
<Option VALUE="synopsis">Synopsis</option>
<Option VALUE="author">Author</option>
<Option VALUE="category">Category</option>
</Select>
<input type="text" id="find" name="find" />
<input type="hidden" id="searching" name="searching" value="yes" />
<input type="submit" id="search" name="search" value="Go" />
</form>

 

and here's my PHP/SQL to get the data:

<?

if ($searching =="yes") 
{
if ($find == "")
{
echo "<table width='90%'><tr><td align='left'><font color='white' face='arial' size='5'>No term entered, please try again.</font></td></tr></table>";

exit;
}

// Otherwise we connect to our Database
mysql_connect("edit","edit","edit") or die(mysql_error());
mysql_select_db("edit") or die(mysql_error());

// We preform a bit of filtering
$find = strtoupper($find);
$find = strip_tags($find);
$find = trim ($find);

//Now we search for our search term, in the field the user specified
$data = mysql_query("SELECT * FROM tutsearch WHERE upper($field) LIKE'%$find%'");

echo "<table width='90%'>";
echo "<tr>";
echo "<td width='20%' bgcolor='#111111'>";
echo "<font color='white' face='arial' size='3'><b>Title</b></font>";
echo "</td>";
echo "<td width='55%' bgcolor='#111111'>";
echo "<font color='white' face='arial' size='3'><b>Synopsis</b></font>";
echo "</td>";
echo "<td width='10%' bgcolor='#111111'>";
echo "<font color='white' face='arial' size='3'><b>Category</b></font>";
echo "</td>";
echo "<td width='15%' bgcolor='#111111'>";
echo "<font color='white' face='arial' size='3'><b>Author</b></font>";
echo "</td>";
echo "</tr>";
// Display results
while($result = mysql_fetch_array( $data ))
{
echo "<tr>";
echo "<td width='20%' bgcolor='#111111'>";
echo "<font color='white' face='arial' size='3'>" .$result['Title']. "</font>";
echo "</td>";
echo "<td width='55%' bgcolor='#111111'>";
echo "<font color='white' face='arial' size='3'>" .$result['Synopsis']. "</font>";
echo "</td>";
echo "<td width='10%' bgcolor='#111111'>";
echo "<font color='white' face='arial' size='3'>" .$result['Category']. "</font>";
echo "</td>";
echo "<td width='15%' bgcolor='#111111'>";
echo "<font color='white' face='arial' size='3'>" .$result['Author']. "</font>";
echo "</td>";
echo "</tr>";
}
//This counts the number or results - and if there wasn't any it gives them a little message explaining that
$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{
echo "<tr>";
echo "<td width='15%' bgcolor='#111111'>";
echo "<font color='white' face='arial' size='3'>No matches were found for your search.</font>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
}
?> 

It I can't find out what's wrong! Please help! :(

I just glimpsed over your code and saw:

 

</pre>
<form name="search" method="post" action="<?=%24PHP_SELF?>"><

 

which again, is a global variable issue, and needs to be: (and don't use short tags to start PHP "" use "<?php".

 

</pre>
<form name="search" method="post" action="<?php%20echo%20%24_SERVER%5B'PHP_SELF'%5D;%20?>"><

I just glimpsed over your code and saw:

 

<form name="search" method="post" action="<?=$PHP_SELF?>">

 

which again, is a global variable issue, and needs to be: (and don't use short tags to start PHP "<?" use "<?php".

<form name="search" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

 

re: $PHP_SELF

Happy side effect to continued use of $PHP_SELF is that it results in echoing out action='' which under these circumstances, produces the same effect as using $_SERVER['PHP_SELF'].  Now, if he were wanting to reference the current script in another script...that would bea different story.

 

re: using short tags.

 

<?= $variable ?>

 

This is actually shorthand for doing <?php echo $variable; ?>.  It is not the same as doing <? echo $variable; ?> (using short tags).  Although to be fair, <?= .. ?> is a setting in php.ini that can be enabled/disabled, thus putting it in the same boat as short tags, as far as possible server incompatibility.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.