• 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

    • Audacious 4.6.1 by Razvan Serea Audacious is a lightweight, open-source audio player that emphasizes simplicity, performance, and sound quality. Designed for Linux, Windows, and macOS, it supports a wide range of audio formats, internet radio streaming, and playlist management. Users can customize the interface with Winamp-style skins or modern themes, making it flexible for different preferences. Audacious also includes an equalizer, advanced audio effects, and a plugin system for extending functionality. Its low resource usage makes it especially suitable for older computers or users who value efficiency without sacrificing playback quality. Audacious key features: High audio quality – delivers clean, gapless playback with minimal distortion. Wide format support – plays MP3, FLAC, Ogg Vorbis, AAC, WAV, WMA, and more. Internet radio streaming – supports Shoutcast, Icecast, and other online streams. Winamp skin support – classic, nostalgic look for users who prefer the old-school style. Modern GTK-based interface – clean, simple UI with a more modern feel. Customizable themes – change appearance through skins and themes. Advanced playlist management – organize, save, and edit playlists with ease. Equalizer – fine-tune audio output with a built-in graphical equalizer. Audio effects – built-in DSP options like crossfade, replay gain, and more. Plugin system – extend functionality with additional components. File metadata support – displays and organizes music based on tags. Drag-and-drop support – quickly add songs or playlists. Global hotkey support – control playback without switching windows. Bit-perfect output modes – bypass system mixers for pure audio output. ReplayGain support – normalizes track loudness automatically. Cue sheet support – play entire albums from a single audio file with .cue. MPRIS2 integration – integrates with Linux desktop environments for media controls. Advanced resampling options – adjust playback quality with different resampler settings. Gapless playback – seamless transition between tracks encoded properly. Crossfade plugin – blend one song into the next smoothly. Last.fm scrobbling plugin – track listening history online. Remote control support – control Audacious via command-line or scripts. Lyrics plugin – display song lyrics if available. Alarm / timer plugin – start or stop playback at set times. SOX resampler plugin – high-quality resampling for audiophiles. Spectrum analyzer / visualization plugins – visual feedback while playing music. Headphone crossfeed effect – simulates speaker listening for headphones. Customizable buffer size – tweak latency and playback smoothness. Audacious 4.6.1 changelog: Use XDG cache dir to store temporary files (#1817) Accept embedded lyrics in more cases (#1818) Bump .so and plugin ABI versions retrospectively (#1819) Include Georgian translation (#1820) Fix build on systems using musl instead of glibc (#1823) Download: Audacious 4.6.1 | 48.2 MB (Open Source) Download: Portable Audacious 4.6.1 | 69.8 MB View: Audacious Website | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • I really wonder if this has to do with the built in VPN or "private DNS" of browsers that trip up legal requirements like cookie consent and Cloudflare (to avoid all the botnet attacks we get). And BTW some botnets still manage to get past Cloudflare, we are constantly having to tweak it to block malicious traffic that ultimately cause a DDoS.
    • CPPC states can also be messed around with in most UEFI settings but aren't as robust as the ones that the Windows Scheduler can provide! Make sure you look into what your motherboard also has before customizing for the Windows Scheduler.
  • Recent Achievements

    • Week One Done
      rolfus earned a badge
      Week One Done
    • One Month Later
      Leroy Jethro Gibbs earned a badge
      One Month Later
    • Conversation Starter
      flexorcist earned a badge
      Conversation Starter
    • One Month Later
      AndreaB earned a badge
      One Month Later
    • One Month Later
      agatameier earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      518
    2. 2
      +Edouard
      199
    3. 3
      PsYcHoKiLLa
      147
    4. 4
      ATLien_0
      93
    5. 5
      Steven P.
      79
  • Tell a friend

    Love Neowin? Tell a friend!