• 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

    • BleachBit 6.0.1 Beta by Razvan Serea When your computer is getting full, BleachBit quickly frees disk space. When your information is only your business, BleachBit guards your privacy. With BleachBit you can free cache, delete cookies, clear Internet history, shred temporary files, delete logs, and discard junk you didn't know was there. Designed for Linux and Windows systems, it wipes clean thousands of applications including Firefox, Microsoft Edge, Google Chrome, Opera, Safari, and more. Beyond simply deleting files, BleachBit includes advanced features such as shredding files to prevent recovery, wiping free disk space to hide traces of files deleted by other applications, and vacuuming Firefox to make it faster. Better than free, BleachBit is open source. BleachBit has many useful features: Delete your private files so completely that "even God can't read them" according to South Carolina Representative Trey Gowdy. Simple operation: read the descriptions, check the boxes you want, click preview, and click delete. Multi-platform: Linux and Windows Free of charge and no money trail Free to share, learn, and modify (open source) No adware, spyware, malware, browser toolbars, or "value-added software" Translated to 64 languages besides American English Shred files to hide their contents and prevent data recovery Shred any file (such as a spreadsheet on your desktop) Overwrite free disk space to hide previously deleted files Portable app for Windows: run without installation Command line interface for scripting and automation CleanerML allows anyone to write a new cleaner using XML Automatically import and update winapp2.ini cleaner files (a separate download) giving Windows users access to 2500+ additional cleaners Frequent software updates with new features Going beyond standard deletion of files, BleachBit has several advanced cleaners: Clear the memory and swap on Linux Delete broken shortcuts on Linux Delete the Firefox URL history without deleting the whole file—with optional shredding Delete Linux localizations: delete languages you don't use. More powerful than localepurge and available on more Linux distributions. Clean APT for Debian, Ubuntu, Kubuntu, Xubuntu, and Linux Mint Find widely-scattered junk such as Thumbs.db and .DS_Store files. Execute yum clean for CentOS, Fedora, and Red Hat to remove cached package data Delete Windows registry keys—often where MRU (most recently used) lists are stored Delete the OpenOffice.org recent documents list without deleting the whole Common.xcu file Overwrite free disk space to hide previously files Vacuum Firefox, Google Chrome, Liferea, Thunderbird, and Yum databases: shrink files without removing data to save space and improve speed Surgically remove private information from .ini and JSON configuration files and SQLite3 databases without deleting the whole file Overwrite data in SQLite3 before deleting it to prevent recovery (optional) BleachBit 6.0.1 Beta release notes: BleachBit 6.0.1 beta is now available for testing. This maintenance-focused release includes bug fixes, updated translations, and a range of safe enhancements. This release fixes a Windows security issue that could allow arbitrary file deletion during privileged cleaning (reported by Zeze with TeamT5). It also adds new cleaners (including a DNS cache cleaner, Claude Code, and Visual Studio Code forks), support for multiple Chrome and Edge profiles, new deep scan options for developer directories like node_modules and venv, and safer, faster file shredding. All Platforms Added cleaners for Claude Code, DNS cache, and many Visual Studio Code forks. Added support for multiple Chrome and Edge profiles. Chrome can now clean downloaded AI models. Deep Scan can optionally remove venv, __pycache__, node_modules, and .angular directories. Deep Scan is faster by skipping directories on the keep list. File shredding is safer, faster, and leaves fewer recoverable traces. Improved handling of cookies, symlinks, Unicode filenames, external processes, and configuration files. Improved Expert Mode warnings and long warning dialogs. Fixed crashes related to cleaner detection, invalid Unicode, and malformed cleaner data. Clipboard is now cleared automatically after shredding files via paste operations. Linux Added AppImage support. Added cleaners for Visual Studio Code, Codeium, Librewolf (.deb), Transmission (Flatpak), and Profanity. Improved Linux trash detection, including Snap-installed applications and mounted drives. Fixed Wayland root CLI issues and several Snap-related problems. Improved package dependencies, AppStream metadata, and desktop file handling. Fixed startup crashes when Python Requests is unavailable. Windows Fixed a security vulnerability that could allow arbitrary file deletion when cleaning with elevated privileges. Added %WindowsSystem% variable support. Improved clipboard clearing using native Windows APIs. Improved installer experience on unsupported Windows versions. Reduced installer size and improved application robustness. Fixed Unicode handling, filename anonymization, Git revision reporting, and splash screen stability. [full release notes] Download: BleachBit 6.0 | Portable | ~20.0 MB (Open Source) View: BleachBit Home page | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • DriversCloud 12.1.6 by Razvan Serea With DriversCloud (formerly My-Config.com), you can explore your computer easily, safely and free. The application quickly scans your PC and identifies the hardware and software components. DriversCloud then establishes a list of the different drivers compatible with your OS and hardware. Download the drivers needed for the proper functioning of your computer. To detect your drivers, DriversCloud also displays a detailed summary of your hardware and software configuration, analyzes your BSOD, monitors in real-time your PC voltages and temperatures and lets you share your configuration online. Once the hardware components have been detected, you will be able to obtain with just a few clicks the latest drivers corresponding to the identified hardware. You can record your configuration on the site for free, and can get the corresponding URL to post the configuration to technical forums, e-mail and social networks. You can also download the detection result (the configuration) as a PDF file. To protect the user's privacy and data confidentiality, a 4-level confidentiality system was created that filters the XML marks and gives control to the user. The default level can be modified in the preferences. Using the maximum level will prevent the user from publishing his configuration and generating a corresponding PDF file. In non-connected mode, each XML configuration is stored on the server for one day (for practical reasons). However, you are given the opportunity to manually delete it. Created in 2004, and continually improved, My-Config.com has established itself on the web as a free service to PC users running Windows and Linux operating systems. The service is designed to work with the most common Internet browsers (Edge, Firefox, Chrome, Safari). Download: DriversCloud 64-bit | 20.0 MB (Freeware) Download: DriversCloud 32-bit | 18.9 MB Link: DriversCloud Home Page | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
  • Recent Achievements

    • Week One Done
      agatameier earned a badge
      Week One Done
    • One Month Later
      agatameier earned a badge
      One Month Later
    • Week One Done
      ssd21345 earned a badge
      Week One Done
    • Contributor
      MarkHughes4096 went up a rank
      Contributor
    • Dedicated
      jordanspringer earned a badge
      Dedicated
  • Popular Contributors

    1. 1
      +primortal
      516
    2. 2
      +Edouard
      189
    3. 3
      PsYcHoKiLLa
      148
    4. 4
      ATLien_0
      96
    5. 5
      Steven P.
      76
  • Tell a friend

    Love Neowin? Tell a friend!