Scripter
Scripter

Reputation: 579

Using array after POST

I would like to edit database entries with a simple form. The id and name of each entry is fixed.

Problems:

  1. After submitting the form $farray does only include the edited data of $array[3] not of the other records.
  2. $farray doesn't include name and id (not included in the form). I need the id of the record for the query.

What could I use as a workaround?

Current script

<?php 
$query = mysql_query("SELECT * FROM database");
$array = array();
while($row = mysql_fetch_assoc($query)){ 
  $array[] = $row;
}

if(isset($_POST['send'])) 
{ 
    $farray = $_POST['farray'];

    foreach ($farray as $key => $value) {
        echo('UPDATE database SET ' . $key . ' = "' . $value . '"' . ' WHERE id = ' . $farray['id']);  //testing
    } 
} 
?>

<form action=" <?=$_SERVER['PHP_SELF']?> " method="POST">
<?php
foreach($array as $key1){
    echo $key1["name"] . "<br />";
    foreach($key1 as $key => $value){
        if ($key != "id" AND $key != "name") {
            print $key. 
            ' <input type="text" name="farray['.$key.']" value="'.$value.'"><br /><br />';
        }    
    }
}
?>
<input type="submit" name="send" value="send"> 
</form> 

Example $array

Array
(
    [0] => Array
        (
            [id] => 0
            [name] => name0
            [1] => 1
            [2] => 2
            [3] => 3
        )

    [1] => Array
        (
            [id] => 1
            [name] => name1
            [1] => 1
            [2] => 2
            [3] => 3
        )

    [2] => Array
        (
            [id] => 2
            [name] => name2
            [1] => 1
            [2] => 2
            [3] => 3
        )

    [3] => Array
        (
            [id] => 3
            [name] => name3
            [1] => 1
            [2] => 2
            [3] => 3
        )
)

Example $farray (after editing and submitting form)

Array
(
    [1] => 10
    [2] => 20
    [3] => 30
)

Upvotes: 0

Views: 52

Answers (1)

Brian Gottier
Brian Gottier

Reputation: 4582

OK, even though your table fields are not 1, 2, and 3, you just needed to change up your posted array a little. So I made you a little example:

<form method="post">
<?php
// Simulate DB records
$array = [
  [
    'id'   => 0,
    'name' => 'name0',
    '1'    => 1,
    '2'    => 2,
    '3'    => 3
  ],
  [
    'id'   => 1,
    'name' => 'name1',
    '1'    => 1,
    '2'    => 2,
    '3'    => 3
  ],
  [
    'id'   => 2,
    'name' => 'name2',
    '1'    => 1,
    '2'    => 2,
    '3'    => 3
  ]
];
// Create the input fields:
foreach( $array as $key1 )
{
    echo $key1["name"] . "<br />";

    foreach($key1 as $key => $value)
    {
        if ($key != "id" AND $key != "name") 
        {
            echo $key. 
            ' <input type="text" name="farray[' . $key1['id'] . ']['.$key.']" value="'.$value.'"><br /><br />';
        }    
    }
}
?>
<button type="submit">Submit</button>
</form>

<pre>
<?php 
if( isset( $_POST['farray'] ) )
{
  print_r( $_POST );

  foreach( $_POST['farray'] as $id => $values ) 
  {
    foreach( $values as $k => $v )
    {
      echo('UPDATE database SET ' . $k . ' = "' . $v . '"' . ' WHERE id = ' . $id ) . '<br />';
    }
  }
}
?>
</pre>

I tested this, and I think it works as you would expect.

Upvotes: 1

Related Questions