Reputation: 379
I'm trying to create a simple PHP page that retrieves and shows the information from the DB, so I do this, which works fine:
echo "<form action='#' method='post'>";
if(!empty($result)){
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "\t<tr>\n";
echo "<td><input type = 'hidden' name = 'value_id' id = 'value_id' value = '" . $row['id'] . "'/></td>";
echo "<td><input type = 'text' name = 'name_value' id = 'name_value' value = '" . $row['name'] . "'/></td>\n";
echo "<td><input type = 'text' name = 'addr_value' id = 'addr_value' value = '" . $row['addr'] . "'/></td>\n";
echo "<td><INPUT type='submit' name = 'modifyItem' value ='Modify'></td>\n";
echo "<td><INPUT type='submit' name = 'deleteItem' value ='Delete'></td>\n";
echo "\t</tr>\n";
}
}
echo "</form>";
Every row has a delete button, which will allow the user to delete each row individually, so I see if the delete button has been clicked and I proceed to remove the row by getting the ID:
if(isset($_POST['deleteItem'])){
$querydelete = "DELETE FROM address WHERE ID = " . $_POST["value_id"];
mysql_query($querydelete) or die('Query failed: ' . mysql_error());
}
The problem is that the ID value is always the same after after the post (which is the ID value of the last row). If I show the ID beforehand for each row, it's the correct one, so the problem must be the value it's getting in the post. What am I doing wrong?
Upvotes: 0
Views: 2595
Reputation: 57
To solve this you need to wrap each into separate form...
while(rows_available){
echo "<form action='#' method='post'>"
echo "<td><input type = 'hidden' name = 'value_id' id = 'value_id' value = '" . $row['id'] . "'/></td>";
echo "<td><input type = 'text' name = 'name_value' id = 'name_value' value = '" . $row['name'] . "'/></td>\n";
echo "<td><input type = 'text' name = 'addr_value' id = 'addr_value' value = '" . $row['addr'] . "'/></td>\n";
echo "<td><INPUT type='submit' name = 'modifyItem' value ='Modify'></td>\n";
echo "<td><INPUT type='submit' name = 'deleteItem' value ='Delete'></td>\n";
echo "\t</tr>\n";
echo "</form>";
}//end of while loop
Upvotes: 1
Reputation: 27092
You have one form with more name=value_id
elements. When you submit the form, in $_POST
is last one (there is the first one, overwrite by second one, etc. to last one).
Use for each row new form, or use array in your names, eg. name="value[]"
.
Upvotes: 1
Reputation: 1067
Change your code like this if you want to place all controls in one form.
echo "<form action='#' method='post'>";
if(!empty($result)){
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "\t<tr>\n";
echo "<td><input type = 'hidden' name = 'field[" . $row['id'] . "][value_id]' id = 'value_id-" . $row['id'] . "' value = '" . $row['id'] . "'/></td>";
echo "<td><input type = 'text' name = 'field[" . $row['id'] . "][name_value]' id = 'name_value-" . $row['id'] . "' value = '" . $row['name'] . "'/></td>\n";
echo "<td><input type = 'text' name = 'field[" . $row['id'] . "][addr_value]' id = 'addr_value-" . $row['id'] . "' value = '" . $row['addr'] . "'/></td>\n";
echo "<td><INPUT type='submit' name = 'field[" . $row['id'] . "][modifyItem]' value ='Modify'></td>\n";
echo "<td><INPUT type='submit' name = 'field[" . $row['id'] . "][deleteItem]' value ='Delete'></td>\n";
echo "\t</tr>\n";
}
}
echo "</form>";
Upvotes: 1
Reputation: 1321
I have already do the same with JS and PHP :
HTML : [index]
echo "<td> <button name=\"$row['id']\" type=\"clear\" >Clear</button> </td>";
JavaScript : [index]
<script>
$(document).ready(function(){
$('[type="clear"]').on('click', function(){
document.location.href = "./php/yourScript.php?param=" + $(this).attr("name");
})
});
</script>
PHP : [./php/yourScript.php]
<?php
$querydelete = "DELETE FROM address WHERE ID = " . $_GET["param"];
mysql_query($querydelete)
?>
I hope, that can help you :)
Upvotes: 1
Reputation: 1871
This is because you are creating many fields with the same name in your form. You can create separate form for every record:
if(!empty($result)){
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "<form action='#' method='post'>"
echo "\t<tr>\n";
echo "<td><input type = 'hidden' name = 'value_id' id = 'value_id' value = '" . $row['id'] . "'/></td>";
echo "<td><input type = 'text' name = 'name_value' id = 'name_value' value = '" . $row['name'] . "'/></td>\n";
echo "<td><input type = 'text' name = 'addr_value' id = 'addr_value' value = '" . $row['addr'] . "'/></td>\n";
echo "<td><INPUT type='submit' name = 'modifyItem' value ='Modify'></td>\n";
echo "<td><INPUT type='submit' name = 'deleteItem' value ='Delete'></td>\n";
echo "\t</tr>\n";
echo "</form>";
}
}
Upvotes: 3
Reputation: 2141
try:
if(!empty($result)){
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "\t<tr>\n";
echo "<form action='#' method='post'><td><input type = 'hidden' name = 'value_id' id = 'value_id' value = '" . $row['id'] . "'/></td>";
echo "<td><input type = 'text' name = 'name_value' id = 'name_value' value = '" . $row['name'] . "'/></td>\n";
echo "<td><input type = 'text' name = 'addr_value' id = 'addr_value' value = '" . $row['addr'] . "'/></td>\n";
echo "<td><INPUT type='submit' name = 'modifyItem' value ='Modify'></td>\n";
echo "<td><INPUT type='submit' name = 'deleteItem' value ='Delete'></td>\n";
echo "\t</form></tr>\n";
}
}
Upvotes: 1
Reputation: 360702
You're using the SAME field names for all of your field sets. Since you're not using the PHP-specific []
array extension/hint on the names, PHP will only process the LAST field name set submitted.
e.g.
<input type="text" name="foo" value="a" />
<input type="text" name="foo" value="b" />
<input type="text" name="foo" value="c" />
var_dump($_POST);
Array(1) {
'foo' => 'c'
}
<input type="text" name="foo[]" value="a" /> <--note the [] here
<input type="text" name="foo[]" value="b" /> <--note the [] here
<input type="text" name="foo[]" value="c" /> <--note the [] here
var_dump($_POST);
Array(1) {
'foo' => array(
0 => 'a'
1 => 'b'
3 => 'c'
)
}
Upvotes: 1
Reputation: 4715
You have multiple inputs with the name value_id
. Only the last occurence of that is transmitted.
What you want is to split into multiple forms. One for each entry. But this would not produce valid html code in your example. There are various ways around that, but this is another question.
Upvotes: 1