我有一个PHP代码,可以在html表中添加/删除新行(tr)。一列(td)具有文本字段,第二列(td)具有下拉列表。下拉源是 mysql表。 第一行(tr)值正确。但是当我添加新行(tr)时,dropdonwn list为null
代码是:
<html>
<head>
<title></title>
<style type="text/css">
input {
display: block; /* makes one <input> per line */
}
</style>
</head>
<?php
include 'dbconnection\dbconnection.php'; // Database connection
$count=1;
if(isset($_POST['btnadd']) == "ADD NEW ITEM") {
// add 1 to the row counter
if (isset($_POST['count'])) $count = $_POST['count'] + 1;
// set value for first load
else $count = 1;
}
if(isset($_POST['btnremove']) == "DELTE ITEM") {
// decrement the row counter
$count = $_POST['count'] - 1;
// set minimum row number
if ($count < 1) $count = 1;
}
?>
<body>
<!-- call same file on submit -->
<form name="form1" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php
$cdquery="select cmc_id,cmc_name from cat_main_category";
$result2=mysqli_query($conn,$cdquery) or die(mysqli_error());
echo '<table>';
// print $count rows
for ($i=1; $i<=$count; $i++) {
echo ' <tr> <td> <input type="text" name="text'.$i.'" align="center"> </td>';
echo '<td> <select id="basicgroup'.$i.'" name="basicgroup'.$i.'" onchange=reload(this.form)>';
echo '<option value=0>Select</option>';
while ($row=mysqli_fetch_array($result2)) {
$cdTitle=$row["cmc_name"];
$id=$row["cmc_id"];
//if($id==@$basicgroup){
echo "<option selected value=$id>
$cdTitle
</option>";
/*}
else{echo "<option value=$id>
$cdTitle
</option>""."<BR>";;*/
}
echo '</select></td></tr> ';
}
?>
</table>
<!-- you only need one set of buttons and only one counter for this script
because every button would do the same -->
<input type="submit" name="btnadd" id="btnadd" value="ADD NEW ITEM" >
<input type="submit" name="btnremove" id="btnremove" value="DELETE ITEM">
<input type="hidden" name="count" value="<?php echo $count; ?>">
</form>
</body>
</html>
答案 0 :(得分:1)
执行MySQL查询后: $ result2 = mysqli_query($ conn,$ cdquery)或die(mysqli_error());
将结果保存在数组中,如:
$categories = [];
while ($row=mysqli_fetch_array($result2)) {
$categories[$row["cmc_id"]] = $row["cmc_name"]
}
然后在你的主循环中:
for ($i=1; $i<=$count; $i++) {
echo ' <tr> <td> <input type="text" name="text'.$i.'" align="center"> </td>';
echo '<td> <select id="basicgroup'.$i.'" name="basicgroup'.$i.'" onchange=reload(this.form)>';
echo '<option value=0>Select</option>';
foreach ($categories as $id => $cdTitle) {
echo "<option selected value=$id>
$cdTitle
</option>";
}
echo '</select></td></tr>';
}
您当前的问题是第一次迭代,您使用mysqli_fetch_array
耗尽了MySQL查询,第二次选择为空
答案 1 :(得分:0)
更改
body
要
<?php
$cdquery="select cmc_id,cmc_name from cat_main_category";
$result2=mysqli_query($conn,$cdquery) or die(mysqli_error());
echo '<table>';
for ($i=1; $i<=$count; $i++) {
echo '<tr><td><input type="text" name="text'.$i.'" align="center"></td>';
echo '<td><select id="basicgroup'.$i.'" name="basicgroup'.$i.'" onchange=reload(this.form)>';
echo '<option value=0>Select</option>';
while ($row=mysqli_fetch_array($result2)) {
$cdTitle=$row["cmc_name"];
$id=$row["cmc_id"];
echo "<option selected value=$id>$cdTitle</option>";
}
echo '</select></td></tr>';
}
?>
</table>
Put,
<?php
echo '<table>';
for ($i=1; $i<=$count; $i++) {
$cdquery="select cmc_id,cmc_name from cat_main_category";
$result2=mysqli_query($conn,$cdquery) or die(mysqli_error());
echo '<tr><td><input type="text" name="text'.$i.'" align="center"></td>';
echo '<td><select id="basicgroup'.$i.'" name="basicgroup'.$i.'" onchange=reload(this.form)>';
echo '<option value=0>Select</option>';
while ($row=mysqli_fetch_array($result2)) {
$cdTitle=$row["cmc_name"];
$id=$row["cmc_id"];
echo "<option selected value=$id>$cdTitle</option>";
}
echo '</select></td></tr>';
}
?>
</table>
在$cdquery="select cmc_id,cmc_name from cat_main_category";
$result2=mysqli_query($conn,$cdquery) or die(mysqli_error());
圈内。