I've created a simple query of retrieving records from my database and passing it to a 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. But if I have 2 or more, the latest record is always retrieved regardless of which record you selected. 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. 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:
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 }