0

I have the script updating everything else in my table with no issues at all, but when I go to update the image it never updates the image, but replaces the current image with the image that is only supposed to show if there is no image uploaded to begin with. This replaces images as well that I don't want replaced if I have no updates on that picture to begin with - it's quite frustrating!

Any suggestions, or comments will be appreciated! The code I'm using is below:

<?php
include ("db_connect.inc");
$tablename = "northwest_conditions";

if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
{
   if (!isset($_POST["update"]))
   {
      $id = $_GET["id"];
      $sql = "SELECT * FROM $tablename WHERE id=$id";
      $result = mysql_query($sql);        
      $tabrow = mysql_fetch_array($result);
      ?>
	  
      <form action="nwedit.php" method="post">
      <input type=hidden name="id" value="<?php echo $tabrow["id"] ?>">
<span class="style22">Date:
<input type="text" name="date" id="date" value="<?php echo $tabrow["date"] ?>" /> 
  (mm/dd/yy)<br />
  Past Weeks Rainfall: 
  <input type="text" name="pastwrain" id="pastwrain" value="<?php echo $tabrow["pastwrain"] ?>" /><br />
  Soil Moisture: 
<input type="text" name="soilmoist" id="soilmoist" value="<?php echo $tabrow["soilmoist"] ?>" />
  <br />
  Temperature:
  <input type="text" name="temp" id="temp" value="<?php echo $tabrow["temp"] ?>" />
<br />
  Crop Progress
<input type="text" name="cp" id="cp" value="<?php echo $tabrow["cp"] ?>" />
</span>
<h2> <strong>Corn</strong></h2>
<span class="style22">
Current Picture:<br />
<img src="uploads/<?php echo $tabrow['cpicname'] ?>" width="150" /><br />
Upload Picture:
  <input type="hidden" name="MAX_FILE_SIZE" value="3200000">
<input name="picfile" type="file" id="picfile"> 
  <br />
  Crop Stage:
  <input type="text" name="ccs" id="ccs" value="<?php echo $tabrow["ccs"] ?>" />
  <br />
  Crop Yield Potential:
  <input type="text" name="ccyp" id="ccyp" value="<?php echo $tabrow["ccyp"] ?>" />
  <br />
  Current Price:
  <input type="text" name="ccp" id="ccp" value="<?php echo $tabrow["ccp"] ?>" />
  <br />
  Fall Price:
  <input type="text" name="cfp" id="cfp" value="<?php echo $tabrow["cfp"] ?>" />
  <br />
  Past Week Trend:
  <input type="text" name="cpwt" id="cpwt" value="<?php echo $tabrow["cpwt"] ?>" />
  </span>
<h2> <strong>Soybeans</strong></h2>
<span class="style22">
Current Picture:<br />
<img src="uploads/<?php echo $tabrow['sbpicname'] ?>" width="150" /><br />
  Upload Picture:
<input type="hidden" name="MAX_FILE_SIZE" value="3200000">
<input name="picfile2" type="file" id="picfile2"> 
  <br />
  Crop Stage:
  <input type="text" name="sbcs" id="sbcs" value="<?php echo $tabrow["sbcs"] ?>" />
  <br />
  Crop Yield Potential:
  <input type="text" name="sbcyp" id="sbcyp" value="<?php echo $tabrow["sbcyp"] ?>" />
  <br />
  Current Price:
  <input type="text" name="sbcp" id="sbcp" value="<?php echo $tabrow["sbcp"] ?>" />
  <br />
  Fall Price:
  <input type="text" name="sbfp" id="sbfp" value="<?php echo $tabrow["sbfp"] ?>" />
  <br />
  Past Week Trend:
  <input type="text" name="sbpwt" id="sbpwt" value="<?php echo $tabrow["sbpwt"] ?>" />
  <br />
  Comments:<br />
  <textarea name="comments" cols="40" rows="4" id="comments"><?php echo $tabrow["comments"] ?></textarea>
</span><br />
  
      <input type="hidden" name="cmd" value="edit">
   
      <input name="update" type="submit" class="box" id="update" value="update">
   
      </form>
   
   <?php } 


// Set the upload dir, and the default image data.
$uploadDir = '/home/website/uploads/';
$defaultImagePath = "/home/website/uploads/NoPicture.jpg";
$defaultImageType = "image/jpeg";
$defaultImageName = basename($defaultImagePath);
$defaultImageSize = filesize($defaultImagePath);
$tablename = "northwest_conditions";

   if ($_POST["update"])
   {
	if(get_magic_quotes_gpc()) {
    foreach($_POST as &$__field) {
        $__field = stripslashes($__field);
    	}
	}
	// Try to use the uploaded file only if the file was
    // actually uploaded, and there was no error.
    if(isset($_FILES['picfile'], $_FILES['picfile2'])
    && $_FILES['picfile']['error'] == 0
    && $_FILES['picfile2']['error'] == 0)
    {
        $fileName = $_FILES['picfile']['name'];
        $tmpName = $_FILES['picfile']['tmp_name'];
        $fileSize = $_FILES['picfile']['size'];
        $fileType = $_FILES['picfile']['type'];
        $fileName2 = $_FILES['picfile2']['name'];
        $tmpName2 = $_FILES['picfile2']['tmp_name'];
        $fileSize2 = $_FILES['picfile2']['size'];
        $fileType2 = $_FILES['picfile2']['type'];

        $filePath = $uploadDir . $fileName;
        $filePath2 = $uploadDir . $fileName2;

        if(!move_uploaded_file($tmpName, $filePath) || !move_uploaded_file($tmpName2, $filePath2))
        {
            echo "Failed to move the uploaded files. Make sure you have write permission in '{$uploadDir}'.";
            exit;
        }
    }
	elseif(isset($_FILES['picfile']) && $_FILES['picfile']['error'] == 0)
    {
        $fileName = $_FILES['picfile']['name'];
        $tmpName = $_FILES['picfile']['tmp_name'];
        $fileSize = $_FILES['picfile']['size'];
        $fileType = $_FILES['picfile']['type'];
		$fileName2 = $tabrow['sbpicname'];
        $tmpName2 = $tabrow['sbpicname'];
        $fileSize2 = $tabrow['sbpicsize'];
        $fileType2 = $tabrow['sbpictype'];

        $filePath = $uploadDir . $fileName;
        $filePath2 = $uploadDir . $fileName2;

        if(!move_uploaded_file($tmpName, $filePath) || !move_uploaded_file($tmpName2, $filePath2))
        {
            echo "Failed to move the uploaded files. Make sure you have write permission in '{$uploadDir}'.";
            exit;
        }
    }
	elseif(isset($_FILES['picfile2']) && $_FILES['picfile2']['error'] == 0)
    {
		$fileName = $tabrow['cpicname'];
        $tmpName = $tabrow['cpicname'];
        $fileSize = $tabrow['cpicsize'];
        $fileType = $tabrow['cpictype'];
        $fileName2 = $_FILES['picfile2']['name'];
        $tmpName2 = $_FILES['picfile2']['tmp_name'];
        $fileSize2 = $_FILES['picfile2']['size'];
        $fileType2 = $_FILES['picfile2']['type'];

        $filePath = $uploadDir . $fileName;
        $filePath2 = $uploadDir . $fileName2;

        if(!move_uploaded_file($tmpName, $filePath) || !move_uploaded_file($tmpName2, $filePath2))
        {
            echo "Failed to move the uploaded files. Make sure you have write permission in '{$uploadDir}'.";
            exit;
        }
    }
    else
    {
        // Use the default image.
        $fileName = $defaultImageName;
        $tmpName = $defaultImagePath;
        $fileSize = $defaultImageSize;
        $fileType = $defaultImageType;
        $fileName2 = $defaultImageName;
        $tmpName2 = $defaultImagePath;
        $fileSize2 = $defaultImageSize;
        $fileType2 = $defaultImageType;

        $filePath = $defaultImagePath;
        $filePath2 = $defaultImagePath;
    }
    // Get the data ready for being inserted into the SQL query
    $nwcomments = mysql_real_escape_string ($_POST['comments']);
    $nwpastwrain = mysql_real_escape_string ($_POST['pastwrain']);
    $nwtemp = mysql_real_escape_string ($_POST['temp']);
    $nwcp = mysql_real_escape_string ($_POST['cp']);
    $nwccs = mysql_real_escape_string ($_POST['ccs']);
    $nwccyp = mysql_real_escape_string ($_POST['ccyp']);
    $nwccp = mysql_real_escape_string ($_POST['ccp']);
    $nwcfp = mysql_real_escape_string ($_POST['cfp']);
    $nwcpwt = mysql_real_escape_string ($_POST['cpwt']);
    $nwsbcs = mysql_real_escape_string ($_POST['sbcs']);
    $nwsbcyp = mysql_real_escape_string ($_POST['sbcyp']);
    $nwsbcp = mysql_real_escape_string ($_POST['sbcp']);
    $nwsbfp = mysql_real_escape_string ($_POST['sbfp']);
    $nwsbpwt = mysql_real_escape_string ($_POST['sbpwt']);
    $nwsoilmoist = mysql_real_escape_string ($_POST['soilmoist']);
    $date = mysql_real_escape_string ($_POST['date']);

    $sql = "UPDATE $tablename SET cpicname='$fileName', cpicsize='$fileSize', cpictype='$fileType', cpicpath='$filePath', sbpicname='$fileName2', sbpicsize='$fileSize2', sbpictype='$fileType2', sbpicpath='$filePath2', date='$date', pastwrain='$nwpastwrain', soilmoist='$nwsoilmoist', temp='$nwtemp', cp='$nwcp', ccs='$nwccs', ccyp='$nwccyp', ccp='$nwccp', cfp='$nwcfp', cpwt='$nwcpwt', sbcs='$nwsbcs', sbcyp='$nwsbcyp', sbcp='$nwsbcp', sbfp='$nwsbfp', sbpwt='$nwsbpwt', comments='$nwcomments' WHERE id=$id";

    $result = mysql_query($sql) or die('Error, query failed : ' . mysql_error());

      echo "Thank you! Information updated. <br />";
	  echo "<a href='edit_select.php'>Click here to edit another entry.</a>";
   }
}
?>
2
Contributors
10
Replies
11
Views
8 Years
Discussion Span
Last Post by sethm
1

Hello again, I didn't have time to go through your whole code (gotta get some shuteye) but I noticed that your Form didn't have enctype="multipart/form-data" in its attribute list. Try running your script with that added.

Also, I recommend that you clean up your validation (all those IF-ELSEIF) so that your code is not repeated.

0

Thanks! I'll give that a shot (and I know...the repeating code is a bit..redundant to say the least - I just have it working..I'll worry about cleaning it up later..I suppose)

0

Finally it is allowing the uploading images : ) So you were right on on that one..can't believe I forgot that... Thank you!!

Anyway now, when I am trying to get the file to only upload one and not change the second photo it completely replaces the second with just a big blank square...

Thanks,
Seth

0

It could be because your validation is kicking in. I noticed that even though you try to validate each file field, you try to move both in the end.

if(!move_uploaded_file($tmpName, $filePath) || !move_uploaded_file($tmpName2, $filePath2))
{
	echo "Failed to move the uploaded files. Make sure you have write permission in '{$uploadDir}'.";
	exit;
}

So even if only one file were uploaded, your script will take both image fields (even if it's empty) . Better to dumb down the logic so it causes less headaches for you.

0

I removed the validation on the image that wasn't being uploaded, then played with some of the other variables..

Right now it will upload one image, but the url path to the other image won't show up correctly as the image that I had uploaded before - this path should remain what it is in the database, which I was trying to do through set variables..but it doesn't seem to work.

Here is my current code:

<?php
include ("db_connect.inc");
$tablename = "northwest_conditions";

if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
{
   if (!isset($_POST["update"]))
   {
      $id = $_GET["id"];
      $sql = "SELECT * FROM $tablename WHERE id=$id";
      $result = mysql_query($sql);        
      $tabrow = mysql_fetch_array($result);
      ?>
	  
<form action="nwedit.php" method="post" enctype="multipart/form-data">
      <input type=hidden name="id" value="<?php echo $tabrow["id"] ?>">
<span class="style22">Date:
<input type="text" name="date" id="date" value="<?php echo $tabrow["date"] ?>" /> 
  (mm/dd/yy)<br />
  Past Weeks Rainfall: 
  <input type="text" name="pastwrain" id="pastwrain" value="<?php echo $tabrow["pastwrain"] ?>" /><br />
  Soil Moisture: 
<input type="text" name="soilmoist" id="soilmoist" value="<?php echo $tabrow["soilmoist"] ?>" />
  <br />
  Temperature:
  <input type="text" name="temp" id="temp" value="<?php echo $tabrow["temp"] ?>" />
<br />
  Crop Progress
<input type="text" name="cp" id="cp" value="<?php echo $tabrow["cp"] ?>" />
</span>
<h2> <strong>Corn</strong></h2>
<span class="style22">
Current Picture:<br />
<img src="uploads/<?php echo $tabrow['cpicname'] ?>" width="150" /><br />
Upload Picture:
  <input type="hidden" name="MAX_FILE_SIZE" value="3200000">
<input name="picfile" type="file" id="picfile"> 
  <br />
  Crop Stage:
  <input type="text" name="ccs" id="ccs" value="<?php echo $tabrow["ccs"] ?>" />
  <br />
  Crop Yield Potential:
  <input type="text" name="ccyp" id="ccyp" value="<?php echo $tabrow["ccyp"] ?>" />
  <br />
  Current Price:
  <input type="text" name="ccp" id="ccp" value="<?php echo $tabrow["ccp"] ?>" />
  <br />
  Fall Price:
  <input type="text" name="cfp" id="cfp" value="<?php echo $tabrow["cfp"] ?>" />
  <br />
  Past Week Trend:
  <input type="text" name="cpwt" id="cpwt" value="<?php echo $tabrow["cpwt"] ?>" />
  </span>
<h2> <strong>Soybeans</strong></h2>
<span class="style22">
Current Picture:<br />
<img src="uploads/<?php echo $tabrow['sbpicname'] ?>" width="150" /><br />
  Upload Picture:
<input type="hidden" name="MAX_FILE_SIZE" value="3200000">
<input name="picfile2" type="file" id="picfile2"> 
  <br />
  Crop Stage:
  <input type="text" name="sbcs" id="sbcs" value="<?php echo $tabrow["sbcs"] ?>" />
  <br />
  Crop Yield Potential:
  <input type="text" name="sbcyp" id="sbcyp" value="<?php echo $tabrow["sbcyp"] ?>" />
  <br />
  Current Price:
  <input type="text" name="sbcp" id="sbcp" value="<?php echo $tabrow["sbcp"] ?>" />
  <br />
  Fall Price:
  <input type="text" name="sbfp" id="sbfp" value="<?php echo $tabrow["sbfp"] ?>" />
  <br />
  Past Week Trend:
  <input type="text" name="sbpwt" id="sbpwt" value="<?php echo $tabrow["sbpwt"] ?>" />
  <br />
  Comments:<br />
  <textarea name="comments" cols="40" rows="4" id="comments"><?php echo $tabrow["comments"] ?></textarea>
</span><br />
  
      <input type="hidden" name="cmd" value="edit">
   
      <input name="update" type="submit" class="box" id="update" value="update">
   
      </form>
   
<?php } ?>
<?php
if(get_magic_quotes_gpc()) {
    foreach($_POST as &$__field) {
        $__field = stripslashes($__field);
    }
}

// Set the upload dir, and the default image data.
$uploadDir = '/home/website/uploads/';
$defaultImagePath = "/home/website/NoPicture.jpg";
$defaultImageType = "image/jpeg";
$defaultImageName = basename($defaultImagePath);
$defaultImageSize = filesize($defaultImagePath);


if ($_POST["update"])
   {
	if(get_magic_quotes_gpc()) {
    foreach($_POST as &$__field) {
        $__field = stripslashes($__field);
    	}
	}
	// Try to use the uploaded file only if the file was
    // actually uploaded, and there was no error.
    if(isset($_FILES['picfile'], $_FILES['picfile2'])
    && $_FILES['picfile']['error'] == 0
    && $_FILES['picfile2']['error'] == 0)
    {
        $fileName = $_FILES['picfile']['name'];
        $tmpName = $_FILES['picfile']['tmp_name'];
        $fileSize = $_FILES['picfile']['size'];
        $fileType = $_FILES['picfile']['type'];
        $fileName2 = $_FILES['picfile2']['name'];
        $tmpName2 = $_FILES['picfile2']['tmp_name'];
        $fileSize2 = $_FILES['picfile2']['size'];
        $fileType2 = $_FILES['picfile2']['type'];

        $filePath = $uploadDir . $fileName;
        $filePath2 = $uploadDir . $fileName2;

        if(!move_uploaded_file($tmpName, $filePath) || !move_uploaded_file($tmpName2, $filePath2))
        {
            echo "Failed to move the uploaded files. Make sure you have write permission in '{$uploadDir}'.";
            exit;
        }
    }
	elseif(isset($_FILES['picfile']) && $_FILES['picfile']['error'] == 0)
    {
        $fileName = $_FILES['picfile']['name'];
        $tmpName = $_FILES['picfile']['tmp_name'];
        $fileSize = $_FILES['picfile']['size'];
        $fileType = $_FILES['picfile']['type'];
		$fileName2 = $tabrow['sbpicname'];
        $tmpName2 = $tabrow['sbpicpath'];
        $fileSize2 = $tabrow['sbpicsize'];
        $fileType2 = $tabrow['sbpictype'];

        $filePath = $uploadDir . $fileName;
        $filePath2 = $uploadDir . $fileName2;

        if(!move_uploaded_file($tmpName, $filePath))
        {
            echo "Failed to move the uploaded files. Make sure you have write permission in '{$uploadDir}'.";
            exit;
        }
    }
	elseif(isset($_FILES['picfile2']) && $_FILES['picfile2']['error'] == 0)
    {
		$fileName = $tabrow['cpicname'];
        $tmpName = $tabrow['cpicpath'];
        $fileSize = $tabrow['cpicsize'];
        $fileType = $tabrow['cpictype'];
        $fileName2 = $_FILES['picfile2']['name'];
        $tmpName2 = $_FILES['picfile2']['tmp_name'];
        $fileSize2 = $_FILES['picfile2']['size'];
        $fileType2 = $_FILES['picfile2']['type'];

        $filePath = $uploadDir . $fileName;
        $filePath2 = $uploadDir . $fileName2;

        if(!move_uploaded_file($tmpName2, $filePath2))
        {
            echo "Failed to move the uploaded files. Make sure you have write permission in '{$uploadDir}'.";
            exit;
        }
    }
    else
    {
        // Use the default image.
		$fileName = $tabrow['cpicname'];
        $tmpName = $tabrow['cpicpath'];
        $fileSize = $tabrow['cpicsize'];
        $fileType = $tabrow['cpictype'];
		$fileName2 = $tabrow['sbpicname'];
        $tmpName2 = $tabrow['sbpicpath'];
        $fileSize2 = $tabrow['sbpicsize'];
        $fileType2 = $tabrow['sbpictype'];

        $filePath = $uploadDir . $fileName;
        $filePath2 = $uploadDir . $fileName2;
    }
    // Get the data ready for being inserted into the SQL query
    $nwcomments = mysql_real_escape_string ($_POST['comments']);
    $nwpastwrain = mysql_real_escape_string ($_POST['pastwrain']);
    $nwtemp = mysql_real_escape_string ($_POST['temp']);
    $nwcp = mysql_real_escape_string ($_POST['cp']);
    $nwccs = mysql_real_escape_string ($_POST['ccs']);
    $nwccyp = mysql_real_escape_string ($_POST['ccyp']);
    $nwccp = mysql_real_escape_string ($_POST['ccp']);
    $nwcfp = mysql_real_escape_string ($_POST['cfp']);
    $nwcpwt = mysql_real_escape_string ($_POST['cpwt']);
    $nwsbcs = mysql_real_escape_string ($_POST['sbcs']);
    $nwsbcyp = mysql_real_escape_string ($_POST['sbcyp']);
    $nwsbcp = mysql_real_escape_string ($_POST['sbcp']);
    $nwsbfp = mysql_real_escape_string ($_POST['sbfp']);
    $nwsbpwt = mysql_real_escape_string ($_POST['sbpwt']);
    $nwsoilmoist = mysql_real_escape_string ($_POST['soilmoist']);
    $date = mysql_real_escape_string ($_POST['date']);

    $sql = "UPDATE $tablename SET cpicname='$fileName', cpicsize='$fileSize', cpictype='$fileType', cpicpath='$filePath', sbpicname='$fileName2', sbpicsize='$fileSize2', sbpictype='$fileType2', sbpicpath='$filePath2', date='$date', pastwrain='$nwpastwrain', soilmoist='$nwsoilmoist', temp='$nwtemp', cp='$nwcp', ccs='$nwccs', ccyp='$nwccyp', ccp='$nwccp', cfp='$nwcfp', cpwt='$nwcpwt', sbcs='$nwsbcs', sbcyp='$nwsbcyp', sbcp='$nwsbcp', sbfp='$nwsbfp', sbpwt='$nwsbpwt', comments='$nwcomments' WHERE id=$id";

    $result = mysql_query($sql) or die('Error, query failed : ' . mysql_error());

      echo "Thank you! Information updated. <br />";
	  echo "<a href='edit_select.php'>Click here to edit another entry.</a>";
   }
}
?>

Can anyone figure out what I am missing? Please? : )
Thanks jomanlk for your help so far! : )

0

Ok, well I have the script narrowed down to all those if elseif's causing the problem with my coding.

What I want is
Image in the database already - use current image unless uploading new image.

I thought my logic was logical in the following script..but now that it isn't working I'm really wondering if it is or not.

if(isset($_FILES['userfile'], $_FILES['userfile2'])
    && $_FILES['userfile']['error'] == 0
    && $_FILES['userfile2']['error'] == 0)
    {
        $fileName = $_FILES['userfile']['name'];
        $tmpName = $_FILES['userfile']['tmp_name'];
        $fileSize = $_FILES['userfile']['size'];
        $fileType = $_FILES['userfile']['type'];
        $fileName2 = $_FILES['userfile2']['name'];
        $tmpName2 = $_FILES['userfile2']['tmp_name'];
        $fileSize2 = $_FILES['userfile2']['size'];
        $fileType2 = $_FILES['userfile2']['type'];

        $filePath = $uploadDir . $fileName;
        $filePath2 = $uploadDir . $fileName2;

        if(!move_uploaded_file($tmpName, $filePath) || !move_uploaded_file($tmpName2, $filePath2))
        {
            echo "Failed to move the uploaded files. Make sure you have write permission in '{$uploadDir}'.";
            exit;
        }
    }
	elseif(isset($_FILES['userfile']) && $_FILES['userfile']['error'] == 0)
    {
        $fileName = $_FILES['userfile']['name'];
        $tmpName = $_FILES['userfile']['tmp_name'];
        $fileSize = $_FILES['userfile']['size'];
        $fileType = $_FILES['userfile']['type'];
		$fileName2 = $tabrow["sbpicname"];
        $tmpName2 = $tabrow["sbpicpath"];
        $fileSize2 = $tabrow["sbpicsize"];
        $fileType2 = $tabrow["sbpictype"];

        $filePath = $uploadDir . $fileName;
        $filePath2 = $uploadDir . $fileName2;

        if(!move_uploaded_file($tmpName, $filePath))
        {
            echo "Failed to move the uploaded files. Make sure you have write permission in '{$uploadDir}'.";
            exit;
        }
    }
	elseif(isset($_FILES['userfile2']) && $_FILES['userfile2']['error'] == 0)
    {
		$fileName = $tabrow["cpicname"];
        $tmpName = $tabrow["cpicpath"];
        $fileSize = $tabrow["cpicsize"];
        $fileType = $tabrow["cpictype"];
        $fileName2 = $_FILES['userfile2']['name'];
        $tmpName2 = $_FILES['userfile2']['tmp_name'];
        $fileSize2 = $_FILES['userfile2']['size'];
        $fileType2 = $_FILES['userfile2']['type'];

        $filePath = $uploadDir . $fileName;
        $filePath2 = $uploadDir . $fileName2;

        if(!move_uploaded_file($tmpName2, $filePath2))
        {
            echo "Failed to move the uploaded files. Make sure you have write permission in '{$uploadDir}'.";
            exit;
        }
    }
    else
    {
        // Use the default image.
		$fileName = $tabrow["cpicname"];
        $tmpName = $tabrow["cpicpath"];
        $fileSize = $tabrow["cpicsize"];
        $fileType = $tabrow["cpictype"];
		$fileName2 = $tabrow["sbpicname"];
        $tmpName2 = $tabrow["sbpicpath"];
        $fileSize2 = $tabrow["sbpicsize"];
        $fileType2 = $tabrow["sbpictype"];

        $filePath = $uploadDir . $fileName;
        $filePath2 = $uploadDir . $fileName2;
    }
0

I got this code to work at updating the sql database - got it originally from Atli - who was VERY helpful!

It still does the same issue as the other code in only allowing one photo to upload and the other being replaced with the default image. Which would be fine, but I am trying to edit this record and want to update just one picture while retaining the original... here's the code for this one:

<?php
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>";

}
else
{
    echo "<pre>No data was submitted.</pre>";
}
}
?>

If nothing else, I will have the whole thing figured out online for my future reference : S I never imagined how difficult php would be. 8 hours on it today..and still only got this far - disgusting! lol

Thanks to anyone who's looked at this, or helped me out with it - I appreciate someone helping me get started!
Seth

0

Your code has been written so that if an image is not sent, the system will replace the empty record with the default value. And when the query is run to update it, the query will update the DB overwriting the existing image with the default image.

I suggest you take a step back and look at the big picture. Some points you should consider in implementing your solution.

1. Have a tickbox next to the image letting the user indicate whether you want it updated or not.

2. Making the user click the tickbox in order to update the image will let you know for sure it needs to be updated. The file input box can remain hidden till the user ticks the tick box.

Your code is fine, you just need to refine your solution so that it fits your needs.

0

Your code has been written so that if an image is not sent, the system will replace the empty record with the default value. And when the query is run to update it, the query will update the DB overwriting the existing image with the default image.

I suggest you take a step back and look at the big picture. Some points you should consider in implementing your solution.

1. Have a tickbox next to the image letting the user indicate whether you want it updated or not.

2. Making the user click the tickbox in order to update the image will let you know for sure it needs to be updated. The file input box can remain hidden till the user ticks the tick box.

Your code is fine, you just need to refine your solution so that it fits your needs.

0

I suggest you take a step back and look at the big picture. Some points you should consider in implementing your solution.

1. Have a tickbox next to the image letting the user indicate whether you want it updated or not.

2. Making the user click the tickbox in order to update the image will let you know for sure it needs to be updated. The file input box can remain hidden till the user ticks the tick box.

Great suggestions! I was just trying to make it overcomplicated (like I normally always do).

I will sit down and see if I can revise this to utilize a check box to update it - Thanks! : )

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.