我正在尝试创建一个搜索框,但是当我运行该页面时,不会显示任何搜索结果。我使用名称和家庭字段进行搜索。所以我的代码在这里:
<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。谢谢你的帮助。
答案 0 :(得分:3)
这是因为您的提交按钮名称和$_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();
中有{ }
,你需要做的就是像这样在while循环中移动$stmt1->close();
:
....
$output1 = $stmt1->get_result();
$stmt1->close();
....etc
这有助于深入挖掘代码分析。