Jump to content

How to prevent submitting search form when search field is empty?


Simongf

Recommended Posts

When I search item from database works fine,but How to prevent submitting search form when search field is empty? And get Fatal error : call to member function fetch_assoc() on string. Even if there is no item match in the database my function doesn’t return error message which I expect “Your search can not be found”.

<?php

include’lib/Session.php’>

Session::int();

Include’lib/Database.php’;

Include’helpers/Format.php’;

Spl_autoload_register(function ($class){

Include_once”Classes/“.$class.”.php”;});

$db = new Database();

$sr = new Search();

?>

<div class=”search”>

<form name=“form1” method=“post” action=“search.php”>

<input name=“search” type=“text” value=“Search for products”>

<input type=“submit” value=“Search”>

</form>

</div>

header.php

————————————————

<?php include’inc/header.php’;?>

<?php

If($_SERVER[‘REQUEST_METHOD’]== ‘POST’])

{ $search = $_POST[‘search’];

$getsr = $sr->search($search)

}

?>

<div class = “ section “>

<?php

If (isset ($getsr))

{

while($result = $getsr->fetch_assoc())

{

?>

<div class =“ grid “>

<a href=“details.php?proid=<?php echo $result[‘productId’];?><img src=“admin/<?php echo $result[‘image’];?>” alt=“”/></a>

<h2><?php echo $result[‘productName’];?></h2>

<p><?php echo $fm->textShorten($result[‘body’],60);?></p>

<p><?php echo $result[‘price’];?></p>

<div class=“button”><a href=“details.php?proid=<?php echo $tesult[‘productId’];?>”Details</a></div>

</div>

<?php } } ? >

</div>

search.php

———————————————————<?php

$filepath = realpath(dirname(FILE));

Include_once($filepath.’/…/lib/Database.php’);

Include_once($filepath.’/…/helpers/Format.php’);

?>

<?php

Class Search{

Private $db;

Private $fm;

Public function __construct()

{ $this->db = new Database();

$this->fm = new Format();

}

Public function search ($search)

{ $search = $this->fm->validation($search);

$search = mysql_real_escape_string($this->dB->link,$search);

If(empty($search))

{ $searchmsg = “Search field must not be empty”;

return $searchmsg;

}

else {

$query = “ SELECT * FROM tbl_product WHERE productName LIKE ‘% “.$_POST[‘search’]. “ %’ “;

$result = $this->db->select($query);

If ($result!= False){

return $result;

}

else{

$searchmsg = “Search result not found ”;

return $searchmsg;

}

}

}

}

?>

Search.php

 

 

Link to comment
Share on other sites

Difficult to follow the logic flow given the absence of indents, but you seem to be assuming the query will return false if no results are found.

Queries return false when they fail. Not finding results is not a failure (it may in fact be the desired result). Check the number of records returned.

Link to comment
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.