• 0

Passing $_POST data in a form


Question

Have a db table with:

id | Name | Age | Location

----------------------------------------

1 | jim | 67 | AL

2 | sam | 23 | CA

3

4..... etc

tableView.php that displays a table of all the sql rows. At the end of each table row I have an Edit button. The goal of clicking the button would be it then goes to tableRowEdit.php where it displays only the db row that was clicked on in editable text fields.

tableView.php snippet


<?php
echo "<form name=\"tableView\" method=\"post\" action=\"tableRowEdit.php\">";
echo <table width=\"750\" border=\"0\" cellspacing=\"0\" cellpadding=\"1\">";


$result = mysqli_query($connect, "SELECT * FROM $db order by Age");

while ($row = mysqli_fetch_array($result)) {
echo"<tr align=\"center\">";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['Age'] . "</td>";
echo "<td>" . $row['Location'] . "</td>";
echo "<td><input type='submit' value='Edit' /></td>";
echo "<input type='hidden' name='id' value='" . $row['id'] . "' />";
echo "</tr>";
}

echo "</table>";
echo "</form>";
?>
[/CODE]

tableRowEdit.php snippet

[CODE]
<?
$posted_id = $_POST['id'];

echo $posted_id;

$result = mysqli_query($connect, "SELECT * FROM $db where id='" . $posted_id . "'") or die ("Error in query: $result");
if ($row = mysqli_fetch_array($result)) {

echo "<form name='tableRowEdit' method='post' action='tableUpdate.php'>\n";

echo "<table width=\"750\" border=\"0\" cellspacing=\"0\" cellpadding=\"1\">";
echo "<tr align=\"center\">";
echo "<td><input type=\"text\" name=\"tre_name\" value=\"" . $row['Name'] . "\" /></td>";
echo "<td><input type=\"text\" name=\"tre_age\" value=\"" . $row['Age'] . "\" /></td>";
echo "<td><input type=\"text\" name=\"tre_location\" value=\"" . $row['Location'] . "\" /></td>";
echo "<td><input type='submit' value='Submit' /></td>";
echo "<input type='hidden' name='id' value='" . $row['id'] . "' />";
echo "</tr>";
}

echo "</table>";
echo "</form>";
?>
[/CODE]

]

And from there I'm going to have the tableUpdate.php to send the update query sql commands. Which I haven't started yet due to my problem.

Problem is clicking the edit button is only displaying the last row's data, I am unable to get it to only pass the row that was clicked on. Has to be something simple that my tired eyes are simply not seeing.

Any hints would be appreciated.

Link to comment
https://www.neowin.net/forum/topic/1059642-passing-_post-data-in-a-form/
Share on other sites

5 answers to this question

Recommended Posts

  • 0

From first glance at tableView.php, you're missing a " on line 3, you're using mysqli_fetch_array instead of mysqli_fetch_assoc, and your hidden <input> is improperly placed (not in a <td>)... move it to before your submit button INSIDE the <td>. Also, is it 'id', 'Id', or 'ID'? Because Name, Age and Location all have a capital letter at the start, and MySQL is case-sensitive with column names.

There may be more issues I haven't spotted, but try fixing them things first. I guess the root of the problem would be lack of _assoc, but then again, you don't say you have an issue with Name, Age and Location from displaying.

  • 0

As Alex has said, that little comma in the third line can produce catastrophic results! It's even strange that it doesn't throw you an error.

Also, if that doesn't work, try putting ticks ` ` between the column names:

SELECT * FROM $db order by `Age`

Although not always necessary, this can help you sometimes.

Edit: Inspecting it again, I'm seeing that on the tableView.php you never close the connection to the database, and then you assign it with the same variable another connection.

Try adding at the end of tableview.php

$result = null;

Or

unset($result)

Or

mysql_close($result)

Or

Simply write $result2 in tableRowEdit.php

  • 0

In its present form it's never going to work, the name='id' in

echo "&lt;input type='hidden' name='id' value='" . $row['id'] . "' /&gt;";

is redefined with each row iteration, which will result in always posting the value of the last one encountered in the form, as you're seeing.

You'd be better off losing the form wrapper and using a GET request to point to tableRowEdit.php

&lt;?php
echo "&lt;table width=\"750\" border=\"0\" cellspacing=\"0\" cellpadding=\"1\"&gt;";


$result = mysqli_query($connect, "SELECT * FROM $db order by Age");

while ($row = mysqli_fetch_array($result)) {
		echo"&lt;tr align=\"center\"&gt;";
		echo "&lt;td&gt;" . $row['Name'] . "&lt;/td&gt;";
		echo "&lt;td&gt;" . $row['Age'] . "&lt;/td&gt;";
		echo "&lt;td&gt;" . $row['Location'] . "&lt;/td&gt;";
		echo "&lt;td&gt;&lt;a href='tableRowEdit.php?id=" . $row['id'] . "'&gt;Edit&lt;/a&gt;&lt;/td&gt;";
  echo "&lt;/tr&gt;";
}

echo "&lt;/table&gt;";
?&gt;

and change tableRowEdit to fetch the id from $_GET instead

$posted_id = $_GET['id'];

  • 0

Much appreciated to all who replied. Yeah I must have accidently removed that " when posting as its definitly there and as noted it wouldnt have worked without it. Odd. Got it to pass the correct vars and then I was able to make the update sql script after. All works great.

Thanks again!

This topic is now closed to further replies.
  • Posts

    • You are clueless. The updates are done in the background so the next time you open Edge the updates are applied automatically. There is no need to close all your tabs. Just keep browsing like you normally do. Clearly you don't use Edge and are just one of those haters that complain for the sake of complaining.
    • I don't get this David. Can you explain it please.  
    • Microsoft is busy. Lots of changes to be released imminently for Windows server or soon. Also, lots happening for next version as well. Third party virus scanning software is being moved out of Kernel mode to avoid repeat of Crowdstrike incident. Windows Protected Mode and Windows Ready Print no longer require third party print drivers to be installed. New storage stack being developed. New NVME drivers now available for Windows Server 2025 to improve local NVME drive performance by 60+ percent. NVME-Of of fabric being worked on for next release to improve network access to NVME drives. ReFs (next file system) now has ability to boot and will become default file system in next release of Windows Server. ReFs improves on NTFS in several areas including resiliency and reliability and scalability. New update stack is being worked on to unify Windows updates, and updates for drivers and first party/3rd party application software. A stricter and more robust third-party driver certification program (ODI) is being worked on to improve performance, thermals, battery life, and reliability on modern Windows hardware by tightening how OEMs and IHVs (Intel, AMD, Qualcomm, NVIDIA, etc.) build and ship drivers. There is a tone more but too numerous to mention.
    • Now disable that stupid OneDrive backup request when Windows starts please. So unbelievably frustrating to only have “remind me later” instead of “no and never ask me again”
    • Hello, The Media Creation Tool is still at v10.0.26100.7019 from October 2025. Just looks like the backend has been updated. Regards, Aryeh Goretsky
  • Recent Achievements

    • One Month Later
      Markus94287 earned a badge
      One Month Later
    • Week One Done
      Markus94287 earned a badge
      Week One Done
    • One Year In
      Markus94287 earned a badge
      One Year In
    • Dedicated
      truespursfan earned a badge
      Dedicated
    • Rookie
      restore went up a rank
      Rookie
  • Popular Contributors

    1. 1
      +primortal
      507
    2. 2
      +Edouard
      168
    3. 3
      PsYcHoKiLLa
      154
    4. 4
      ATLien_0
      90
    5. 5
      Steven P.
      79
  • Tell a friend

    Love Neowin? Tell a friend!