我想弹出一个从数据库中搜索人员的弹出窗口。
仅当我没有输入“ while($ row = $ sql-> fetch(PDO :: FETCH_ASSOC))”时,才会显示弹出窗口。我已经在没有弹出窗口的其他页面上尝试了相同的代码。它的工作。当我尝试将代码用于弹出窗口时,它不起作用。
<!--Button to open the modal -->
<button class="myBtnSea" id="myBtnSea" data-toggle="modal" data-target="#myModalSea"><i class="fa fa-search"></i></button>
<!-- The Modal -->
<div id="myModalSea" class="modalSea">
<!-- Modal content -->
<div class="modalSea-content">
<span class="closeSea">×</span>
<div id="SearchStaff">
<h3>Search for Staff</h3>
<form action="search.php" method="POST">
<div class="container">
<p>You can search either by name or IC number.</p>
<hr>
<input type="text" name="nama" placeholder="Search By Name" style="text-transform:uppercase"> <br><br>
<input type="text" name="icnum" placeholder="Search By IC Number" >
<button class="btn" name="search">Search</button><br><br>
<!--Table for display the data after search -->
<table id="table">
<tr>
<th>Name</th>
<th>Department</th>
<th>Campus</th>
</tr>
<!--This code makes the popup does not appear -->
<?php while ($row = $sql->fetchAll(PDO::FETCH_ASSOC)) { ?>
<tr>
<td><?php echo $row['name']?></td>
<td><?php echo $row['dept']?></td>
<td><?php echo $row['campus']?></td>
</tr>
<?php } ?>
</table>
</div>
</form>
</div>
</div>
</div>
答案 0 :(得分:0)
在此处$sql>fetch(PDO::FETCH_ASSOC)
循环时,您会遇到语法错误,应该是这样的$sql->fetchAll
使用PDO提取所有记录的正确语法是
while ($row = $sql->fetchAll(PDO::FETCH_ASSOC))
您可以查看有关PDOStatement::fetch
的更多信息更新:
似乎您没有在按钮中设置数据目标和数据切换
<button class="myBtnSea" id="myBtnSea" data-toggle="modal" data-target="#myModalSea"><i class="fa fa-search"></i></button>
答案 1 :(得分:0)
首先,您不能更改引导程序的类别才能使用它。您只能扩展类,否则引导程序将无法工作。我已经更新了整个代码。请尝试。
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
<!--Button to open the modal -->
<button class="myBtnSea" id="myBtnSea" data-toggle="modal" data-target="#myModalSea"><i class="fa fa-search"></i> Modal</button>
<!-- The Modal -->
<div id="myModalSea" class="modal fade modalSea" role="dialog">
<div class="modal-dialog">
<!-- Modal content -->
<div class="modal-content modalSea-content">
<div class="modal-header">
<span class="close closeSea" data-dismiss="modal">×</span>
</div>
<div id="SearchStaff">
<h3>Search for Staff</h3>
<form action="search.php" method="POST">
<div class="container">
<p>You can search either by name or IC number.</p>
<hr>
<input type="text" name="nama" placeholder="Search By Name" style="text-transform:uppercase"> <br><br>
<input type="text" name="icnum" placeholder="Search By IC Number" >
<button class="btn" name="search">Search</button><br><br>
<!--Table for display the data after search -->
<table id="table">
<tr>
<th>Name</th>
<th>Department</th>
<th>Campus</th>
</tr>
<!--This code makes the popup does not appear -->
<?php while ($row = $sql->fetchAll(PDO::FETCH_ASSOC)) { ?>
<tr>
<td><?php echo $row['name']?></td>
<td><?php echo $row['dept']?></td>
<td><?php echo $row['campus']?></td>
</tr>
<?php } ?>
</table>
</div>
</form>
</div>
</div>
</div>
</div>
第二,您错过了-
附近的fetch(PDO::FETCH_ASSOC)
连字符。请添加它。
还有一件事情,在循环结果之前,您应该检查查询返回的num行。