sethm 0 Light Poster

I have recently had some help from a very knowledgeable person here on the forums about uploading an image and inputting the path into a DB. I have even been able to tweak this code to update the rest of the fields on the website, when I need to edit them. However, this is where I run into an issue.

When I upload just a single picture (to replace the "nopicture available") it changes the other image that was there on the server to "nopciture available" picture.

I have looked over the script, tried to understand it, but alas my php skills are pretty much "NULL". If someone could help me, what I need the script to do is the following:
• Add a photo if there is one being uploaded
• If a photo is already added and we aren't replacing it - maintain the current photo and photo location that is already in the DB.
• Still update the rest of the fields like they are currently being updated - if no photos are being uploaded make sure the photos stay the way they currently are in the db without making them back to the default no image photo.

if(get_magic_quotes_gpc()) {
    foreach($_POST as &$__field) {
        $__field = stripslashes($__field);
    }
}

$uploadDir = '/home/website/uploads/';
$defaultImage = array(
    'path'  => "/home/website/uploads/NoPicture.jpg",
    'name'  => "NoPicture.jpg",
    'type'  => "image/jpeg",
    'size'  => filesize('/home/website/uploads/NoPicture.jpg')
);

if(isset($_POST['update']))
{
    // Create a list of possible image uploads
    $images = array('userfile', 'userfile2');
    
    // Create containers for data used in the SQL query
    $imgData = array();
    $postData = array();
    
    // Loop through the images and see if they exists.
    // If not, use the default.
    foreach($images as $_index => $_image) 
    {
        if(isset($_FILES[$_image]) && $_FILES[$_image]['error'] == 0) 
        {
            // Add the uploaded image to the imgData array and construct the
            // path that it should be moved to.
            $imgData[$_index] = $_FILES[$_image];
            $imgData[$_index]['path'] = $uploadDir . $imgData[$_index]['name'];

            // Try to move the uploaded image.
            // If it fails, use the default image instead.
            if(!move_uploaded_file($imgData[$_index]['tmp_name'], $imgData[$_index]['path']))
            {
                trigger_error("Failed to upload image '{$_image}', falling back on default. Image path '{$imgData[$_index]['path']}' is not accessible.", E_USER_WARNING);
                $imgData[$_index] = $defaultImage;
            }
        }
        else 
        {
            // Add the default image to the imgData array, replacing the current
            // image, which is apparently missing or didn't upload properly.
            $imgData[$_index] = $defaultImage;
        }
    }

    // Get the data ready for being inserted into the SQL query.
    // This simply fetches and prepares every POST field sent to the page.
    foreach($_POST as $_key => $_value)
    {
        $postData[$_key] = mysql_real_escape_string($_value);
    }

    // Construct the query using the imgData and postData arrays, and execute it.
    $sql =<<<SQL
UPDATE $tablename SET cpicname='{$imgData[0]['name']}', cpicsize='{$imgData[0]['size']}', cpictype='{$imgData[0]['type']}', cpicpath='{$imgData[0]['path']}', sbpicname='{$imgData[1]['name']}', sbpicsize='{$imgData[1]['size']}', sbpictype='{$imgData[1]['type']}', sbpicpath='{$imgData[1]['path']}', date='{$postData['date']}', pastwrain='{$postData['pastwrain']}', soilmoist='{$postData['soilmoist']}', temp='{$postData['temp']}', cp='{$postData['cp']}', ccs='{$postData['ccs']}', ccyp='{$postData['ccyp']}', ccp='{$postData['ccp']}', cfp='{$postData['cfp']}', cpwt='{$postData['cpwt']}', sbcs='{$postData['sbcs']}', sbcyp='{$postData['sbcyp']}', sbcp='{$postData['sbcp']}', sbfp='{$postData['sbfp']}', sbpwt='{$postData['sbwt']}', comments='{$postData['comments']}' WHERE id=$id;
SQL;
    mysql_query($sql) or die('Error, query failed : ' . mysql_error());
	
    echo "<br><a href='" . $tablename . ".php'>View Updated Page</a><br>";

}

I've added and removed code to this script many times - but every time I do it it breaks something. If someone could just explain what kind of things I need to pull or how to pull it correctly - just put me on the right track it would be awesome!

Thanks so much for any help I can get!
Seth