Simongf Posted November 2, 2018 Share Posted November 2, 2018 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 More sharing options...
Barand Posted November 2, 2018 Share Posted November 2, 2018 Checking $_POST['search'] before calling the search function is one method that springs to mind. Link to comment Share on other sites More sharing options...
maxxd Posted November 2, 2018 Share Posted November 2, 2018 Or JavaScript; check the value on submit - call an alert() can return false if it's empty, otherwise proceed as normal. Link to comment Share on other sites More sharing options...
Barand Posted November 2, 2018 Share Posted November 2, 2018 I would dispute your use of "or" in that last post. Client side checks, javascript or setting the "required" attribute on the field, should be seen as additions for the convenience of the user. Validation should still be done on the server. Link to comment Share on other sites More sharing options...
maxxd Posted November 2, 2018 Share Posted November 2, 2018 Absolutely needs to be validated on the server side as well. My wording could have been better - thanks! Link to comment Share on other sites More sharing options...
Simongf Posted November 2, 2018 Author Share Posted November 2, 2018 Thanks for your reply , I’m trying to do what you suggested .my another question is if someone trys to fine some item which is not in the database ,I wrote a function that would respond “No item found” ,but it’s not happening . Link to comment Share on other sites More sharing options...
Barand Posted November 2, 2018 Share Posted November 2, 2018 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.