MySQL Updating some database fields without rewriting fields is not changed

Okay, this one turns me on. I have a file uploader that uploads .jpg files to a server. Then I want to load the .jpg filename into my database. So when the page loads, I can add the filename from the database and the images will be displayed on the page. This works great, but I also need to be able to update the files and filenames in the database. If the user changes all files and filenames, everything is fine. But if the user only wants to change one or two of the file (s) and the file name (s), the MySql update statement ends up with some of the variables being empty, thus effectively deleting the existing filenames in the record, instead of leaving them alone ... As usual, I've searched stackoverflow and google before asking for help and I haven't found anything that is really relevant.Here is the applicable code.

 $id = $_SESSION['id'];

 //This is the directory where images will be saved 
 $target = "imgs/"; 
// " ";

 $targetlogo = $target . basename( $_FILES['imageLogo']['name']);
 $targetpic1 = $target . basename( $_FILES['image1']['name']);
 $targetpic2 = $target . basename( $_FILES['image2']['name']);
 $targetpic3 = $target . basename( $_FILES['image3']['name']);
 $targetpic4 = $target . basename( $_FILES['image4']['name']);
 $targetpic5 = $target . basename( $_FILES['image5']['name']);

 //This gets all the other information from the form 


 // Connects to Database 
 mysql_connect("localhost", "surfcup_HotAdmin","password") or die ('I cannot connect to        the database because: ' .mysql_error());
 mysql_select_db("surfcup_hotels") or die('I cannot connect to the database because: .mysql_error());

 $query="UPDATE Hotels
 hotel.imageLogo = '".$logo."',
 hotel.image1 = '".$pic1."',
 hotel.image2 = '".$pic1."',
 hotel.image3 ='".$pic1."',
 hotel.image4 = '".$pic1."',
 hotel.image5 = '".$pic1."'

 mysql_query($query) or die ('Error Updating Hotel '.mysql_error());

//stuff to upload the files below



I think I need to either check if the variables are null, or somehow not load them, but stop the database from accepting null records. Later it would force the user to add 6 files when he creates a record. What if they only had 5 or 3? I can't seem to figure out how to check if the variables are null and only load the filenames given in the UPLOAD statement. Thanks again for your help. Dave


source to share

2 answers

You can try this. It basically checks if the value is empty. If it is not, then it adds the value to the array. At the end, we will inject the array into a string that we will add to your request. In this example, I took some pictures, but you should understand this. Disallowing syntax errors should work in my code.

Although I will say that this is not the best way to do it. You can definitely improve this and make it safer and more efficient.

$uploaded_images = array();

   $uploaded_images[] = "hotel.imageLogo = '".$logo."'";

   $uploaded_images[] = "hotel.image1 = '".$pic1."'";

   $uploaded_images[] = "hotel.image2 = '".$pic2."'";

$values_to_set = implode(', ', $uploaded_images);
$query= "UPDATE Hotels SET " . $values_to_set . " WHERE'" . $id . "'";




I think the best way to do this is to build your query dynamically. For example:

$images = array();
$images[] = ($_FILES['imageLogo']['name']); 
$images[] = ($_FILES['image1']['name']); 
$images[] = ($_FILES['image2']['name']); 
$images[] = ($_FILES['image3']['name']); 
$images[] = ($_FILES['image4']['name']); 
$images[] = ($_FILES['image5']['name']);

// Looping index to determine which hotel image it is
$index = 0;

// Start building query
$query = "UPDATE Hotels SET hotel.imageLogo = '".$images[0]."'";

// Loop through images and check if empty string
foreach($images as $image)
  if(!empty($image) && $index != 0)
     // Image name found, add to query
     $query .= " hotel.image".$index." = '".$image."',";
  // First hotel image iteration needs to be 1

// Finish query
$query .= " WHERE'".$id."'";




All Articles