Blank / Empty Search Form field - php

I have set up a search form for my database. When I search and results are found a message is echoed below the search form. For instance, 10 records found, 0 records found.
How can I get that message to disappear if the search form field is blank/empty. Currently it displays 15 records found for a blank/empty search field. Which is all the database records.
Thanks for any help.
Form:
<form action="" method="post">
<input type="text" name="search_box" value="<?php if (isset($_POST['search_box'])) echo $_POST['search_box']; ?>" placeholder="Search here ..."/>
<input value="Search" name="search" type="submit" /><br>
</form>
PHP:
<?php
$count = mysqli_num_rows($result);
if($count > 0){
echo $count . " Records Found";
}if($count == 0){
echo "0 Records Found";
}if($count == ""){
echo "";
}
?>
Query:
//Retrieve the practice posts from the database table
$query = "SELECT * FROM practice";
//check if search... button clicked, if so query fields
if(isset($_POST['search'])){
$search_term = trim($_POST['search_box']);
$query .= " WHERE title = '{$search_term}'";
$query .= " or subject LIKE '%{$search_term}%'";}

<?php
//Retrieve the practice posts from the database table
$query = "SELECT * FROM practice";
//check if search... button clicked, if so query fields
if(isset($_POST['search'])){
$search_term = trim($_POST['search_box']);
$query .= " WHERE title = '{$search_term}'";
$query .= " or subject LIKE '%{$search_term}%'";
//execute your query
$result = $dbconnect->query($query);
$count = mysqli_num_rows($result);
if($count > 0){
echo $count . " Records Found";
}
if($count == 0){
echo "0 Records Found";
}
}
else {
// it is mean your search box value($_POST['search']) is empty, so it will echo null value
echo $_POST['search'];
}
?>
please try, hope will save your day :D

Please replace PHP code with these one :-
<?php
if(isset($_REQUEST['search_box'])){
$count = mysqli_num_rows($result);
} else {
$count = '';
}
if($count > 0){
echo $count . " Records Found";
}if($count == 0){
echo "0 Records Found";
}if($count == ""){
echo "";
}
?>

Just put your code in the post method
if ((isset($_POST['search_box'])) && ($_POST['search_box']!=""))
{
$count = mysqli_num_rows($result);
if($count > 0){
echo $count . " Records Found";
}if($count == 0){
echo "0 Records Found";
}if($count == ""){
echo "";
}
}

Related

Why is isset($_POST['like']) not working when I click on submit button?

My POST method is not working in this code but if I chnage it to GET method then it works fine.
This is my code. If you change it to GET then works just fine. Even with POST, it displays likes from the database but it's not adding more like(s) when I click on the like button.
<form action="" method="POST">
<input type="hidden" name="blogID" value=<?php echo $blogID;?> />
<input id="likeButton" <?php echo isset($_POST["like"]) ? "disabled" : "";?> type = "submit" value = "Click to Like this Article" name="like"/> <br><br>
</form>
<br>
<?php
$pageWasRefreshed = isset($_SERVER['HTTP_CACHE_CONTROL']) && $_SERVER['HTTP_CACHE_CONTROL'] === 'max-age=0';
if($pageWasRefreshed )
{
$likesQuery = "SELECT likes FROM blogstatus where blogID=$blogID";
$likes = $conn->query($likesQuery);
if ($likes->num_rows > 0) {
// output data of each row
while($row2 = $likes->fetch_assoc()) {
echo $row2["likes"] . " Likes <br> <br>";
}
} else {
echo "0 Likes";
}
}
else{
if(isset($_POST['like'])) {
$blogID=$_POST['blogID'];
$likesQuery2 = "UPDATE blogstatus set likes = likes+1 where blogID=$blogID";
$conn->query($likesQuery2);
$likesQuery3 = "SELECT likes FROM blogstatus where blogID=$blogID";
$likes3 = $conn->query($likesQuery3);
if ($likes3->num_rows > 0) {
// output data of each row
while($row = $likes3->fetch_assoc()) {
echo $row["likes"] . " Likes <br> <br>";
}
} else {
echo "0 Likes";
}
}
else{
$likesQuery = "SELECT likes FROM blogstatus where blogID=$blogID";
$likes = $conn->query($likesQuery);
if ($likes->num_rows > 0) {
// outputz data of each row
while($row2 = $likes->fetch_assoc()) {
echo $row2["likes"] . " Likes <br> <br>";
}
} else {
echo "0 Likes";
}
}
}
;?>
In else part, you are trying to check isset($_POST['like']) field in the form which not exists.
Update
else{
if(isset($_POST['like'])) {
$blogID=$_POST['blogID'];
To
else{
if(isset($_POST['blogID'])) {
$blogID=$_POST['blogID'];
Okay so the problem is not with the isset itself, it is that $pageWasRefreshed is always true and thus not reaching to your $_POST condition
Try putting your $_POST condition first, I think it will work fine
$pageWasRefreshed = isset($_SERVER['HTTP_CACHE_CONTROL']) && $_SERVER['HTTP_CACHE_CONTROL'] === 'max-age=0';
if(isset($_POST['like'])) {
$blogID=$_POST['blogID'];
$likesQuery2 = "UPDATE blogstatus set likes = likes+1 where blogID=$blogID";
$conn->query($likesQuery2);
$likesQuery3 = "SELECT likes FROM blogstatus where blogID=$blogID";
$likes3 = $conn->query($likesQuery3);
if ($likes3->num_rows > 0) {
// output data of each row
while($row = $likes3->fetch_assoc()) {
echo $row["likes"] . " Likes <br> <br>";
}
} else {
echo "0 Likes";
}
} else if($pageWasRefreshed ){
$likesQuery = "SELECT likes FROM blogstatus where blogID=$blogID";
$likes = $conn->query($likesQuery);
if ($likes->num_rows > 0) {
// output data of each row
while($row2 = $likes->fetch_assoc()) {
echo $row2["likes"] . " Likes <br> <br>";
}
} else {
echo "0 Likes";
}
}
else {
$likesQuery = "SELECT likes FROM blogstatus where blogID=$blogID";
$likes = $conn->query($likesQuery);
if ($likes->num_rows > 0) {
// outputz data of each row
while($row2 = $likes->fetch_assoc()) {
echo $row2["likes"] . " Likes <br> <br>";
}
} else {
echo "0 Likes";
}
}

PHP display database using a search result

I am currently trying to create a search menu, and display it's result in a form of table. I managed to get the search result only as a string, but unable to use it to display a full row in the table.
Here is my code :
- Creating the Form :
<form action = "profile.php" method = "post">
<input type="text" name="search" placeholder = "Search">
<input type="submit" value = "SEARCH"><br>
</form>
This is the php to get search result
if(isset($_POST['search']))
{
$searchq = $_POST['search'];
$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
$query = mysql_query("SELECT * FROM identity WHERE fullname LIKE '%$searchq%'") or die("Could not Find!");
$count = mysql_num_rows($query);
if($count == 0)
{
$output = 'There is no result';
}
else
{
while($row = mysql_fetch_array($query))
{
$fullname = $row['fullname'];
$output .= '<div> '.$fullname.' </div>';
$records = mysql_query("SELECT * FROM identitas_pengadu WHERE fullname='$output'");
}
}
}
This is the code I used to display the result in a database :
while($identity = mysql_fetch_assoc($records))
{
echo "<tr>";
echo "<td>".$identity['fullname']."</td>";
echo "<td>".$identity['nip']."</td>";
echo "<td>".$identity['email']."</td>";
echo "<td>".$identity['position']."</td>";
echo "<td>".$identity['status']."</td>";
echo "<td>EDIT</td>";
echo "</tr>";
}
It works fine if the value of $records does not use the WHERE function. Please help.
Thank You

Fulltext Search engine, multiple columns, boolean mode

I am making a search engine for an android app that does fulltext search and match against multiple columns against '+word1 +word2' in boolean mode.
However, I can't get any search result.
E.g. search field type- "open sea"
then, Sql will search Match...Against ('+open +sea' IN BOOLEAN MODE)
and display list of selectable results, on which each result clicked, will provide details of the particular result on a new page.
Sorry, I am a newbie in android app development.
Here is my php code for search.php
<?php
# $db = new mysqli('localhost','username','password','db');
if (mysqli_connect_errno()) {
echo 'Error: Could not connect to database.
Please try again later.';
exit;
}
if(!empty($_POST)){
$term = $_POST['query'];
$words = explode(" ", trim($term));
$termArray = array();
foreach($words as $word){
if(!empty($word)){
$termArray[] = "+$word";
}
}
$searchquery = implode(" ", $termArray);
if (!$term) {
echo 'You have not entered any search details. Please go back and try again.';
exit;
}
//initial query
$query = "SELECT *
FROM servicetable
WHERE MATCH(title,cat,brand,company)
AGAINST ('".$searchquery."' IN BOOLEAN MODE)
ORDER BY title ASC";
$result = $db->$query;
$num_results = $result->num_rows;
//show user what user searched.
echo $searchquery;
echo "<p>Results found: ".$num_results."</p>";
//counts results.
if ($num_results == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
}
for ($i=0; $i <$num_results; $i++) {
$row = $result->fetch_assoc();
echo "<p><strong>".($i+1).". Outlet Name: ";
echo stripslashes($row['title']);
echo "</strong><br />Category: ";
echo stripslashes($row['cat']);
echo "<br />Opening Hours: ";
echo stripslashes($row['ophours']);
echo "<br />Brand: ";
echo stripslashes($row['brand']);
echo "</strong><br />Company: ";
echo stripslashes($row['company']);
echo "</p>";
}
$result->free();
$db->close();
} else {
?>
<h1>Search</h1>
<form name="form1" action="search.php" method="post">
Enter Search:<br />
<input type="text" name="query" id="query" placeholder="Search a service"/>
<br/>
<input type="submit" value="Search Now" name="completedsearch" />
</form>
<?php
}
?>
Hi I have found my error:
this line correction-->
$result = $db->query($query);

Search/Query in a large table with many records

I've created a html+php page wherein it searches in a mysql database. I am able to get output when my table contains a few records but when my table has over 100+ records it would no longer show records when i search and shows my else statement which is "0 records".
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
if(!empty($sfname) || !empty($sgen) || !empty($sdoc) || !empty($smisc) || !empty($ssick) || !empty($sothers) ){
$genQueryPart = !empty($sgen) ? "Gender LIKE '%$sgen%'" : "";
$fnameQueryPart = !empty($sfname) ? "FullName LIKE '%$sfname%'" : "";
$docQueryPart = !empty($sdoc) ? "Doctor LIKE '%$sdoc%'" : "";
$miscQueryPart = !empty($smisc) ? "Misc LIKE '%$smisc%'" : "";
$sickQueryPart = !empty($ssick) ? "Sickness LIKE '%$ssick%'" : "";
$othersQueryPart = !empty($sothers) ? "Others LIKE '%$sothers%'" : "";
$arr = array($genQueryPart, $fnameQueryPart,$docQueryPart,$miscQueryPart,$sickQueryPart,$othersQueryPart);
$sql = "select * from index where ";
$needsAnd = false;
for ($i = 0; $i < count($arr); $i++) {
if ($arr[$i] != "") {
if ($needsAnd) {
$sql .= " AND ";
}
$needsAnd = true;
$sql .= " " . $arr[$i];
}
}
//Get query on the database
$result = mysqli_query($conn, $sql);
mysqli_store_result();
//Check results
if (mysqli_num_rows($result) > 0)
{
//Headers
echo "<table border='1' style='width:100%'>";
echo "<tr>";
echo "<th>File ID</th>";
echo "<th>Full Name</th>";
echo "<th>Gender</th>";
echo "<th>Doctor</th>";
echo "<th>Misc</th>";
echo "<th>Sickness</th>";
echo "<th>Others</th>";
echo "</tr>";
//output data of each row
while($row = mysqli_fetch_assoc($result))
{
echo "<tr>";
echo "<td>".$row['FileID']."</td>";
echo "<td>".$row['FullName']."</td>";
echo "<td>".$row['Gender']."</td>";
echo "<td>".$row['Doctor']."</td>";
echo "<td>".$row['Misc']."</td>";
echo "<td>".$row['Sickness']."</td>";
echo "<td>".$row['Others']."</td>";
echo "</tr>";
}
echo "</table>";
}
else {
echo "0 results";
}
} else {
echo "You must enter at least one value";
}
mysqli_close($conn);
?>
Use if condition and paging to show more than 100 records after search. count the number of results after that use paging query to show records per page .This will be good idea for displaying large number of records.
Don't Merge Php and Html together . fetch the record in a function and use is to integrate in HTML

Update Multiple Rows (PHP + MySQL)

I am working on a lead management system - and as the database for it grows the need for more bulk functions appears - and unfortunately I am getting stuck with one of them. The database stores many different leads - with each lead being assigned to a specific closer; thus the database stores for each lead the lead id, name, closer name, and other info. The main lead list shows a checkbox next to each lead which submits the lead id into an array:
<input type=\"checkbox\" name=\"multipleassign[]\" value=\"$id\" />
Now this all goes to the following page:
<?php
include_once"config.php";
$id = $_POST['multipleassign'];
$id_sql = implode(",", $id);
$list = "'". implode("', '", $id) ."'";
$query = "SELECT * FROM promises WHERE id IN ($list) ";
$result = mysql_query($query);
$num = mysql_num_rows ($result);
if ($num > 0 ) {
$i=0;
while ($i < $num) {
$closer = mysql_result($result,$i,"business_name");
$businessname = mysql_result($result,$i,"closer");
echo "$closer - $businessname";
echo"<br>";
++$i; } } else { echo "The database is empty"; };
echo "<select name=\"closer\" id=\"closer\">";
$query2 = "SELECT * FROM members ";
$result2 = mysql_query($query2);
$num2 = mysql_num_rows ($result2);
if ($num2 > 0 ) {
$i2=0;
while ($i2 < $num2) {
$username = mysql_result($result2,$i2,"username");
$fullname = mysql_result($result2,$i2,"name");
echo "<option value=\"$fullname\">$fullname</option>";
++$i2; } } else { echo "The database is empty"; }
echo "</select>";
?>
I want to be able to use the form on this page to select a closer from the database - and then assign that closer to each of the leads that have been selected. Here is where I have no idea how to continue.
Actually - i got it. I don't know why I didn't think of it sooner. First off I passed the original $list variable over to the new page - and then:
<?php
include_once"config.php";
$ids = $_POST['list'];
$closer = $_POST['closer'];
$query = "UPDATE `promises` SET `closer` = '$closer' WHERE id IN ($ids) ";
mysql_query($query) or die ('Error updating closers' . mysql_error());
echo "A new closer ($closer) was assigned to the following accounts:";
$query = "SELECT * FROM promises WHERE id IN ($list) ";
$result = mysql_query($query);
$num = mysql_num_rows ($result);
if ($num > 0 ) {
$i=0;
while ($i < $num) {
$businessname = mysql_result($result,$i,"business_name");
echo "<li>$businessname";
++$i; } } else { echo "The database is empty"; };
?>
The updated page before this:
$query = "SELECT * FROM promises WHERE id IN ($list) ";
$result = mysql_query($query);
$num = mysql_num_rows ($result);
if ($num > 0 ) {
$i=0;
while ($i < $num) {
$closer = mysql_result($result,$i,"business_name");
$businessname = mysql_result($result,$i,"closer");
echo "$closer - $businessname";
echo"<br>";
++$i; } } else { echo "The database is empty"; };
echo "<form name=\"form1\" method=\"post\" action=\"multiple_assign2.php\">";
echo "<input type=\"hidden\" name=\"list\" value=\"$list\" />";
echo "<select name=\"closer\" id=\"closer\">";
$query2 = "SELECT * FROM members ";
$result2 = mysql_query($query2);
$num2 = mysql_num_rows ($result2);
if ($num2 > 0 ) {
$i2=0;
while ($i2 < $num2) {
$username = mysql_result($result2,$i2,"username");
$fullname = mysql_result($result2,$i2,"name");
echo "<option value=\"$fullname\">$fullname</option>";
++$i2; } } else { echo "The database is empty"; }
echo "</select>";
echo "<input name=\"submit\" type=\"submit\" id=\"submit\" value=\"Reassign Selected Leads\">";
?>
After you select the leads and submit the form , your script should show them in a list with hidden inputs (with name=leads[] and value=the_lead's_id) and next to each lead there will be a dropdown box () which will be populated with all the closers.
After choosing and sending the second form your script will "run" all-over the leads' ids array and update each and every one of them.
Got the idea or you want some code?

Categories