Images disappearing when db updated

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Noobie
Forum Commoner
Posts: 85
Joined: Sun May 15, 2005 11:38 am

Images disappearing when db updated

Post by Noobie »

This is a bit long-winded so I apologise.

I've got a MySQL DB which holds vehicle information plus paths to a range of images for each vehicle. There are 6 large images and 6 thumbs for each vehicle. Each image has it's own database field (I realise that an array might have been better in hindsight but that's why I'm a Noobie!).

The client adds new vehicle details and uploads images via a form which all works nicely.

There is also another form where they can edit the existing information and upload different images if they choose to.

The problem is that if the client uses the edit form to upload a different image, all the large versions of images related to that vehicle after the new one disappear.

So if you've got:

image 1, image 2, image 3, image 4, image 5, image 6 in the database and you decide to upload a new image to replace the existing image 4 then the large version of image 5 and image 6 vanish from the DB - the thumbs are all still there.

If you upload 6 new images via the edit form then it all works fine - it's only if you try to upload less than 6.

I can't quite figure out which bit is causing me the problem - these are the various functions that process the edit form:

Code: Select all

// get the variables from the URL request string
  $id = $_POST['id'];
  $make = $_POST['make'];
  $model = $_POST['model'];
  $mileage = $_POST['mileage'];
  $price = $_POST['price'];
  $moreinfo = $_POST['moreinfo'];
   
// Get current image filenames
  $sql = "SELECT * FROM stocklist WHERE id=$id";
  $result = mysql_query ($sql,$dbh) or die ("Error Selecting Information From DB");

  while ($db_image_array = mysql_fetch_array($result)) 
  {
  $db_large1 = $db_image_array['image1400300'];
  $db_large2 = $db_image_array['image2400300'];
  $db_large3 = $db_image_array['image3400300'];
  $db_large4 = $db_image_array['image4400300'];
  $db_large5 = $db_image_array['image5400300'];
  $db_large6 = $db_image_array['image6400300'];
  $db_small1 = $db_image_array['image1150113'];
  $db_small2 = $db_image_array['image2150113'];
  $db_small3 = $db_image_array['image3150113'];
  $db_small4 = $db_image_array['image4150113'];
  $db_small5 = $db_image_array['image5150113'];
  $db_small6 = $db_image_array['image6150113'];
  }
   
 // Update entry
  $large1 = $image_array[0]['large'];
  $large2 = $image_array[1]['large'];
  $large3 = $image_array[2]['large'];
  $large4 = $image_array[3]['large'];
  $large5 = $image_array[4]['large'];
  $large6 = $image_array[5]['large'];
  $small1 = $image_array[0]['small'];
  $small2 = $image_array[1]['small'];
  $small3 = $image_array[2]['small'];
  $small4 = $image_array[3]['small'];
  $small5 = $image_array[4]['small'];
  $small6 = $image_array[5]['small'];

   
if ($large1 == "") {
   $large1 = $db_large1;
   }
if ($large2 == "") {
   $large2 = $db_large2;
   }
if ($large3 == "") {
   $large3 - $db_large3;
   }
if ($large4 == "") {
   $large4 - $db_large4;
   }
if ($large5 == "") {
   $large5 - $db_large5;
   }
if ($large6 == "") {
   $large6 - $db_large6;
   }
if ($small1 == "") {
   $small1 = $db_small1;
   }
if ($small2 == "") {
   $small2 = $db_small2;
   }
if ($small3 == "") {
   $small3 = $db_small3;
   }
if ($small4 == "") {
   $small4 = $db_small4;
   }
if ($small5 == "") {
   $small5 = $db_small5;
   }
if ($small6 == "") {
   $small6 = $db_small6;
   }
	
      
 if( $id )
   {
   $query = "UPDATE stocklist SET make='$make', model='$model', mileage='$mileage',  price='$price', moreinfo='$moreinfo', image1400300='$large1', image2400300='$large2', image3400300='$large3', image4400300='$large4', image5400300='$large5', image6400300='$large6', image1150113='$small1', image2150113='$small2', image3150113='$small3', image4150113='$small4', image5150113='$small5', image6150113='$small6' WHERE id='$id'";
   }
  
// save the info to the database
   $results = mysql_query( $query );

Does anything jump out at you as being wrong?

Thanks in advance.
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Re: Images disappearing when db updated

Post by Buddha443556 »

Noobie wrote:Does anything jump out at you as being wrong?
How about these minus signs:

Code: Select all

if ($large3 == "") {
   $large3 - $db_large3;
   }
if ($large4 == "") {
   $large4 - $db_large4;
   }
if ($large5 == "") {
   $large5 - $db_large5;
   }
if ($large6 == "") {
   $large6 - $db_large6;
   }
Noobie
Forum Commoner
Posts: 85
Joined: Sun May 15, 2005 11:38 am

Post by Noobie »

Thanks for spotting that. You wouldn't believe the amount of times I've read through that and missed them!
Post Reply