• 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

    • I noticed this was already happening within my organization; my teams location will change between remote and on-site without me having to do anything. Is it possible this is live already for select customers?
    • I wonder what it will show when I'm plugged in with my ethernet cable at home and not using WiFi.
    • While LibreOffice is not pleased to see a new competitor, they are absolutely correct in stating that Euro-Office using a MS file standard as a default is not being truly "European." Using a MS standard just means Euro-Office is just a "bastardized MS Office Suite." (Wasn't a major purpose of Euro-Office was to get away from being captive and enslaved to MS's Office Suite??)
    • Microsoft continues its long-term policy of spying on their users--despite vehement denials. That feature will be disabled (or removed) either "elegantly" with MS providing a true way to disable it, or "quick and dirty" via a third-party who WILL come up with a way to disable it. Your choice MS...
    • Helium Browser 0.13.3.1 by Razvan Serea Helium is a private, fast, and honest Chromium-based web browser — built for people, with love. It offers the best privacy by default, unbiased ad-blocking, and a clean experience free from bloat and noise. Proudly based on Ungoogled-Chromium, Helium removes Google’s clutter while keeping a fast, efficient development pipeline. With thoughtful touches like native !bangs and split view, Helium is a people-first, fully open-source browser that puts control back in your hands. Privacy, security, and control come first. Ads, trackers, and third-party cookies are blocked automatically, HTTPS is enforced everywhere, and all Chromium extensions work seamlessly — while Google can’t track your activity. Helium’s 13,000+ offline-ready !bangs let you jump straight to sites or AI tools like ChatGPT instantly. Open-source, people-first, and unbiased, Helium delivers a browsing experience that’s fast, secure, and free from noise, ads, and compromises. Helium Browser key features: Performance Fast, efficient, and lightweight — built on Chromium’s optimized engine. Energy-saving and consistent — stays fast over time without slowing down. No bloat — stripped of unnecessary components for maximum speed. Minimalist interface — compact, clean, and distraction-free. Customizable toolbar — hide elements you don’t need. Smooth and stable — no flicker, lag, or animation glitches. Comfort-focused experience — intuitive and unobtrusive. Privacy & Security Best privacy by default — blocks ads, trackers, phishing, and third-party cookies. Unbiased ad-blocking — powered by community filters and uBlock Origin. No telemetry or analytics — zero background web requests on first launch. Strict HTTPS enforcement — warns for insecure sites. Passkeys supported — modern authentication made simple. No built-in password manager or cloud sync — your data stays yours. Extension Compatibility Full Chromium extension support — including MV2 extensions. Anonymized Chrome Web Store requests — Google can’t track extension installs. Extended MV2 support — maintained for as long as possible. Smart Features Native !bangs — browse faster using 13,000+ offline-ready shortcuts. AI integration — use !chatgpt and others directly from the address bar. Offline functionality — bangs work without an Internet connection. Philosophy People-first design — open source, transparent, and community-driven. No ads, no noise, no bias — privacy and honesty over profit. Helium Browser 0.13.3.1 changelog: f53b28d update: helium 0.13.3.1 (#292) b3cbb2ba revision: bump to 3 (#1925) bcacb8c7 chromium: update to 149.0.7827.114 (#1924) Download: Helium 64-bit | Portable 64-bit |~100.0 MB (Open Source) Download: Helium ARM64 | Portable ARM64 Links: Helium Home Page | macOS | Linux | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
  • Recent Achievements

    • Week One Done
      ssd21345 earned a badge
      Week One Done
    • Contributor
      MarkHughes4096 went up a rank
      Contributor
    • Dedicated
      jordanspringer earned a badge
      Dedicated
    • Rookie
      Rimplesnort went up a rank
      Rookie
    • One Year In
      Markus94287 earned a badge
      One Year In
  • Popular Contributors

    1. 1
      +primortal
      507
    2. 2
      +Edouard
      179
    3. 3
      PsYcHoKiLLa
      140
    4. 4
      ATLien_0
      91
    5. 5
      Steven P.
      78
  • Tell a friend

    Love Neowin? Tell a friend!