简体   繁体   English

<select><option>值未出现在第二个选择框中

[英]<select> <option> value not appearing on second select box

Background: I have an edit form accessed via a GET from a view form. 背景:我有一个通过GET从视图表单访问的编辑表单。

The form has two dynamic select fields: state and country . 表单具有两个动态选择字段: statecountry
When populated, the first select box shows the proper database entry, but the second is blank. 填充后,第一个选择框显示正确的数据库条目,但是第二个为空白。 Any fields entered after the first select show up as blank. 第一次选择后输入的所有字段均显示为空白。

The code is as follows: 代码如下:

include 'connection.php';

$newid = $_GET['id'];

try {

$conec = new Connection();
$con   = $conec->Open();
if ($con) {

$sql = "SELECT * FROM certco where id = " . $newid;
$re  = $con->query($sql);
foreach ($con->query($sql) as $row) {
?>

<link rel="stylesheet" href="gridstyle.css" />

<form action="processeditcertco.php" method="post">
<fieldset class="block">
<legend>Certification Companies</legend>
<label for="coname">Co Name</label>
<input type="text" name="coname" id="coname" value="<?php echo $row['coname']; ?>" size="65" />

<label for="shortname">Initials</label>
<input type="text" name="shortname" id="shortname" value="<?php echo $row['shortname']; ?>"  size="40" /> 

<label for="addr1">Address 1</label>
<input type="text" name="addr1" id="addr1" value="<?php echo $row['addr1']; ?>"  size="65" />

<label for="addr2">Address 2</label>
<input type="text" name="addr2" id="addr2" value="<?php echo $row['addr2']; ?>"  size="65" />

<label for="city">City</label>
<input type="text" name="city" id="city" value="<?php echo $row['city']; ?>"  size="65" />

<label for="zip">Zip</label>
<input type="text" name="zip" id="zip" value="<?php echo $row['zip']; ?>"  size="65" />

<input type="text" name="country" id="country" value="<?php echo $row['country']; ?>"  size="65" />
// above just to show that the 'country' field has data above select

<label for="state">State</label>
<select id="state" name="state">
<option value="<?php echo $row['state']; ?>"><?php echo $row['state']; ?></option>
// note this is exactly the same as the country code below that does not show data.

<?php
$conn = new PDO('mysql:host=localhost;dbname=money', 'muser', 'mpass');
$stmt = $conn->query('SELECT * from state where type = "state" or type = "capitol"');
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "<option value= " . $row['abbreviation'] . ">" . $row['name'] . "</option>";
}
?>
</select> 

<label for="country">Country</label>
<select id="country" name="country">
<option value="<?php echo $row['country']; ?>"><?php echo $row['country']; ?></option>
//no data shows in this option field.

<?php
$conn = new PDO('mysql:host=localhost;dbname=money', 'muser', 'mpass');
$stmt = $conn->query("SELECT * FROM country where currencycode > 0 order by menuposition asc");
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "<option value= " . $row['alpha3'] . ">" . $row['name'] . " - " . $row['officialname'] . "</option>";
}
?>
</select>

</fieldset>
<fieldset>
<input type="submit" class="button"  name="submit" value="Update Record" />
</fieldset>
</form>

<?php
} 
} else {
echo $con;
}
} catch (PDOException $ex) {
echo $ex->getMessage();
}
$conec->Close();
?>

I think the issue is that you're reusing the $row variable name. 我认为问题在于您正在重用$row变量名。 In this part of your code block: 在代码块的这一部分:

<?php
$conn = new PDO('mysql:host=localhost;dbname=money', 'muser', 'mpass');
$stmt = $conn->query('SELECT * from state where type = "state" or type = "capitol"');
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
   echo "<option value= " . $row['abbreviation'] . ">" . $row['name'] . "</option>";
}
?>
</select> 

<label for="country">Country</label>
<select id="country" name="country">
   <option value="<?php echo $row['country']; ?>"><?php echo $row['country']; ?></option>
//no data shows in this option field.

You're using $row variable name for the while() loop, which means that the original value of $row from your for() loop will be overwritten and end up being null by the time you get to the second select field. 您正在为while()循环使用$row变量名,这意味着来自for()循环的$row原始值将被覆盖,直到到达第二个选择字段时最终为null Changing your while loop to use a different variable name (like $whileRow , for example) would prevent the original $row variable from being overwritten. while循环更改为使用其他变量名(例如$whileRow )可以防止原始的$row变量被覆盖。

Side note: You're using $row again for the second while() loop, which is going to again cause issues. 旁注:您在第二个while()循环中再次使用$row ,这将再次导致问题。 You need to be careful about what variable names you use, especially in nested loops, because it can end up producing some weird bugs (as you've seen) that can be hard to track down. 您需要注意使用什么变量名,尤其是在嵌套循环中,因为它最终会产生一些难以跟踪的怪异错误(如您所见)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM