制作一个搜索框


Make a searching box

我正在尝试制作一个搜索框,但当我运行页面时,不会显示任何搜索结果。我使用名称和族字段进行搜索。所以我的代码在这里:

      <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>

所以当我试图获取信息时,页面会给我null。谢谢你的帮助。

这是因为您的提交按钮名称和$_POST中的名称不同:

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

$_POST具有不同的提交按钮名称:

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

这意味着你的条件永远不会接受表单提交,也不会返回任何搜索结果,请尝试更正它,使两者具有相同的名称,如

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

它应该工作


添加
关于OP与Fatal error: Call to a member function close()意见中的问题。

这是因为您在while循环大括号{ }中有$stmt1->close();,您需要做的是将$stmt1->close();移动到while循环中,如下所示:

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

这将有助于深入挖掘代码分析。