[英]Cannot display Mysql search results from search in a different div
I am trying to display MySQL results (with and without a search) using three divisions. 我试图使用三个划分来显示MySQL结果(有和没有搜索)。 Div 1 has radio buttons for selecting the viewing the results. 第1分区具有用于选择查看结果的单选按钮。 Div 2 has a text that states all members are displayed, a text search box for Name, and a drop-down menu for Type. 第2区的文字说明所有成员均已显示,名称的文字搜索框和类型的下拉菜单。 The results of the search are displayed in Div 3. 搜索结果显示在Div 3中。
When All Members is selected the text "Display All Members" shows in Div 2 and the correct data is displayed in Div 3 (works correctly). 选择所有成员后,文本“显示所有成员”将显示在Div 2中,并且正确的数据将显示在Div 3中(正常工作)。 When the Name radio button is selected the correct text search box displays in Div 2 but I receive the following errors in Div 3: 选择“名称”单选按钮后,正确的文本搜索框会在第2分区中显示,但在第3分区中会收到以下错误消息:
Warning: mysqli_query(): Couldn't fetch mysqli in /home/desgar20/elrenochamber.com/member_dir_test2.php on line 171 警告:mysqli_query():无法在第171行的/home/desgar20/elrenochamber.com/member_dir_test2.php中获取mysqli。
Warning: mysqli_error() expects exactly 1 parameter, 0 given in /home/desgar20/elrenochamber.com/member_dir_test2.php on line 173 Could not access database: 警告:mysqli_error()恰好需要1个参数,第173行的/home/desgar20/elrenochamber.com/member_dir_test2.php中给定的0个参数无法访问数据库:
If I enter text into the search box and submit, Div 2 and Div 3 go blank 如果我在搜索框中输入文本并提交,则Div 2和Div 3变为空白
I am using javascript to show/hide the divisions and php for the search. 我正在使用javascript显示/隐藏除法和php进行搜索。
I have searched for answers but have not found anything to resolve this issue. 我已经搜索了答案,但未找到任何解决此问题的方法。 I don't know what I am missing. 我不知道我在想什么。 Help- -need advice. 帮助-需要建议。
<style type="text/css">
.box {
display: none;
}
</style>
<script type="text/javascript">
//Show or application part based on selection
$(document).ready(function(){
$('input[type="radio"]').click(function(){
if($(this).attr("value")=="all"){
$(".box").not(".all").hide();
$(".all").show();
$(".all_listing").show();
}
if($(this).attr("value")=="name"){
$(".box").not(".name").hide();
$(".name").show();
$(".all_name").show();
}
if($(this).attr("value")=="type"){
$(".box").not(".type").hide();
$(".type").show();
$(".all_type").show();
}
});
});
</script>
</head>
<body>
<?php
require_once 'php/dbconnect.php'; // Connect to database
$connection = db_connect();
?>
<div id="container">
<div id="service">
<div id="web">
<img width="150px" src="images/search1.png" />
<h3>Member <strong><span class="green">Directory</span></strong></h3>
<strong>View Members By:</strong><br /><br />
<div id="sortOptions">
<label><input type="radio" name="sortRadio" value="all"> All Members</label><br />
<label><input type="radio" name="sortRadio" value="name"> Name</label><br />
<label><input type="radio" name="sortRadio" value="type"> Type</label>
</div><!-- sortOptions -->
</div><!-- end web -->
<div id="vector">
<div class="all box">
<h3>Display <strong><span class="green">All Members</span></strong></h3>
<p>All Members Displayed</p>
</div><!-- all box -->
<div class="name box">
<h3>Display by <strong><span class="green">Member Name</span></strong></h3>
<p><form name="namesearch" method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>">
Name: <input type="text" name="find">
<input type="submit" name="search" value="Search Names">
</form></p>
</div><!-- name box -->
<div class="type box">
<h3>Display by <strong><span class="green">Member Type</span></strong></h3>
<p><form action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>" method="post">
Type: <select name="type" id="type">
<option value="">-- Select A Type --</option>
<?php
$query = "SELECT * FROM select_type"; //create type drop-down menu
$result = mysqli_query($connection, $query);
while ($line = mysqli_fetch_array($result)) {
echo "<option value='". $line['type'] ."'>". $line['type']."</option>";
}
?>
</select>
<input type="submit" name="searchType" value="Search Types">
</form>
</div><!-- type box -->
</div><!-- end vector -->
</div><!-- end service-->
<div id="media" class="group">
<div class="all_listing box">
<p>Directory Listing</p>
<!-- Start Directory Listing -->
<?php
$sql = "SELECT * FROM members ORDER BY name"; // Database query and results
$result = mysqli_query($connection, $sql);
while($row = mysqli_fetch_array($result)) {
// Check record for website
if ($row['web']!== "") {
echo "<a target=blank href=". $row['web'] . ">" .$row["name"] . "</a><br> " .
"". $row["type"] . "<br>" .
"Address: " . $row["physicaladdress"] . "<br>" .
"Phone: " . $row["phone"] . "<br>" . "<hr>";
}
else {
echo $row["name"]. "<br> " .
"". $row["type"] . "<br>" .
"Address: " . $row["physicaladdress"] . "<br>" .
"Phone: " . $row["phone"] . "<br>" . "<hr>";
}
}
$connection->close();
?>
</div><!-- all_listing box -->
<div class="all_name box">
<p>Results based on Name Search</p>
<?php
if (isset($_POST['search'])) { // Has "Select Names button ben pushed
$find = $_POST['find'];
$sql = "SELECT * FROM members WHERE name LIKE '%" . $find . "%' ";
$result = mysqli_query($connection, $sql);
if(! $result) {
die ('Could not access database: ' . mysqli_error());
}
while($row = mysqli_fetch_array($result)) {
// Check record for website
if ($row['web']!== "") {
echo "<a target=blank href=". $row['web'] . ">" .$row["name"] . "</a><br> " .
"". $row["type"] . "<br>" .
"Address: " . $row["physicaladdress"] . "<br>" .
"Phone: " . $row["phone"] . "<br>" . "<hr>";
}
else {
echo $row["name"]. "<br> " .
"". $row["type"] . "<br>" .
"Address: " . $row["physicaladdress"] . "<br>" .
"Phone: " . $row["phone"] . "<br>" . "<hr>";
}
}
}
?>
</div><!-- all_name box -->
</body>
</html>
I modified the code as recommendation but still receive the same errors as mention above. 我将代码修改为建议,但仍然收到与上述相同的错误。 Here is the area that I am having difficulty with: 这是我遇到困难的领域:
This is the division (div 1) that will display the search option in div 2: 这是将在div 2中显示搜索选项的部门(div 1):
<div id="web">
<div id="sort_options">
<label><input type="radio" name="sortRadio" value="name"> Name</label><br />
</div><!-- sort_options -->
</div><!-- end web -->
This is the search feature for div 2: 这是div 2的搜索功能:
<div class="name box">
<form name="namesearch" method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>">
Name: <input type="text" name="find">
<input type="submit" name="search" value="Search Names">
</form></p>
</div>
This is the php code in div 3 to run the query with input from div 2: 这是div 3中的php代码,可使用div 2的输入来运行查询:
<div class="all_name box">
<?php
if (isset($_POST['search']))
{
$find = $_POST['find'];
$sql = "SELECT * FROM members WHERE name LIKE '%" . $find . "%' ";
$result = mysqli_query($connection, $sql);
if ( $result == false )
{
echo ("Error description: " . mysqli_error($connection));
}
else
{
while($row = mysqli_fetch_array($result))
{
// Check record for website
if ($row['web']!== "")
{
echo "<a target=blank href=". $row['web'] .">" .$row["name"] . "</a><br> " .
"". $row["type"] . "<br>" .
"Address: " . $row["physicaladdress"] . "<br>" .
"Phone: " . $row["phone"] . "<br>" . "<hr>";
}
else
{
echo $row["name"]. "<br> " .
"". $row["type"] . "<br>" .
"Address: " . $row["physicaladdress"] . "<br>" .
"Phone: " . $row["phone"] . "<br>" . "<hr>";
}
}
}
}
?>
</div>
What am I doing wrong here? 我在这里做错了什么?
Change this snippet: 更改此代码段:
$sql = "SELECT * FROM members ORDER BY name"; // Database query and results
$result = mysqli_query($connection, $sql);
while($row = mysqli_fetch_array($result)) {
to at least 至少
$sql = "SELECT * FROM members ORDER BY name"; // Database query and results
$result = mysqli_query($connection, $sql);
while($result !== false && $row = mysqli_fetch_array($result)) {
Reason: If your query is faulty, $result
will have the value false
which will cause the given error. 原因:如果查询错误,则$result
的值将为false
,这将导致给定的错误。
Better code would be like this: 更好的代码是这样的:
$sql = "SELECT * FROM members ORDER BY name"; // Database query and results
$result = mysqli_query($connection, $sql);
if ( $result == false )
{
// handle error with mysqli_... functions
}
else
{
while($row = mysqli_fetch_array($result)) {
...
As always: take the error as standard, success is the exception! 一如既往:以错误为标准,成功是例外!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.