I would like to edit database entries with a simple form. The id and name of each entry is fixed.
Problems:
- After submitting the form $farray does only include the edited data of $array[3] not of the other records.
- $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
)