Jump to content

little help


kjavia795

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

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.