Jump to content

Query updating when upload field blank


zimmo

Recommended Posts

I have a form that allows my client to update some products. Now the products are simple just basic info and 1 picture.

 

I have set this up so they can edit the products and change the information, having done this many times in the past, but now hit a puzzling block that I am baffled.

 

The client when editing is presented with the form with the information pulled from the database and the form fields loaded with that data ready to edit.

 

The image can either be left alone or they can choose to upload a new image. They are shown the image they currently have stored in the database.

 

The problem I have is EVEN if they decide not to upload an image and change other information, when the submit the form it must be sending a blank value for the image somewhere as it is updating the database and removing the image reference as if it has been removed.

 

I have an if/else statement based on the form to perform 2 different queries for the update in mysql.

 

Here is the code for the form update, as you can see the image should not update?? Please help??

 

if ($_SERVER['REQUEST_METHOD'] =='POST') {

//This stops SQL Injection in POST vars 
foreach ($_POST as $key => $value) { 
  $_POST[$key] = mysql_real_escape_string($value); 
}

// **************************** THIS IS FOR NO NEW IMAGE ********************************

if ($_SERVER['REQUEST_METHOD'] =='POST' && empty($_FILES['product_image']['name'])) {

   # setup SQL statement for no new image
  $SQL = " UPDATE products SET product_title = '{$_POST['product_title']}', product_description = '{$_POST['product_description']}', standard_price = '{$_POST['standard_price']}', deluxe_price = '{$_POST['deluxe_price']}' WHERE product_id = '{$_REQUEST['product_id']}' ";
}

// **************************** THIS IS FOR A NEW IMAGE ********************************

else {

// Check the image type is a jpeg or gif for the image.
if (($_FILES['product_image']['type'] != "image/gif") && ($_FILES['product_image']['type'] != "image/jpeg") && ($_FILES['product_image']['type'] != "image/pjpeg")) {
  echo "<FONT FACE=\"Verdana\"><SPAN CLASS=\"content\">You have chosen not to upload a <b>Product Image</b>.<BR></SPAN>" ;
} elseif ($_FILES['product_image']['size'] > 100000) {
  echo "<FONT FACE=\"Verdana\"><SPAN CLASS=\"content\">The file size is bigger than 300kb.<BR></SPAN>" ;
} else {
  move_uploaded_file($_FILES['product_image']['tmp_name'], "/httpdocs/product_images/".$_FILES['product_image']['name']) ;
echo "<FONT FACE=\"Verdana\"><SPAN CLASS=\"content\"><B>Your front image has successfully uploaded.</B><BR></SPAN>" ;
  }
}

  # setup SQL statement for update
  $SQL = " UPDATE products SET product_title = '{$_POST['product_title']}', product_description = '{$_POST['product_description']}', standard_price = '{$_POST['standard_price']}', deluxe_price = '{$_POST['deluxe_price']}', product_image = '{$_FILES['product_image']['name']}' WHERE product_id = '{$_REQUEST['product_id']}' ";
}
  
  #execute SQL statement
  $result = mysql_db_query( *****,"$SQL",$connection );

  # check for error
  if (!$result) { echo("ERROR: " . mysql_error() . "\n$SQL\n");    }

 

Link to comment
Share on other sites

I have been looking into this and I am using php5 which I have not used before. I just read something about using empty and it said: As of PHP 5, objects with no properties are no longer considered empty.

 

Could this be causing my problem???? and if so how do I change it to work?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.