开发者

Identifying each table row using SQl

开发者 https://www.devze.com 2023-01-24 13:04 出处:网络
I have created a table using HTML that retrieves data from my database. At the end of each row, I have manually added a submit button which is inside the while loop as the data is retireved from the d

I have created a table using HTML that retrieves data from my database. At the end of each row, I have manually added a submit button which is inside the while loop as the data is retireved from the database. Once the button is clicked, the session username is inserted into a table in the database. I also want it to enter other data of only the specific row. With what I have right now, no matter which button you click on any row, the data from the first row is only inse开发者_Python百科rted.

Thanks for all your help.

In simpler words, on button click how do I select the data from that row where the button is present and insert it into the db?

mysql_select_db("my_db", $con);

$result = mysql_query("SELECT * FROM Persons");

echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['FirstName'] . "</td>";
  echo "<td>" . $row['LastName'] . "</td>";
echo "<td>"; include "button.php"; echo "</td>";
      echo "</tr>";
      }
    echo "</table>";

Button.php checks for the session_username and adds it to the database. I want it to also insert the "firstname" .


You need to add a name to your submit buttons so you know which one was clicked.


If you don't mind the page being reloaded, a simple way to do it is by having a separate for each line of your table, something like this:

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<form action=""submit.php"">";
  echo "<input type=""hidden"" name=""FirstName"" value=""" . $row['FirstName'] . """>";
  echo "<td>" . $row['FirstName'] . "</td>";
  echo "<td>" . $row['LastName'] . "</td>";
  echo "<td> <input type=""submit"" value=""Save""> </td>";
  echo "</tr>";
  }

Pressing the button will submit the form to submit.php with a POST parameter called FirstName which contains the person's first name, and submit.php can do the database INSERT.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号