sammy
sammy

Reputation: 717

Make a searching box

I'm trying to make a searching box, but when I've running the page, no search results will appears. I use of name and family fields for searching. so my code is here:

      <fieldset class="fdex" >
            <legend><span class="style4">لیست خدمات انجام شده</span></legend>

            <form name="form1"  dir="rtl" method="post" action="">
<label for="search"> جستجو </label> <br>
<input name="search" type="text" size="40" maxlength="50" placeholder="جستجو کنید"><br />
<input type="radio" name="search_type" value="family" checked="checked">جستجو بر اساس فامیل<br/>
<input type="radio" name="search_type" value="name">جستجو بر اساس نام<br/>
<input type="submit" name="search_user_hasjob" value="جستجو"/>
</form>

            <?php
if(isset($_POST['search_user_job_hasjob']))
{
$db_hostname = 'localhost';
$db_database = 'site';
$db_username = 'root';
$db_password = '';

// Create connection
$conn = new mysqli($db_hostname, $db_username, $db_password, $db_database);
mysqli_set_charset($conn, "utf8");

$field = mysql_real_escape_string($_POST['search_type']);
$value = mysql_real_escape_string($_POST['search']);

// Check connection
if ($conn->connect_error)
    die("Connection failed: " . $conn->connect_error);

// Statement to get all clients that have job and the comments belong to it
$sql_clientName = "
SELECT tablesite.id_user,
          tablesite.name,
          tablesite.family,
          tablesite.phone_number,
          tablesite.email,
          relation.comments
FROM  tablesite
    INNER JOIN relation
    ON tablesite.id_user=relation.user_id
   INNER JOIN job_list
    ON relation.job_id=job_list.job_id
    WHERE $field LIKE '%".$value."%'
GROUP BY tablesite.name;";

// Statement to get specific client job info by User id
$sql_clientJob = "
SELECT job_list.job_name, relation.comments
FROM tablesite
  INNER JOIN relation
    ON tablesite.id_user=relation.user_id
  INNER JOIN job_list
    ON relation.job_id=job_list.job_id
WHERE $field LIKE '%".$value."%' AND id_user = ?;";

$stmt = $conn->prepare($sql_clientName);
$stmt->execute();
$output = $stmt->get_result();
$stmt->close();

// Go through all clients and print them out
echo "
<table width='900px'>
<tr>
    <td>نام</td><td>نام خانوادگی</td><td>تلفن</td><td>مشاغل</td>
</tr>
";

while ($row = $output->fetch_array(MYSQLI_ASSOC))
{
    echo "
    <tr>
        <td>" . $row['name'] . "</td><td>" . $row['family'] . "</td><td>" . $row['phone_number'] . "</td>
    ";

    // We call statement once
    $stmt1 = $conn->prepare($sql_clientJob);
    $stmt1->bind_param("i", $row['id_user']);
    $stmt1->execute();
    $output1 = $stmt1->get_result();

    // Fetch the job name belong to the client
    echo "<td>";
    while ($row1 = $output1->fetch_array(MYSQLI_ASSOC))
    {
        echo $row1['job_name'] . ", ";
    }
    echo "</td>";

    echo '</tr>';
}
echo "</table>";
$stmt1->close();
}
?>

</fieldset>

So when I'm trying to get information, page give me null. thanks for your help.

Upvotes: 1

Views: 72

Answers (1)

Maytham Fahmi
Maytham Fahmi

Reputation: 33437

This is because your submit button name and in your $_POST has different name:

<input type="submit" name="search_user_hasjob" value="جستجو"/>

$_POST has not the same submit button name:

if (isset($_POST['search_user_job_hasjob']))

This mean your condition does never accept the form submitting and won't return any search results, try to correct it so both has same name like

if (isset($_POST['search_user_hasjob']))

It should work


Addition
Regarding problem in comments from OP with Fatal error: Call to a member function close().

This is because you have $stmt1->close(); out of while loop curly braces { }, what you need to do is to move the $stmt1->close(); inside while loop like this:

....
$output1 = $stmt1->get_result();
$stmt1->close();
....etc

This should help with out digging deep in code analysis.

Upvotes: 3

Related Questions