websurfer 0 Junior Poster in Training

Hello all again...

I have a problem with this "mini-CMS" I am buidling as I'm trying to learn this PHP thing...
Everything seems to be working OK, EXCEPT when I try to UPDATE the images from a specific record, it only updates one of the images, and it always looks like its the second one... even if I re-upload 2 new images.. it only uploads one of them. I must have the upload-imges-section of the UPDATE script in the wrong place, but for the life of me, cant figure it out... tried several positions, but nothing! (I forgot to mention that I am including a record's images' ID's when form is called, so that the "foreach" loop woudl pick this up as it goes thru it, but obviously I aint doing right... maybe i am supposed to call each images ID differently?

All the CMS functions (insert, delete, modify) are all in this one page.

It's funny, cause when I insert a brand new record, the INSERT command works just fine when writing the image's path names...

SEE THE CODE HERE BELOW...

appreciate any feedback! :)

<CODE>


<?php
require_once('definitionsAdmin.php');
mysql_select_db('database_name',$conAdmin) or die('no database found');


?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Blog Admin</title>
</head>


<body>
<table width="478" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="478">


<h1 align="center">Blog Administration</h1>


<p align="center"> <a href="index.php">&lt;&lt; Back to Listings >></a><br />
</p>


<?php


$recordID = $_GET;
$id = $_POST;
$title = $_POST;
$body = $_POST;
$update = $_POST;
$delete = $_POST;
$insert = $_POST;
$insertURL = $_GET;
$fileupdate = $_POST;



if(!isset($recordID))
{
$result = mysql_query("SELECT * from blog ORDER BY blog_id DESC");
}



if (isset($update))
{
$result = mysql_query("UPDATE blog SET blog_title = '$title',blog_body = '$body' WHERE blog_id = '$recordID'");


// this inserts images if any are to be updated
foreach ($_FILES["fileupload"]["error"] as $key => $error)
{
if ($error == UPLOAD_ERR_OK)
{
$tmp_name = $_FILES["fileupload"]["tmp_name"][$key];
$name = $_FILES["fileupload"]["name"][$key];
$random = rand();
move_uploaded_file($tmp_name, "uploads/$random$name");
mysql_query("UPDATE images SET image_path = 'uploads/$random$name' WHERE image_id = '$fileupdate'");
}
}


// END image script


}



if (isset($delete))
{
$result = mysql_query("DELETE FROM blog WHERE blog_id = '$recordID'") or die("CANNOT DELETE RECORD");
}
if (isset($insert))
{ if (($title != "") && ($body != ""))
{
$result = mysql_query("INSERT INTO blog (blog_title,blog_body) VALUES ('$title','$body')");
// calls newly created blog ID, to be used righ back into images table so creates relationship between blogs/images tables thru ID
$newID = mysql_insert_id();



// this inserts images if any are to be updated
foreach ($_FILES["fileupload"]["error"] as $key => $error)
{
if ($error == UPLOAD_ERR_OK)
{
$tmp_name = $_FILES["fileupload"]["tmp_name"][$key];
$name = $_FILES["fileupload"]["name"][$key];
$random = rand();
move_uploaded_file($tmp_name, "uploads/$random$name");
mysql_query("INSERT INTO images (image_path,blog_post_id) VALUES ('uploads/$random$name','$newID')");
}
}


// END image script



echo "<font color='red'>Record Inserted!</font>";
}
else
{
echo "FILL OUT FORM INFO!";
}
}
if (isset($recordID))
{
$result = mysql_query("SELECT * from blog WHERE $recordID = blog_id");
$row = mysql_fetch_array($result);
}



if(!isset($recordID))
{
while($row = mysql_fetch_array($result))
{
echo $row . " – " .  "<a href='?b=" . $row."'>". $row . "<a><br>";
}
}
elseif (isset($recordID))
{


echo"";?><form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
<p>Record ID: <?php echo $row; ?> </p>
<p>Title:<br />
<input name="title" type="text" id="title" value="<?php echo $row; ?>" size="50"/>
</p>
<p>
Body:<br />
<textarea name="body" cols="60" rows="15" id="body"><?php echo $row;  ?></textarea>
<br />
</p>
<table width="88%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="36" colspan="2" align="center"><input type="file" name="fileupload[]" id="fileupload[]" /></td>
<td align="center">&nbsp;</td>
</tr>
<tr>
<td height="30" colspan="2" align="center"><input type="file" name="fileupload[]" id="fileupload[]" /></td>
<td align="center">&nbsp;</td>
</tr>
<tr>
<td height="43" colspan="3" align="center">
<?php
$images = mysql_query("SELECT * FROM images WHERE blog_post_id = '{$row}'");
while($image = mysql_fetch_assoc($images)){
echo "<img height='100' src='" . $image . "' />  ";
echo "<input name='fileupdate' id='fileupdate' type='hidden' value='" . $image . "' />";
}?></td>
</tr>


<tr>
<td width="27%" height="21" align="center">   <?php if (!isset($insertURL)) { echo "<input name='update' type='submit' id='update' value='update' />"; }  ?>   </td>
<td width="41%" align="center"><?php if (isset($insertURL)) { echo "<input name='insert' type='submit' id='insert' value='insert' />"; }  ?></td>
<td width="32%" align="center"><?php if (!isset($insertURL)) { echo "<input name='delete' type='submit' id='delete' value='delete' />"; }  ?></td>
</tr>
</table>
<input type="hidden" name="id" id="id" value=" <?php echo $row; ?>"/>
</form>
<p>
<?php "";}


?>
<br />
</p>
<hr />
<p><a href="index.php?b=&i=">Insert New Record</a><br />
<br />
</p>
</tr>
</table>


<p>


</p>
</body>
</html>


</CODE>