user316717
user316717

Reputation: 89

MySQL Updating some database fields without overwriting fields not changed

Ok, this one is driving me nuts. I have a backend file uploader that uploads .jpg files to the server. Then I want to upload the filename(s) of the .jpgs to my database. So when the page loads I can add the filename from the database and the pictures will display on the page. This works fine, but I also need to be able to update the files and the filenames in the database. If the user changes all the files and file names everything is fine. But if the user wishes to change only one or two file(s) and filename(s) the MySql update statement ends up having some of the variables empty thereby effectively deleting the existing filenames in the record instead of leaving them alone. As usual I have searched stackoverflow and google before asking for help and I have not found anything that is really pertinent. Here is the applicable code.

<?php 
 session_start();
 $id = $_SESSION['id'];

 //This is the directory where images will be saved 
 $target = "imgs/"; 
// "http://www.surfcup.com/travel_site/images/ ";

 $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 

 $logo=($_FILES['imageLogo']['name']); 
 $pic1=($_FILES['image1']['name']); 
 $pic2=($_FILES['image2']['name']); 
 $pic3=($_FILES['image3']['name']); 
 $pic4=($_FILES['image4']['name']); 
 $pic5=($_FILES['image5']['name']); 


 // 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
 SET 
 hotel.imageLogo = '".$logo."',
 hotel.image1 = '".$pic1."',
 hotel.image2 = '".$pic1."',
 hotel.image3 ='".$pic1."',
 hotel.image4 = '".$pic1."',
 hotel.image5 = '".$pic1."'
 WHERE Hotels.id='".$id."'";


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

//stuff to upload the files below



?>

I think I either need to check if the variables are null and somehow not up load them or stop the database from accepting null entries. The later though would make the user have to add 6 files when he/she creates a record. What if they only had 5 or 3? I can't seem to get my head around how I would check if the variables are null and only upload the ones with filenames in them in the UPLOAD statement. Thanks again, in advance, for all your help. Dave

Upvotes: 3

Views: 1696

Answers (2)

Jeremy Harris
Jeremy Harris

Reputation: 24549

I think a better way of doing 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
  $index++;
}

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

Upvotes: 2

Gohn67
Gohn67

Reputation: 10638

You can try this. Basically it checks if the value is empty. If it is not, then it adds the value to the array. At the end we implode the array into a string that we will add to the your query. In the example I only did a few of the images, but you should get the point. It should work barring syntax errors in my code.

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

$uploaded_images = array();

if(!empty($logo)){
   $uploaded_images[] = "hotel.imageLogo = '".$logo."'";
}

if(!empty($pic1)){
   $uploaded_images[] = "hotel.image1 = '".$pic1."'";
}

if(!empty($pic2)){
   $uploaded_images[] = "hotel.image2 = '".$pic2."'";
}

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

Upvotes: 0

Related Questions