这是某个网站的管理面板的代码。客户询问的约会将显示在此页面中。管理员可以根据可用性change
约会。
<?php
while($row = mysqli_fetch_assoc($result)){
?>
<form action="adminEdit.php" method="POST">
<tr>
<td><input type="text" id="name" value="<?php echo $row['Name'];?>"></input></td>
<td><input type="text" id="address" value="<?php echo $row['Address'];?>"></input></td>
<td><input type="text" id="phone" value="<?php echo $row['Phone'];?>"></input></td>
<td><input type="text" id="license" value="<?php echo $row['Car_License_No'];?>"></input></td>
<td><input type="text" id="engine" value="<?php echo $row['Car_Engine_No'];?>"></input></td>
<td><input type="text" id="date" value="<?php echo $row['Date'];?>"></input></td>
<td><input type="text" id="mechanic" value="<?php echo $row['Mechanic'];?>"></input></td>
<td><input type="submit" name="submit" value="Change"/></td>
</tr>
</form>
<?php
}
?>
这里,每行数据都有一个相应的按钮,用于更改或修改该特定行的记录。管理员更改特定约会后,应在数据库中更新。
我的问题是,所有行都是由while循环生成的。现在,如果单击该特定行的change
按钮,如何访问特定行?当循环生成行时,我赢了t be able to access them by
名称or
id because all of them will have the same
名称and
id`。
搜索了相关问题,但没有一个与我的情景相符。得到一个答案将是非常有帮助的。提前致谢。
答案 0 :(得分:0)
就个人而言,我倾向于将输出放在循环中以更好地控制数据并解决问题。您还在每个循环上创建一个新表单 只需循环数据库并创建一个新变量,然后使用该变量输出表单中的数据。
示例代码,用于显示基本概念,未经过测试或说明已完成等等:
while ($row = mysqli_fetch_assoc($result)) {
$someNewVar[$row['id']] = $row;
// The 'id' index would be from DB which identifies individual rows
}
?>
<form action="adminEdit.php" method="POST">
<?php
foreach ($someNewVar as $index => $value) {
?>
<tr>
<td>
<input
type="text"
id="<?php echo $index;?>"
value="<?php echo $value['Name'];?>">
</input>
</td>
<td>
<input
type="submit"
name="submit"
value="Change"/>
</td>
</tr>
<?php
}
?>
</form>
然后,您需要通过单击提交按钮传递行ID。
另一方面,整个方法可以整理,数据应该在一个文件中获得,与您输出的位置分开。
然后在获取数据的文件中,您可以将数组设置为输出文件中标识的内容,以便在未获取数据时进行管理。
即
的访问getdata.php 强>
while ($row = mysqli_fetch_assoc($result)) {
$dataFromDb[$row['id']] = $row;
}
$someNewVar = !empty($dataFromDb) ? $dataFromDb : false;
showData.php
if ($someNewVar) {
// do the loop and form
} else {
echo 'sorry no data found';
}