I have got myself in a bit of a mess creating a dynamic form and not knowing how to get at the resulting data. I am creating a dynamic form from a mysql database, in order to allow the user to edit and re-store their data.
Database is quite simple:
id (INT)
project_name (the name of the project)
type (0 = project heading, 1=project history)
description (text to describe wither the project (where type=0) or a history entry where Type=1)
project_number (unique number for project)
display (0=do not display, 1=display)
img_path (file system path where image is stored)
First, I get the user to select the project they want to edit, then I run simple DB queries to get the data and present to the user like this (this part is working OK)
$project_number=$_POST['select_project'];
echo "<form name=edit_project_details2 id=add_project_details2 enctype=multipart/form-data method=post action=edit_project_details2_process.php onsubmit=return formCheck(this);>";
$query="SELECT id,name,description FROM g_projects WHERE project_number= '$project_number' AND type='0'";
$result=mysql_query($query);
$row=mysql_fetch_array($result);
$project_id=$row['id'];
$project_name=$row['name'];
$project_description=$row['description'];
echo "<p class=large>Projects - Edit Descriptions</p>";
echo "<h1>Amend the following details and click SAVE CHANGES when finished</h1>";
echo "<p> </p>";
echo "<p class=large>Project Name/Description</p>";
echo "<p>Project Name: <input type=text name=project_name value=".$project_name." /><input type=hidden name=project_id value=".$project_id." /></p>";
echo "<p>Project Description: <textarea name=project_description cols=50 rows=5>".$project_description."</textarea>";
echo "<p> </p>";
echo "<p class=large>Description of Photo(s)</p>";
echo "<table width=800px aligh=left>";
$query1="SELECT id,description,img_path FROM g_projects WHERE project_number= '$project_number' AND type='1'";
$result1=mysql_query($query1);
$num_rows=mysql_num_rows($result);
if($num_rows<1)
{
echo "<p class=red>The are not any photo description in this project to edit</p>";
}
else
{
$num=1;
while($row1=mysql_fetch_array($result1))
{
$image_id=$row1['id'];
$image_description=$row1['description'];
$description_name=$num++;
$image_path=$row1['img_path'];
echo "<tr>
<td><img src=".$image_path." width=100px height=50px /></td>
<td>Photo Description: </td>
<td><textarea name=".$description_name." cols=50 rows=5>".$image_description."</textarea><input type=hidden name=".$image_id." value=".$image_id." /></td>
</tr>";
}
echo "</table>";
}echo "<input type=submit name=submit value='Save Changes' />";
echo "</form>";
But it is at this point where my trouble starts
Once the user posts their changes, I an trying to process the changes with a simple database UPDATE query, however, I cannot pre-determine the posted field names or even their values to create variables for my DB query.
For example
I have tried using the foreach loop
foreach ( $_POST as $name => $value){something}
to define the image_id and description_name so I can perform my UPDATE update query like; UPDATE description where id equals image_id.
But because I have had to create the $_POST field names on the fly, I cannot determine the field names once they are posted.
Also
foreach ( $_POST['field name'] as $name => $value){something}
reports invalid argument error.
Is there another PHP function to help with this or is there something I'm missing with the foreach function? NOTE: I've tried to add known variables to the field name on creation, like name=$variable.'XXX', so I can say if $name contains, etc, etc, but I would see this as a work around, not a solution.
Can anyone point me in the right direction?