Php 在HTML文本框中显示SQL查询结果
我创建了一个名为Php 在HTML文本框中显示SQL查询结果,php,html,mysqli,Php,Html,Mysqli,我创建了一个名为users的数据库表,并创建了一个名为forms.php的php文件forms.php有两个表单。第一种形式是搜索,第二种形式是数据库中的记录应出现: <form method="post" action="search.php"> <div class="input-group has-success"> <input id="bt
users
的数据库表,并创建了一个名为forms.php
的php文件forms.php
有两个表单。第一种形式是搜索,第二种形式是数据库中的记录应出现:
<form method="post" action="search.php">
<div class="input-group has-success">
<input id="btn-input" name="search" type="text" class="form-control input-md" placeholder="Please type the ID number of the user you want to update..." />
<span class="input-group-btn">
<button class="btn btn-success btn-md" id="btn-chat" type="submit">Search</button>
</span>
</div>
</form>
</div>
<div class="col-md-6">
<form role="form">
<div class="form-group">
<label>username</label>
<input class="form-control" placeholder="Placeholder">
</div>
<div class="form-group">
<label>Password</label>
<input class="form-control">
</div>
<div class="form-group">
<label>UserType</label>
<input class="form-control">
</div>
</form>
搜寻
用户名
密码
用户自定义类型
这是用于搜索的php脚本的第一种形式。这是我的search.php中的脚本:
<?php
$conn = mysqli_connect("localhost", "root", "", "asset_inventory");
if($conn === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$search = mysqli_real_escape_string($conn, $_POST['search']);
$sql = "SELECT * FROM users WHERE user_id='$search'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$id=$row['user_id'];
$name=$row['user_name'];
$pass=$row['user_pass'];
$type=$row['user_type'];
header("location: forms.php");
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
现在我想使用search.php
中提到的变量,这些变量包含要在标题forms.php
的第二个表单中显示的行。有什么建议吗?我被困在这一点上了。谢谢 您有两种选择:
我会尽量让我的解释/解决方案尽可能简单,因为您的代码表明您刚刚开始使用这些东西 编写search.php是为了从数据库返回数据。这方面做得很好。问题是一旦你有了这些数据,你就什么都不用做了。我说的是这些代码行:
if (mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result))
{
$name=$row['user_name'];
$pass=$row['user_pass'];
$type=$row['user_type'];
}
}
else
{
echo "0 results";
}
因此,您正在设置$name、$pass和$type变量,这是您需要的数据。如果找到第二个用户,它将覆盖$name、$pass和$type变量,第一个用户数据将从PHP中消失(不过,不用担心,它会留在数据库中)
一个易于理解的解决方案是更改代码的位置。您应该在while()循环中显示数据。因此,如果将while()循环代码放在HTML页面中,可以创建一个表,显示如下用户信息:
<table>
<?php
if (mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result))
{
?>
<tr>
<td> <?php echo $name=$row['user_name']; ?> </td>
<td> <?php echo $name=$row['user_pass']; ?> </td>
<td> <?php echo $name=$row['user_type']; ?> </td>
</tr>
<?php
}
}
else
{
?>
<tr>
<td colspan="3">0 results</td>
</tr>
<?php
}
?>
</table>
0结果
其他解决方案包括函数和类,但您的代码表明您还不了解它们。我建议你尝试用你已有的知识来构建你的页面。然后阅读函数。然后重新构建同一页面,但为其创建一个函数。我将尽量使我的解释/解决方案尽可能简单,因为您的代码表明您刚刚开始使用这些内容 编写search.php是为了从数据库返回数据。这方面做得很好。问题是一旦你有了这些数据,你就什么都不用做了。我说的是这些代码行:
if (mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result))
{
$name=$row['user_name'];
$pass=$row['user_pass'];
$type=$row['user_type'];
}
}
else
{
echo "0 results";
}
因此,您正在设置$name、$pass和$type变量,这是您需要的数据。如果找到第二个用户,它将覆盖$name、$pass和$type变量,第一个用户数据将从PHP中消失(不过,不用担心,它会留在数据库中)
一个易于理解的解决方案是更改代码的位置。您应该在while()循环中显示数据。因此,如果将while()循环代码放在HTML页面中,可以创建一个表,显示如下用户信息:
<table>
<?php
if (mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result))
{
?>
<tr>
<td> <?php echo $name=$row['user_name']; ?> </td>
<td> <?php echo $name=$row['user_pass']; ?> </td>
<td> <?php echo $name=$row['user_type']; ?> </td>
</tr>
<?php
}
}
else
{
?>
<tr>
<td colspan="3">0 results</td>
</tr>
<?php
}
?>
</table>
0结果
其他解决方案包括函数和类,但您的代码表明您还不了解它们。我建议你尝试用你已有的知识来构建你的页面。然后阅读函数。然后重新构建相同的页面,但为其创建一个函数。这里是一种ajax方法。简便 表单页面
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
<script>
$(function(){
$(document).on("click", "#btn-chat", function(e) {
e.preventDefault();
//Built a url to send
var info = $("#form").serialize();
$.ajax({
type: "POST",
url: "search.php",
data: info,
success: function(result){
//$('#respoondarea').load(location.href + '#respondarea');
//$("#form")[0].reset();
$('#respond').html(result);
}
});
e.preventDefault();
});
});
</script>
<form method="post" id="form" action="search.php">
<div class="input-group has-success">
<input id="btn-input" name="search" type="text" class="form-control input-md" placeholder="Please type the ID number of the user you want to update..." />
<span class="input-group-btn">
<button class="btn btn-success btn-md" id="btn-chat" type="submit">Search</button>
</span>
</div>
</form>
<div id="respond"></div>
<?php
$conn = mysqli_connect("localhost", "root", "", "asset_inventory");
if($conn === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$search = $_POST['search'];
//$query_user = mysqli_query($conn,"SELECT * FROM users WHERE user_id='$search'");
$query_user = mysqli_query($conn,"SELECT * FROM users WHERE user_id LIKE '%$search%' ");
$row_user = mysqli_fetch_assoc($query_user);
$totalRows_user = mysqli_num_rows($query_user);
?>
<table class="table">
<thead>
<tr>
<th>Username</th><th>User pass</th><th>User type</th>
</tr>
</thead>
<tbody>
<?php do { ?>
<tr>
<td><?php echo $row_user['user_name']; ?></td>
<td><?php echo $row_user['user_pass']; ?></td>
<td><?php echo $row_user['user_type']; ?></td>
</tr>
<?php } while ($row_user = mysqli_fetch_assoc($query_user)); ?>
</tbody>
</table>
<?php if ($totalRows_user == 0) { // Show if record empty ?>
<div class="alert alert-info" role="alert"> No record found </div>
<?php } // Show if recordset empty ?>
$(函数(){
$(文档)。在(“单击”上,“btn聊天”,功能(e){
e、 预防默认值();
//构建了一个要发送的url
var info=$(“#形式”).serialize();
$.ajax({
类型:“POST”,
url:“search.php”,
数据:信息,
成功:函数(结果){
//$('respoondarea').load(location.href+'respoondarea');
//$(“#形式”)[0]。重置();
$('#respond').html(结果);
}
});
e、 预防默认值();
});
});
搜寻
搜索PHP页面
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
<script>
$(function(){
$(document).on("click", "#btn-chat", function(e) {
e.preventDefault();
//Built a url to send
var info = $("#form").serialize();
$.ajax({
type: "POST",
url: "search.php",
data: info,
success: function(result){
//$('#respoondarea').load(location.href + '#respondarea');
//$("#form")[0].reset();
$('#respond').html(result);
}
});
e.preventDefault();
});
});
</script>
<form method="post" id="form" action="search.php">
<div class="input-group has-success">
<input id="btn-input" name="search" type="text" class="form-control input-md" placeholder="Please type the ID number of the user you want to update..." />
<span class="input-group-btn">
<button class="btn btn-success btn-md" id="btn-chat" type="submit">Search</button>
</span>
</div>
</form>
<div id="respond"></div>
<?php
$conn = mysqli_connect("localhost", "root", "", "asset_inventory");
if($conn === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$search = $_POST['search'];
//$query_user = mysqli_query($conn,"SELECT * FROM users WHERE user_id='$search'");
$query_user = mysqli_query($conn,"SELECT * FROM users WHERE user_id LIKE '%$search%' ");
$row_user = mysqli_fetch_assoc($query_user);
$totalRows_user = mysqli_num_rows($query_user);
?>
<table class="table">
<thead>
<tr>
<th>Username</th><th>User pass</th><th>User type</th>
</tr>
</thead>
<tbody>
<?php do { ?>
<tr>
<td><?php echo $row_user['user_name']; ?></td>
<td><?php echo $row_user['user_pass']; ?></td>
<td><?php echo $row_user['user_type']; ?></td>
</tr>
<?php } while ($row_user = mysqli_fetch_assoc($query_user)); ?>
</tbody>
</table>
<?php if ($totalRows_user == 0) { // Show if record empty ?>
<div class="alert alert-info" role="alert"> No record found </div>
<?php } // Show if recordset empty ?>
这里是一种ajax方法。简便
表单页面
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
<script>
$(function(){
$(document).on("click", "#btn-chat", function(e) {
e.preventDefault();
//Built a url to send
var info = $("#form").serialize();
$.ajax({
type: "POST",
url: "search.php",
data: info,
success: function(result){
//$('#respoondarea').load(location.href + '#respondarea');
//$("#form")[0].reset();
$('#respond').html(result);
}
});
e.preventDefault();
});
});
</script>
<form method="post" id="form" action="search.php">
<div class="input-group has-success">
<input id="btn-input" name="search" type="text" class="form-control input-md" placeholder="Please type the ID number of the user you want to update..." />
<span class="input-group-btn">
<button class="btn btn-success btn-md" id="btn-chat" type="submit">Search</button>
</span>
</div>
</form>
<div id="respond"></div>
<?php
$conn = mysqli_connect("localhost", "root", "", "asset_inventory");
if($conn === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$search = $_POST['search'];
//$query_user = mysqli_query($conn,"SELECT * FROM users WHERE user_id='$search'");
$query_user = mysqli_query($conn,"SELECT * FROM users WHERE user_id LIKE '%$search%' ");
$row_user = mysqli_fetch_assoc($query_user);
$totalRows_user = mysqli_num_rows($query_user);
?>
<table class="table">
<thead>
<tr>
<th>Username</th><th>User pass</th><th>User type</th>
</tr>
</thead>
<tbody>
<?php do { ?>
<tr>
<td><?php echo $row_user['user_name']; ?></td>
<td><?php echo $row_user['user_pass']; ?></td>
<td><?php echo $row_user['user_type']; ?></td>
</tr>
<?php } while ($row_user = mysqli_fetch_assoc($query_user)); ?>
</tbody>
</table>
<?php if ($totalRows_user == 0) { // Show if record empty ?>
<div class="alert alert-info" role="alert"> No record found </div>
<?php } // Show if recordset empty ?>
$(函数(){
$(文档)。在(“单击”上,“btn聊天”,功能(e){
e、 预防默认值();
//构建了一个要发送的url
var info=$(“#形式”).serialize();
$.ajax({
类型:“POST”,
url:“search.php”,
数据:信息,
成功:函数(结果){
//$(“#respoondarea”).load(location.href+”#respond