[英]how to show two mysql tables values at one while loop using php
我创建了两个表,table1 和 table2 table1 包括(id、代码和标题)。 table2 包括 id、table1_id、column1、column2、column3 我想在 table1(代码、标题)上插入值并使用 PHP 和插入 table2(column1、column2、column3)在表中显示并在 table1 值下显示它。 这是我的代码
<?php
include('config.php');
$ret=" SELECT * from table1
";
$stmt= $mysqli->prepare($ret);
//$stmt->bind_param('i',$aid);
$stmt->execute() ;//ok
$res=$stmt->get_result();
$cnt=1;
while($row=$res->fetch_object())
{
?>
<div class="card shadow">
<div class="card-header py-0">
<table class="table">
<tr >
<td style="border: none"><?php echo $row->codes; ?></td>
<td style="border: none"><?php echo $row->titles; ?></td>
<td style="border: none">edit</td>
</tr>
</table>
</div>
<div class="card-body">
<div class="table-responsive table mt-2" id="dataTable" role="grid" aria-
describedby="dataTable_info">
<table class="table my-0" id="dataTable">
<thead>
<tr>
<th>column 1</th>
<th>column 2</th>
<th> column 3 </th>
</tr>
</thead>
<tbody>
<?php
$ret=" SELECT * from table2";
$stmt= $mysqli->prepare($ret);
$stmt->execute();
$res=$stmt->get_result();
while($row=$res->fetch_object() )
{
?>
<tr>
<td><?php echo $row->column1 ?></td>
<td> <?php echo $row->column3 ?></td>
<td> <?php echo $row->column3 ?></td>
</tr>
<?php } ?>
</tbody>
<tfoot>
<tr></tr>
</tfoot>
</table>
</div>
<h3 class="text-dark mb-4"> <button class="btn btn-info " data-toggle="modal" data-
target="#login_itech3">adding column</button></h3>
</div>
</div>
<br>
<?php } ?>
你犯了两个错误:
$row
进行迭代where
条件中指定table1_id
试试下面的代码,我确定它会工作<?php
include('config.php');
$ret = " SELECT * from table1
";
$stmt = $mysqli->prepare($ret);
//$stmt->bind_param('i',$aid);
$stmt->execute(); //ok
$res = $stmt->get_result();
$cnt = 1;
while ($row1 = $res->fetch_object()) {
?>
<div class="card shadow">
<div class="card-header py-0">
<table class="table">
<tr>
<td style="border: none"><?php echo $row1->codes; ?></td>
<td style="border: none"><?php echo $row1->titles; ?></td>
<td style="border: none">edit</td>
</tr>
</table>
</div>
<div class="card-body">
<div class="table-responsive table mt-2" id="dataTable" role="grid" aria- describedby="dataTable_info">
<table class="table my-0" id="dataTable">
<thead>
<tr>
<th>column 1</th>
<th>column 2</th>
<th> column 3 </th>
</tr>
</thead>
<tbody>
<?php
$ret = " SELECT * from table2 where table1_id = ".$row1->id;
$stmt = $mysqli->prepare($ret);
$stmt->execute();
$res = $stmt->get_result();
while ($row2 = $res->fetch_object()) {
?>
<tr>
<td><?php echo $row2->column1 ?></td>
<td> <?php echo $row2->column3 ?></td>
<td> <?php echo $row2->column3 ?></td>
</tr>
<?php } ?>
</tbody>
<tfoot>
<tr></tr>
</tfoot>
</table>
</div>
<h3 class="text-dark mb-4"> <button class="btn btn-info " data-toggle="modal" data- target="#login_itech3">adding column</button></h3>
</div>
</div>
<br>
<?php } ?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.