Error Notice: Undefined index: in search php

0

I'm doing a search in PHP and I get Notice: Undefined index:str I do not know how to solve it because I already have it called from the form where I get the information.

I leave the search code.

<form method="get" action="index.php" name="searchform" id="searchform">
<input type="text" name="str" id="str">
<input type="submit" name="submit" id="submit" value="Search">
</form>
<?php

$user = "root"; 
$password = ""; 
$host = "localhost"; 
$dbase = "employees_assign"; 
$table = "tbl_employees";
$str;

$search_term= $_GET['str'];

mysqli_connect($host,$user,$password); 
@mysqli_select_db($dbase) or die("Unable to select database");

if(empty($search_term))
{
echo ("");
}
else
{

$result1= mysqli_query( "SELECT * FROM $table WHERE emp_fname LIKE '%$search_term%") 
or die("SELECT Error: ".mysqli_error()); 




$count= mysqli_num_rows($result1);

if ($count == 0)
{
echo "<fieldset><b>No Results Found for Search Query '$search_term'</b></fieldset>";
}
else
{
print "<table border=1>\n"; 
while ($row = mysqli_fetch_array($result1)){ 

$emp_fname= $row['emp_fname'];
$emp_lname= $row['emp_lname'];
print "<tr>\n"; 

print "</td>\n";
print "\t<td>\n"; 
print "<font face=arial size=4/><div align=center>$emp_fname</div></font>"; 
print "</td>\n";
print "\t<td>\n"; 
echo "<font face=arial size=4/>$emp_lname</font>";
print "</td>\n";
print "</tr>\n"; 
} 

print "</table>\n"; 
}
}
?> 

I also have a code that prints the details and I get an error, I appreciate the help I'm new in PHP so do not give me that hard.

<div id="content">
    details view.
    <?php



    //echo '<li><a href="index.php?id='.$emp['emp_id'].'">'.$emp['emp_fname'].' '.$emp['emp_lname'].'</a></li>'


    ?>
    </div>

Thank you very much!

    
asked by Bella 19.10.2017 в 09:17
source

2 answers

0

Your error is in this part of your code where you try to access the index 'str' of the array GET but this does not yet exist because the form has not been sent, I recommend you change it for this

//Esto es para que te guies donde está el error
$user = "root"; 
$password = ""; 
$host = "localhost"; 
$dbase = "employees_assign"; 
$table = "tbl_employees";
$str;

//Asi lo tenias
$search_term= $_GET['str'];

//Te recomiendo que lo pongas así, de esta manera te aseguras que tenga el valor correcto solo
//cuando existe ese índice en el arreglo $_GET
$search_term = isset($_GET['str']) ? $_GET['str'] : '';
    
answered by 19.10.2017 в 14:22
0

Validate with isset that you receive $ _GET ['str'].

$search_term= isset($_GET['str']) ? $_GET['str'] : '';
    
answered by 19.10.2017 в 15:34