PDA

View Full Version : Image Change on Upload Issues


peakymatt
10-19-2012, 11:06 AM
Good Evening All

Well I am back again. I have now got things to work a bit better. In my update form I have two forms. The first is to upload changed data into a record in a sql database (which it does) and the second is to upload an image to the server and to put its path into the database.

The only thing I can't do is to get the image path to go into the record of the changed data from the first form. I have tried all sorts of things but nothing seems to work. I know it must have something to do with the record ID but can't work out how to do it.

Any help would be greatly appreciated.


<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form2")) {
$updateSQL = sprintf("UPDATE tb_instruments SET sub_category=%s, prod_name=%s, brand=%s, `desc`=%s, colour=%s, price_s=%s, price_r=%s, img_loc=%s, Category=%s, paypal=%s WHERE prod_ID=%s",
GetSQLValueString($_POST['sub_category'], "text"),
GetSQLValueString($_POST['prod_name'], "text"),
GetSQLValueString($_POST['brand'], "text"),
GetSQLValueString($_POST['desc'], "text"),
GetSQLValueString($_POST['colour'], "text"),
GetSQLValueString($_POST['price_s'], "int"),
GetSQLValueString($_POST['price_r'], "int"),
GetSQLValueString($_POST['img_loc'], "text"),
GetSQLValueString($_POST['Category'], "text"),
GetSQLValueString($_POST['paypal'], "text"),
GetSQLValueString($_POST['prod_ID'], "int"));

mysql_select_db($database_ramos, $ramos);
$Result1 = mysql_query($updateSQL, $ramos) or die(mysql_error());

$updateGoTo = "instrument_admin_view.php";
if (isset($_SERVER['QUERY_STRING'])) {
$updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
$updateGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $updateGoTo));
}

$colname_inst_update = "-1";
if (isset($_GET['recordID'])) {
$colname_inst_update = $_GET['recordID'];
}
mysql_select_db($database_ramos, $ramos);
$query_inst_update = sprintf("SELECT * FROM tb_instruments WHERE prod_ID = %s", GetSQLValueString($colname_inst_update, "int"));
$inst_update = mysql_query($query_inst_update, $ramos) or die(mysql_error());
$row_inst_update = mysql_fetch_assoc($inst_update);
$totalRows_inst_update = mysql_num_rows($inst_update);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/admin_temp.dwt" codeOutsideHTMLIsLocked="false" -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="keywords" content="Music Tuition at home, Canberra, gungahlin, tuggeranong, woden, weston creek, queanbeyan, jerrabomberra, Yass, Tumut, captains flat, bungendore, tarago, goulburn, gundagai, tumbarumba, batlow, adelong, brungle, talbingo, one on one lessons, group lessons, string lessons, violin lessons, viola lessons, cello lessons, banjo lessons, flute lessons, saxophone lessons, trumpet lessons, clarinet lessons, guitar lessons, bass guitar lessons, drum lessons, singing lessons, drama classes, woodwind lessons, brass lessons, reeds, instrument sales, intrument accesssories, tuition books, instrument hire" />
<!-- InstanceBeginEditable name="doctitle" -->
<title>Welcome to Ramos Music</title>
<!-- InstanceEndEditable -->
<script src="SpryAssets/SpryMenuBar.js" type="text/javascript"></script>

<script type="text/javascript" src="scripts/fadeImages.js"></script>
<link href="css/int-admin.css" rel="stylesheet" type="text/css" />
<link href="SpryAssets/SpryMenuBarHorizontal.css" rel="stylesheet" type="text/css" />
<!-- InstanceBeginEditable name="head" -->
<!-- InstanceEndEditable -->
<style type="text/css">
body {
background-image: url();
background-repeat: no-repeat;
}
</style>
</head>

<body>

<div class="container">
<div class="sidebar1">
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p></div>
<div class="content">
<table width="100%" height="215" align="center" cellpadding="0" cellspacing="0" class="nav">
<tr>
<td width="75%" height="213" valign="top"><table width="100%" cellspacing="0" cellpadding="0">
<tr>
<td height="178" valign="top"></tr>
<tr>
<td height="21" align="center" valign="middle"><ul id="MenuBar2" class="MenuBarHorizontal">
<li><a href="Index.htm">Home</a> </li>
<li><a href="Index.html">Log Out</a></li>
<li><a href="contacts.html" class="MenuBarItemSubmenu">Contact</a>
<ul>
<li><a href="map.html" title="See how far we go.">Tutoring Locations</a></li>
</ul>
</li>
</ul></td>
</tr>
</table></td>
<td width="25%" valign="top"><img src="images/ramos Header_admin.jpg" width="389" height="207" /></td>
</tr>
</table>
<!-- InstanceBeginEditable name="Content" -->
<p>Instrument Update Page</p>
<p>This page is used to update the Instruments for Ramos Music.</p>
<form action="<?php echo $editFormAction; ?>" method="post" name="form2" id="form2">
<table align="center" class="table">
<tr valign="baseline">
<td nowrap="nowrap" align="right">Product ID</td>
<td><?php echo $row_inst_update['prod_ID']; ?></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Category:</td>
<td><input type="text" name="Category" value="<?php echo htmlentities($row_inst_update['Category'], ENT_COMPAT, 'UTF-8'); ?>" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Sub Category:</td>
<td><input type="text" name="sub_category" value="<?php echo htmlentities($row_inst_update['sub_category'], ENT_COMPAT, 'UTF-8'); ?>" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Product Name:</td>
<td><input type="text" name="prod_name" value="<?php echo htmlentities($row_inst_update['prod_name'], ENT_COMPAT, 'UTF-8'); ?>" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Brand:</td>
<td><input type="text" name="brand" value="<?php echo htmlentities($row_inst_update['brand'], ENT_COMPAT, 'UTF-8'); ?>" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Description:</td>
<td><input type="text" name="desc" value="<?php echo htmlentities($row_inst_update['desc'], ENT_COMPAT, 'UTF-8'); ?>" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Colour:</td>
<td><input type="text" name="colour" value="<?php echo htmlentities($row_inst_update['colour'], ENT_COMPAT, 'UTF-8'); ?>" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Sale Price:</td>
<td><input type="text" name="price_s" value="<?php echo htmlentities($row_inst_update['price_s'], ENT_COMPAT, 'UTF-8'); ?>" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Regular Price:</td>
<td><input type="text" name="price_r" value="<?php echo htmlentities($row_inst_update['price_r'], ENT_COMPAT, 'UTF-8'); ?>" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Paypal:</td>
<td><input type="text" name="paypal" value="<?php echo htmlentities($row_inst_update['paypal'], ENT_COMPAT, 'UTF-8'); ?>" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Current Image:</td>
<td><img src="img/thumbs/<?php echo $row_inst_update['img_loc']; ?>" alt="" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">&nbsp;</td>
<td><input type="submit" value="Update record" /></td>
</tr>
</table>
<input type="hidden" name="MM_update" value="form2" />
<input type="hidden" name="prod_ID" value="<?php echo $row_inst_update['prod_ID']; ?>" />
</form>
<p>&nbsp;</p>
<p>
<?php



// function to get file extension
function getExtension($str) {

$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
// define max file size, change to suit your own maximum file size
define ("MAX_SIZE","1000");
$errors=0;
//get the original file from form
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$image = $_FILES["file"]["name"];// from your upload form
$uploadedfile = $_FILES['file']['tmp_name'];// from your upload form
// check for correct file extension
if ($image)
{
$filename = stripslashes($_FILES['file']['name']);
$extension = getExtension($filename);
$extension = strtolower($extension);
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
{
echo ' Unknown Image extension! ';// build your own error handling here
$errors = 1;
}
// check for correct file size
else
{
$size=filesize($_FILES['file']['tmp_name']);

if ($size > MAX_SIZE*1024)
{
echo "You have exceeded the size limit";// build your own error handling here
$errors = 1;
}
// check which extension we have and create file
if($extension=="jpg" || $extension=="jpeg" )
{
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);
}
else if($extension=="png")
{
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefrompng($uploadedfile);
}
else
{
$src = imagecreatefromgif($uploadedfile);
}
//resize image and create thumbs and small thumbs
list($width,$height)=getimagesize($uploadedfile);
//for main images, this keeps the width at 300px regardless of portrait or landscape
$newwidth=300;
$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);
//for thumbnails, change the $newwidth variable to what ever size you need
if($width > $height){//landscape
$newwidth1=120;
$newheight1=($height/$width)*$newwidth1;
$tmp1=imagecreatetruecolor($newwidth1,$newheight1) ;
}elseif($width < $height){//portrait
$newheight1=120;
$newwidth1=($width/$height)*$newheight1;
$tmp1=imagecreatetruecolor($newwidth1,$newheight1) ;
}
//for smaller thumbnails, my site required this smaller thumb. Delete this section if you dont need it
if($width > $height){//landscape
$newwidth2=50;
$newheight2=($height/$width)*$newwidth2;
$tmp2=imagecreatetruecolor($newwidth2,$newheight2) ;
}elseif($width < $height){//portrait
$newheight2=50;
$newwidth2=($width/$height)*$newheight2;
$tmp2=imagecreatetruecolor($newwidth2,$newheight2) ;
}
// create new resized files
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$ne wheight,$width,$height);

imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$ newheight1,$width,$height);

imagecopyresampled($tmp2,$src,0,0,0,0,$newwidth2,$ newheight2,$width,$height);
//declare destinations
$filename = "img/". $_FILES['file']['name']; //
$filename1 = "img/thumbs/". $_FILES['file']['name']; // your destination paths here
$filename2 = "img/small/". $_FILES['file']['name']; //
//write files to folders
imagejpeg($tmp,$filename,100);
imagejpeg($tmp1,$filename1,100);
imagejpeg($tmp2,$filename2,100);
//destroy temps to save memory
imagedestroy($src);
imagedestroy($tmp);
imagedestroy($tmp1);
imagedestroy($tmp2);
}
}
}
//If no errors registered, print the success message
if(isset($_POST['Submit']) && !$errors)
{
echo "Image Uploaded Successfully!";
}



//$uploadDir = 'img/';

if(isset($_POST['upload']))
{
$fileName = $_FILES['file']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$img_name = $_POST['img_name'];

$filePath = $uploadDir . $name;

//$result = move_uploaded_file($name, $filePath);
//if (!$result) {
//echo "Error uploading file";
//exit;
//}

mysql_select_db($database_ramos, $ramos);
//include 'file:///Macintosh HD/Applications/MAMP/htdocs/library/config.php';
//include 'file:///Macintosh HD/Applications/MAMP/htdocs/library/opendb.php';

if(!get_magic_quotes_gpc())
{
$name = addslashes($name);
$filePath = addslashes($filePath);
}

$query = "INSERT INTO tb_instruments ( filetype, size, type, name_loc, img_loc, name ) ".
"VALUES ('$filetype', '$fileSize', '$fileType', '$img_name', '$fileName', '$filePath')";

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

//include 'file:///Macintosh HD/Applications/MAMP/htdocs/library/closedb.php';
echo '<p align="center"><img src="images/Anim sax.gif" alt="" height="80" width="auto"></p>';
echo '<p align="center">Thankyou, your File has uploaded successfully.</p>';

}
?> Please select an image below to update to current record
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" enctype="multipart/form-data" name="upload" id="upload">
<table width="478" border="0" align="center" cellpadding="1" cellspacing="1" class="table">
<tr>
<td>Product ID:</td>
<td bgcolor="#CCCCCC"><?php echo $row_inst_update['prod_ID']; ?></td>
</tr>
<tr>
<td width="154">Select Image</td>
<td width="315" bgcolor="#CCCCCC"><input name="file" type="file" id="file" />
<input type="hidden" name="MAX_FILE_SIZE" value="2000000" /></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><span class="shopheader_categorytree"><strong>Note:</strong> When uploading an image please make sure that it has been resized to a max of 900px wide.</span></td>
</tr>
<tr>
<td><input name="upload" type="submit" class="button" id="upload" value=" Upload " /></td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>
<p><br />
</p>
<input type="hidden" name="MM_update" value="form" />
<input type="hidden" name="prod_ID" value="<?php echo $row_inst_update['prod_ID']; ?>" />
</form>
<!-- InstanceEndEditable -->
<p><br />
</p>
</div>
<div class="sidebar2">
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
</div>
<div class="footer">
<p>.</p>
<!-- end .footer --></div>
<!-- end .container --></div>
<script type="text/javascript">
var MenuBar1 = new Spry.Widget.MenuBar("MenuBar2", {imgDown:"SpryAssets/SpryMenuBarDownHover.gif", imgRight:"SpryAssets/SpryMenuBarRightHover.gif"});
</script>
</body>
<!-- InstanceEnd --></html>
<?php
mysql_free_result($inst_update);
?>

jmichae3
10-26-2012, 09:52 AM
- any data you are putting in a query that's a string should be wrapped by mysql_real_escape_string() before passing to mysql_query.

- you have a very unnecessary habit of selecting your database multiple times, it only need be done once.

- after using a result from mysql_query, and you are finished with that result, use mysql_free_result($result); or you will have unexplainable results and behavior from the database/php. I see this over and over again with new coders.

peakymatt
10-26-2012, 11:16 AM
Hi jmichae3

Thanks for the helpful hints. Am currently working through the code trying to fix those things up.

Thanks for your time