[英]Adding edit button to each row of table to update database in PHP
I've created a simple query of retrieving records from my database and passing it to a html. 我创建了一个简单的查询,可以从数据库中检索记录并将其传递给html。 I want to add an edit/view button for each row so after some research, I ended up with this:
我想为每一行添加一个“编辑/查看”按钮,因此在进行了一些研究之后,我得出以下结论:
$query = mysqli_query($con, "SELECT * FROM mytable") or die(mysqli_error($con));
if(mysqli_num_rows($query) > 0) {
while($row = mysqli_fetch_array($query)) {
echo "<tr><td>".$row['pId']."</td>";
echo "<td>".$row['data1']."</td>";
echo "<td>".$row['data2']."</td>";
echo "<td><form action='detailform.php' method='POST'><input type='hidden' name='tempId' value='".$row["pId"]."'/><input type='submit' name='submit-btn' value='View/Update Details' /><form></td>";
}
}
This works fine for 1 record. 这可以正常工作1条记录。 But if I have 2 or more, the latest record is always retrieved regardless of which record you selected.
但是,如果我有2个或更多,则无论选择哪个记录,总是会检索到最新记录。 For example, if you have 5 records and you select any record, the 5th record will always be selected so I am unable to update the previous records.
例如,如果您有5条记录,并且选择了任何一条记录,那么将始终选择第5条记录,因此我无法更新以前的记录。 Why is this is happening?
为什么会这样呢? Am I missing something?
我想念什么吗?
Not sure if this helps my case but here's my the basic logic of my detailform.php: 不知道这是否对我有帮助,但这是我的detailform.php的基本逻辑:
if(isset($_POST["tempId"]){
//pass data using post then update. Here's where I keep getting only the latest record regardless of selected record from previous page
} else { //add data }
Close the form: 关闭表格:
echo "<td><form action='detailform.php' method='POST'><input type='hidden' name='tempId' value='".$row["pId"]."'/><input type='submit' name='submit-btn' value='View/Update Details' /></form></td></tr>";
The first one is sent correctly because it is the closest to the submit button, the rest will be closer to the last submit button 第一个正确发送,因为它最接近“提交”按钮,其余则更接近最后一个“提交”按钮
try this 尝试这个
while($row = mysql_fetch_assoc($query)) {
echo "<tr><td>".$row['pId']."</td>";
echo "<td>".$row['data1']."</td>";
echo "<td>".$row['data2']."</td>";
echo "<td><form action='detailform.php' method='POST'><input type='hidden' name='tempId' value='".$row["pId"]."'/><input type='submit' name='submit-btn' value='View/Update Details' /></form></td></tr>";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.