Jump to content

I have a problem with this "mini-CMS", BUT only 1 image UPDATES, not BOTH ima...


mac007

Recommended Posts

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 am including a record's images' ID's in a hidden field when form is called for an UPDATE, so that the "foreach" loop woudl pick this up as it goes thru it, but obviously I aint doing right... since it's just updating one image, maybe i am supposed to call each images ID differently? or my msql query is not right?

 

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

 

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

 

 

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"><< Back to Listings >></a><br />

</p>

 

<?php

 

 

 

 

 

$recordID = $_GET['b'];

$id = $_POST['id'];

$title = $_POST['title'];

$body = $_POST['body'];

$update = $_POST['update'];

$delete = $_POST['delete'];

$insert = $_POST['insert'];

$insertURL = $_GET['i'];

$fileupdate = $_POST['fileupdate'];

 

 

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['blog_id'] . " – " . "<a href='?b=" . $row['blog_id']."'>". $row['blog_title'] . "<a><br>";

}

}

elseif (isset($recordID))

{

 

echo"";?><form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">

<p>Record ID: <?php echo $row['blog_id']; ?> </p>

<p>Title:<br />

<input name="title" type="text" id="title" value="<?php echo $row['blog_title']; ?>" size="50"/>

</p>

<p>

Body:<br />

<textarea name="body" cols="60" rows="15" id="body"><?php echo $row['blog_body']; ?></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"> </td>

</tr>

<tr>

<td height="30" colspan="2" align="center"><input type="file" name="fileupload[]" id="fileupload[]" /></td>

<td align="center"> </td>

</tr>

<tr>

<td height="43" colspan="3" align="center">

<?php

$images = mysql_query("SELECT * FROM images WHERE blog_post_id = '{$row['blog_id']}'");

while($image = mysql_fetch_assoc($images)){

echo "<img height='100' src='" . $image['image_path'] . "' /> ";

echo "<input name='fileupdate' id='fileupdate' type='hidden' value='" . $image['image_id'] . "' />";

}?></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['blog_id']; ?>"/>

</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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.