我正在尝试从数据库中获取数据。但是循环正在执行并多次显示数据。请看屏幕截图
<?php
$conn = mysqli_connect("localhost", "root", "","midata");
$sql = "SELECT * FROM campaign_profile ORDER BY id DESC";
?>
<div class="modal-body">
<form>
<?php
$result = mysqli_query($conn,$sql);
while($row = mysqli_fetch_array($result))
{
$name= $row['campaign_name'];
$issues= $row['issue_name'];
$ename= $row['unused_elements'];
$uquantity= $row['unused_number'];
$descript= $row['experience']; ?>
<div class="form-group">
<label for="recipient-name" class="control-label">Campaign Name:</label>
<input type="text" class="form-control c-square" id="recipient-name" value="<?php echo $row['campaign_name'];?>">
</div>
<div class="form-group">
<label for="recipient-name" class="control-label">Issues:</label>
<input type="text" class="form-control c-square" id="recipient-name" value="<?php echo $row['issue_name'];?>">
</div>
<div class="row col-md-12">
<div class="form-group col-md-6">
<label for="recipient-name" class="control-label">Element Name:</label>
<input type="text" class="form-control c-square" id="recipient-name" class="col-md-6" value="<?php echo $row['unused_elements'];?>">
</div>
<div class="form-group col-md-6">
<label for="recipient-name" class="control-label">Unused Quantity:</label>
<input type="text" class="form-control c-square" id="recipient-name" class="col-md-6" value="<?php echo $row['unused_number'];?>">
</div>
</div>
<div class="form-group">
<label for="message-text" class="control-label">Description:</label>
<textarea class="form-control c-square" id="message-text" value="<?php echo $row['experience'];?>"></textarea>
</div>
<?php } ?>
</form>
</div>
我不明白为什么会这样。请告诉我需要做什么
答案 0 :(得分:0)
可能是你的桌子有更多行,那么你期待。
使用此限制您的查询。
$sql = "SELECT * FROM campaign_profile ORDER BY id DESC limit 1";
或尝试使用id或主键获取数据。
或更改您的代码,如下所示。
<?php
$conn = mysqli_connect("localhost", "root", "","midata");
$sql = "SELECT * FROM campaign_profile ORDER BY id DESC";
?>
<div class="modal-body">
<form>
<?php
$name= "";
$issues= "";
$ename= "";
$uquantity= "";
$descript= "";
$result = mysqli_query($conn,$sql);
while($row = mysqli_fetch_array($result))
{
$name= $row['campaign_name'];
$issues= $row['issue_name'];
$ename= $row['unused_elements'];
$uquantity= $row['unused_number'];
$descript= $row['experience'];
} ?>
<div class="form-group">
<label for="recipient-name" class="control-label">Campaign Name:</label>
<input type="text" class="form-control c-square" id="recipient-name" value="<?php echo $row['campaign_name'];?>">
</div>
<div class="form-group">
<label for="recipient-name" class="control-label">Issues:</label>
<input type="text" class="form-control c-square" id="recipient-name" value="<?php echo $row['issue_name'];?>">
</div>
<div class="row col-md-12">
<div class="form-group col-md-6">
<label for="recipient-name" class="control-label">Element Name:</label>
<input type="text" class="form-control c-square" id="recipient-name" class="col-md-6" value="<?php echo $row['unused_elements'];?>">
</div>
<div class="form-group col-md-6">
<label for="recipient-name" class="control-label">Unused Quantity:</label>
<input type="text" class="form-control c-square" id="recipient-name" class="col-md-6" value="<?php echo $row['unused_number'];?>">
</div>
</div>
<div class="form-group">
<label for="message-text" class="control-label">Description:</label>
<textarea class="form-control c-square" id="message-text" value="<?php echo $row['experience'];?>"></textarea>
</div>
</form>
</div>
答案 1 :(得分:0)
尝试通过终端或phpmyadmin工具直接运行mysql select查询来分析查询结果。请使用php代码检查查询结果,如果两者相同,则表中包含重复数据。
答案 2 :(得分:0)
如果想要show single record
表示不需要while loop
就像下面这样做
通过添加record
limit 1
SELECT * FROM campaign_profile ORDER BY id DESC limit 1
PHP:
$result = mysqli_query($conn,$sql);
$row = mysqli_fetch_array($result);
$name= $row['campaign_name'];
$issues= $row['issue_name'];
$ename= $row['unused_elements'];
$uquantity= $row['unused_number'];
$descript= $row['experience'];