Scheduled PSN downtime tomorrow in Back Page News

Passing $_POST data in a form


5 replies to this topic - - - - -

#1 vas

    A PC Gamer

  • 151 posts
  • Joined: 25-March 02

Posted 23 February 2012 - 00:53

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>";
?>



tableRowEdit.php snippet
<?
$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>";
?>
]

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.


#2 -Alex-

    Noob Hunter

  • 2,551 posts
  • Joined: 08-August 06
  • Location: Liverpool / Amsterdam

Posted 23 February 2012 - 04:13

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.

#3 AnthonySterling

    Offering bad advice since 23-December 04.

  • 883 posts
  • Joined: 23-December 04
  • Location: North-East, UK

Posted 23 February 2012 - 10:57

View Post-Alex-, on 23 February 2012 - 04:13, said:

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.

_array returns a numerically, and associative, indexed array. :)

#4 Jose_49

    Resident Elite

  • 1,784 posts
  • Joined: 30-July 09

Posted 23 February 2012 - 11:20

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

#5 CrispCreations

    Neowinian³

  • 456 posts
  • Joined: 19-April 04
  • Location: North West, UK

Posted 23 February 2012 - 14:07

In its present form it's never going to work, the name='id' in
echo "<input type='hidden' name='id' value='" . $row['id'] . "' />";


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

<?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><a href='tableRowEdit.php?id=" . $row['id'] . "'>Edit</a></td>";
  echo "</tr>";
}

echo "</table>";
?>

and change tableRowEdit to fetch the id from $_GET instead

$posted_id = $_GET['id'];


#6 vas

    A PC Gamer

  • 151 posts
  • Joined: 25-March 02

Posted 24 February 2012 - 00:27

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!