0

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?

Abhineet Verma
  • 1,008
  • 8
  • 18
Ankoku
  • 339
  • 1
  • 3
  • 7
  • 3
    [Please, don't use `mysql_*` functions in new code](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [red box](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). **You are also wide open to [SQL injections](http://stackoverflow.com/q/60174)** – John Conde Jun 23 '14 at 14:47
  • 1
    It's because you're including all your `input` nodes within one `form` node, so the last `input` in the DOM with the same `name` value will take precedence. You could use new `form` nodes for each or a bit of Javascript trickery to get your desired output. – esqew Jun 23 '14 at 14:51

8 Answers8

3

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>";
    }
}
Liberat0r
  • 1,852
  • 2
  • 16
  • 21
1

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.

colburton
  • 4,685
  • 2
  • 26
  • 39
1

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'
   )
}
Marc B
  • 356,200
  • 43
  • 426
  • 500
1

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";
    }
}
Stackia
  • 2,110
  • 17
  • 23
1

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[]".

pavel
  • 26,538
  • 10
  • 45
  • 61
1

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 :)

StrawHara
  • 1,301
  • 19
  • 33
1

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>";
Syam Mohan M P
  • 1,047
  • 8
  • 23
1

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