这没有任何意义。我所拥有的这是一个下拉列表,它将选定的部分添加到选项中,并使用PDO填充MySQL的所有其余选项。我的问题是,当填写所有字段时,它只填写选择框上方的字段。下面的所有字段都没有填写。如果我删除了下拉列表的代码,那么每个字段都会填写。这很奇怪。我只包含了一部分HTML代码,因为它已经退出了很长时间。有人不知道为什么会这样吗?
<?php
$sid = $_GET["sid"];
$sql = "SELECT * FROM signings WHERE id = '$sid'";
$stmt = $db->query($sql);
$row =$stmt->fetchObject();
$custid = $row->customer;
?>
<div class="container center col-md-9">
<form action="formpross.php" id="testForm" method="post" class="form-horizontal" role="form">
<div class="row well well-lg">
<div class="col-md-4">
<label for="signdate" class="control-label">
SignStart:
</label>
<input name="signstart" id="datetimepicker" type="text" class="form-control input-sm" value="<?php echo $row->signstart;?>">
</div>
<div class="col-md-4">
<label for="signend"class="control-label">
SignEnd:
</label>
<input name = "signend" id="datetimepicker1" type="text" class="form-control input-sm"value="<?php echo $row->signend;?>">
</div>
<div class="col-md-4">
<label for="customer" class="control-label">
Customer:
</label>
// THIS IS WHERE THE CODE THAT SEAMS TO BE CASING THE PROBLEM IS
<select name="customers"id="customer" class="form-control input-sm">
<?php
$result = $db->query("SELECT custid, name FROM customers WHERE pid = $pid");
foreach($result as $row)
{
echo '<option value="'.$row['custid'].'"';
if($row['custid']==$custid)
{
echo ' selected';
}
echo '>'. $row['name'] . '</option>'."\n";
}
?>
</select>
// THIS IS WHERE THE CODE THAT SEAMS TO BE CASING THE PROBLEM ENDS
</div>
<div class="col-md-4">
<label for="contact" class="control-label">
Contact:
</label>
<input name="contact" class="form-control input-sm" id="contact"value="<?php echo $row->contact;?>">
</div>
<div class="col-md-4">
<label for="samount" class="control-label">Signing Amount:</label>
<input name="samount" class="form-control input-sm" id="samount"value="<?php echo $row->samount;?>">
</div>
<div class="col-md-4">
<label for="prtamount" class="control-label">Printing Amount:</label>
<input name="prtamount" class="form-control input-sm" id="prtamount" value="<?php echo $row->prtamount;?>" data-toggle="tooltip" data-placement="left" title="What you charge for printing">
</div>
<div class="col-md-4 col-md-offset-4">
<label for="ordern" class="control-label">
Order #:
</label>
<input name="ordern" class="form-control input-sm" id="order#" value="<?php echo $row->ordern;?>">
</div>
</div>
答案 0 :(得分:2)
实际上,这很有道理。您正在覆盖页面顶部的变量。
您定义$row
:
$row = $stmt -> fetchObject();
然后,在您选择的foreach
循环中,您将重新定义$row
:
foreach($result as $row) { ... }
简单修复,将foreach中的变量更改为其他内容,例如:
foreach($result as $options) { ... }
答案 1 :(得分:0)
在下拉列表的循环中,使用名为$row
的变量循环。然后,再往下,您尝试再次使用$row
预先填充值,并认为它与顶部定义的$row
相同。
只需将$row
重命名为其他内容即可。例如:
// Before:
foreach($result as $row)
// After:
foreach($result as $dropDownRow)