Jump to content

Recommended Posts

$query = "SELECT * FROM uploads WHERE title = '".$_POST['textfield']."' ORDER BY title ASC";
    	$result = mysql_query($query);
    	while($row = mysql_fetch_array($result))
    	{
if($_POST['textfield'] == $row['title'])
{
		echo "<a href='index.php?p=view&sub=".$row['id']."'>".$row['title']."</a><br>";
}



else if ($_POST['textfield'] != $row['title'])
{

echo "Search field was not found. Please go back and enter a search item.";
}

}}

 

Basically, its searching if w/e was entered into a textfield is in the database... and if it is it will be displayed. But when it isn't, it isn't showing the Search field was not found. Please go back and enter a search item. It only shows a blank page. Any help?

Link to comment
https://forums.phpfreaks.com/topic/111279-little-help/
Share on other sites

first off, you do not need

else if ($_POST['textfield'] != $row['title'])
{

because you do not need to specify the exact opposite of your if statement.  All you need is a simple

 

if (...) {

} else {

}

 

In fact, you don't even need the if statement, since you're asking your database to return results based on the posted value in the first place. 

 

As far as your problem is concerned, that message will never echo because the while loop will only execute if it's true in the first place.  If you want some kind of "nothing was found" message, you can do something like this:

 

$query = "SELECT * FROM uploads WHERE title = '".$_POST['textfield']."' ORDER BY title ASC";
$result = mysql_query($query);
$rows = mysql_num_rows($result);

if ($rows > 0) {
   while($row = mysql_fetch_array($result)) {
      echo "<a href='index.php?p=view&sub=".$row['id']."'>".$row['title']."</a><br>";
   } // end while
} else {
   echo "Search field was not found. Please go back and enter a search item.";
} // end else. 

 

Link to comment
https://forums.phpfreaks.com/topic/111279-little-help/#findComment-571203
Share on other sites

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.