Php 从复选框中选择数据库检索值并在另一个页面中查看,但重新加载页面时不会显示结果。如何解决?
使用PHP,我获取了表中的所有记录,用户可以使用复选框从中选择特定记录。所选记录将在其他页面中查看。但问题是重新加载页面时,结果不会被查看。 此代码用于从数据库获取所有记录,并使用复选框提交Php 从复选框中选择数据库检索值并在另一个页面中查看,但重新加载页面时不会显示结果。如何解决?,php,mysql,Php,Mysql,使用PHP,我获取了表中的所有记录,用户可以使用复选框从中选择特定记录。所选记录将在其他页面中查看。但问题是重新加载页面时,结果不会被查看。 此代码用于从数据库获取所有记录,并使用复选框提交 <div class="col-lg-8 card"> <table class="table table-striped table-hover table-bordered datatable"> <thead style="background: #9
<div class="col-lg-8 card">
<table class="table table-striped table-hover table-bordered datatable">
<thead style="background: #967272; color: black;">
<tr>
<th style="width: 50px;"></th>
<th style="color: #3b3b3b;">Company Name</th>
<th style="color: #3b3b3b;">Service</th>
<th style="color: #3b3b3b;">email</th>
<th style="color: #3b3b3b;">Logo</th>
</tr>
</thead>
<tbody>
<form name="" action="index.php" method="POST">
<?php
$sql="select * from company_details ";
$result = $con->query($sql);
if ($result->num_rows > 0) {
?>
<?php
while($row = $result->fetch_assoc()) {
?>
<tr>
<td>
<center><input type="checkbox" name="check[<?php echo $row['id'];?>]" value="1"></center>
</td>
<td><input type="" name="company_name[]" value="<?php echo $row['company_name'];?>"></label></td>
<td><input type="" name="service[]" value="<?php echo $row['service'];?>"></label></td>
<td><input type="label" name="email[]" value="<?php echo $row['email'];?>"></label></td>
<td><?php echo '<img src="' . $row['image_path4']. '" width="100" height="100">'; ?></td>
</tr>
<?php }} //end of while loop ?>
</div>
<button type="submit" class="btn btn-hg btn-success" name="AddEbooks">Submit</button>
</form>
公司名称
服务
电子邮件
标志
提交
此代码显示所选记录
<?php include 'config.php';?>
<?php
session_start();
if(!isset($_POST['check']))
{
?>
<?php
$sql="select * from company_details";
$result = $con->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$ID=$row['id'];
/* Get the Book ID and now we have to fetch from $_POST the value from the form */
if (array_key_exists($ID, $_POST["check"])) {
$ischecked=$_POST["check"][$ID];
/* See if this has a value of 1. If it does, it means it has been checked */
if ($ischecked==1) {
?>
<div class="col-lg-6" style="padding-top: 20px;">
<section>
<div class="well" >
<div class="card" >
<div class="row ">
<div class="col-md-4">
<?php echo '<img src="' . $row['image_path4']. '" width="100" height="100">'; ?>
</div>
<div class="col-md-8 px-3">
<div class="card-block px-3">
<h4 class="card-title"><?php echo $row ['company_name']; ?></h4>
<div >
<label>Service Type</label>
<?php echo $row ['service']; ?>
</div>
<div >
<label>email</label>
<?php echo $row ['email']; ?>
</div>
<div >
<label>About</label>
<?php echo substr($row['details'], 0, 300); ?>
</div>
<a href="#" class="btn btn-success">Read More</a>
</div>
</div>
</div>
</div>
</section>
</div>
<?php
}
}
}
}
?>
服务类型
电子邮件
关于
将$\u POST[“check”]
存储在一个会话变量中,并使用该会话变量使用数组\u key\u exists进行检查
更新
<?php
session_start();
include 'config.php';
if(!isset($_SESSION['checked'])){
$_SESSION['checked']=$_POST['check'];
}
$checked = $_SESSION['checked'];
if(!empty($checked))
{
$sql="select * from company_details";
$result = $con->query($sql);
if ($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
$ID=$row['id'];
/* Get the Book ID and now we have to fetch from $_POST the value from the form */
if (array_key_exists($ID, $checked)) {
$ischecked=$checked[$ID];
/* See if this has a value of 1. If it does, it means it has been checked */
if ($ischecked==1)
{
?>
<div class="col-lg-6" style="padding-top: 20px;">
<section>
<div class="well">
<div class="card" >
<div class="row ">
<div class="col-md-4">
<?php echo '<img src="' . $row['image_path4']. '" width="100" height="100">'; ?>
</div>
<div class="col-md-8 px-3">
<div class="card-block px-3">
<h4 class="card-title"><?php echo $row ['company_name']; ?></h4>
<div >
<label>Service Type</label>
<?php echo $row ['service']; ?>
</div>
<div >
<label>email</label>
<?php echo $row ['email']; ?>
</div>
<div >
<label>About</label>
<?php echo substr($row['details'], 0, 300); ?>
</div>
<a href="#" class="btn btn-success">Read More</a>
</div>
</div>
</div>
</div>
</section>
</div>
<?php
}
}
}
}
}
?>
服务类型
电子邮件
关于
谢谢你的评论。我应该在查看页面中使用它,不是吗?是的,在下一个页面中,您将要卸载所选记录的数据。我会根据您的评论如上所述编辑我的代码,但现在它会显示未定义的索引数组错误消息。你能找出我的错误在哪里吗?你在哪里设置了会话变量?我在代码中看不到它在第二个代码段中,如下所示