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