peakymatt Posted October 19, 2012 Share Posted October 19, 2012 Good Afternoon all This is my first post to this forum. I am quite new to php coding and have got myself totally lost. I am developing a website for a music company, that want to upload their own product. I have created an upload page which stores all the details in a sql database and uploads an image of the instrument to a folder "img" to the server. This all works a treat. A number of the entries have no photo supplied so a generic image has been uploaded in its place, that says "No image Provided". I have created a page to edit the entries so that the client can make changes to existing product, however I am now totally lost in how to replace the "No Image" image with an image of a new instrument. Any help you could provide on this would be a great help. I will supply my code for viewing, however I am pretty sure it is a total mess. Thanks in advance. <?php require_once('Connections/xxxxxx.php'); ?><?phpif (!isset($_SESSION)) {session_start();}$MM_authorizedUsers = "Administrator";$MM_donotCheckaccess = "false"; [/background][/left][/size][/font][/color][color=#000000][font=verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif][size=3] [left][background=rgb(223, 233, 240)]// *** Restrict Access To Page: Grant or deny access to this pagefunction isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) {// For security, start by assuming the visitor is NOT authorized.$isValid = False; [/background][/left] [/size][/font][/color][color=#000000][font=verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif][size=3] [left][background=rgb(223, 233, 240)] // When a visitor has logged into this site, the Session variable MM_Username set equal to their username.// Therefore, we know that a user is NOT logged in if that Session variable is blank.if (!empty($UserName)) {// Besides being logged in, you may restrict access to only certain users based on an ID established when they login.// Parse the strings into arrays.$arrUsers = Explode(",", $strUsers);$arrGroups = Explode(",", $strGroups);if (in_array($UserName, $arrUsers)) { $isValid = true;}// Or, you may restrict access to only certain users based on their username.if (in_array($UserGroup, $arrGroups)) { $isValid = true;}if (($strUsers == "") && false) { $isValid = true;}}return $isValid;} [/background][/left] [/size][/font][/color][color=#000000][font=verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif][size=3] [left][background=rgb(223, 233, 240)]$MM_restrictGoTo = "bad_access.php";if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) {$MM_qsChar = "?";$MM_referrer = $_SERVER['PHP_SELF'];if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";if (isset($_SERVER['QUERY_STRING']) && strlen($_SERVER['QUERY_STRING']) > 0)$MM_referrer .= "?" . $_SERVER['QUERY_STRING'];$MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);header("Location: ". $MM_restrictGoTo);exit;}?><?phpif (!function_exists("GetSQLValueString")) {function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = ""){if (PHP_VERSION < 6) {$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;} [/background][/left] [/size][/font][/color][color=#000000][font=verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif][size=3] [left][background=rgb(223, 233, 240)] $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); [/background][/left] [/size][/font][/color][color=#000000][font=verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif][size=3] [left][background=rgb(223, 233, 240)] 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;}} [/background][/left] [/size][/font][/color][color=#000000][font=verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif][size=3] [left][background=rgb(223, 233, 240)]$editFormAction = $_SERVER['PHP_SELF'];if (isset($_SERVER['QUERY_STRING'])) {$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);} [/background][/left] [/size][/font][/color][color=#000000][font=verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif][size=3] [left][background=rgb(223, 233, 240)]if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "upload")) {$updateSQL = sprintf("UPDATE tb_instruments SET sub_category=%s, prod_name=%s, brand=%s, `desc`=%s, colour=%s, price_s=%s, price_r=%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['Category'], "text"), GetSQLValueString($_POST['paypal'], "text"), GetSQLValueString($_POST['prod_ID'], "int")); [/background][/left] [/size][/font][/color][color=#000000][font=verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif][size=3] [left][background=rgb(223, 233, 240)] mysql_select_db($database_xxxxx, $xxxxx);$Result1 = mysql_query($updateSQL, $xxxxx) or die(mysql_error());// function to get file extensionfunction getExtension($str) { [/background][/left] [/size][/font][/color][color=#000000][font=verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif][size=3] [left][background=rgb(223, 233, 240)] $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 sizedefine ("MAX_SIZE","1000");$errors=0;//get the original file from formif($_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 extensionif ($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 sizeelse{$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 fileif($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 thumbslist($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 needif($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 itif($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 filesimagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); [/background][/left] [/size][/font][/color][color=#000000][font=verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif][size=3] [left][background=rgb(223, 233, 240)]imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height); [/background][/left] [/size][/font][/color][color=#000000][font=verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif][size=3] [left][background=rgb(223, 233, 240)]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 foldersimagejpeg($tmp,$filename,100);imagejpeg($tmp1,$filename1,100);imagejpeg($tmp2,$filename2,100);//destroy temps to save memoryimagedestroy($src);imagedestroy($tmp);imagedestroy($tmp1);imagedestroy($tmp2);}}}//If no errors registered, print the success messageif(isset($_POST['Submit']) && !$errors){echo "Image Uploaded Successfully!";} [/background][/left] [/size][/font][/color][color=#000000][font=verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif][size=3] [left][background=rgb(223, 233, 240)] $updateGoTo = "instrument_admin_view.php";if (isset($_SERVER['QUERY_STRING'])) {$updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";$updateGoTo .= $_SERVER['QUERY_STRING'];}header(sprintf("Location: %s", $updateGoTo));} [/background][/left] [/size][/font][/color][color=#000000][font=verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif][size=3] [left][background=rgb(223, 233, 240)]$colname_Recordset1 = "-1";if (isset($_GET['recordID'])) {$colname_Recordset1 = $_GET['recordID'];}mysql_select_db($database_ramos, $ramos);$query_Recordset1 = sprintf("SELECT * FROM tb_instruments WHERE prod_ID = %s", GetSQLValueString($colname_Recordset1, "int"));$Recordset1 = mysql_query($query_Recordset1, $ramos) or die(mysql_error());$row_Recordset1 = mysql_fetch_assoc($Recordset1);$totalRows_Recordset1 = mysql_num_rows($Recordset1);?><!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>Ramos Music Instrument Upload Page</title><!-- InstanceEndEditable --><script src="SpryAssets/SpryMenuBar.js" type="text/javascript"></script> [/background][/left] [/size][/font][/color][color=#000000][font=verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif][size=3] [left][background=rgb(223, 233, 240)]<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> [/background][/left] [/size][/font][/color][color=#000000][font=verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif][size=3] [left][background=rgb(223, 233, 240)]<body> [/background][/left] [/size][/font][/color][color=#000000][font=verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif][size=3] [left][background=rgb(223, 233, 240)]<div class="container"><div class="sidebar1"><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </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 class="admin_header">Edit Instruments Page</p><p class="firstParagraph">Use this form to Edit Instruments to the website for the public to view.</p><p class="firstParagraph"><a href="instrument_view_new.php">Click here when you have finished inputting data.</a></p><?php [/background][/left] [/size][/font][/color][color=#000000][font=verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif][size=3] [left][background=rgb(223, 233, 240)]$filePath = $uploadDir . $name; [/background][/left] [/size][/font][/color][color=#000000][font=verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif][size=3] [left][background=rgb(223, 233, 240)]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'; [/background][/left] [/size][/font][/color][color=#000000][font=verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif][size=3] [left][background=rgb(223, 233, 240)]if(!get_magic_quotes_gpc()){$name = addslashes($name);$filePath = addslashes($filePath);} [/background][/left] [/size][/font][/color][color=#000000][font=verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif][size=3] [left][background=rgb(223, 233, 240)]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>'; [/background][/left] [/size][/font][/color][color=#000000][font=verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif][size=3] [left][background=rgb(223, 233, 240)]?> [/background][/left] [/size][/font][/color][color=#000000][font=verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif][size=3] [left][background=rgb(223, 233, 240)]<form name="upload" action="<?php echo $editFormAction; ?>" method="POST" enctype="multipart/form-data"><table width="478" border="0" align="center" cellpadding="1" cellspacing="1" class="table"><tr> <td>ID</td> <td bgcolor="#CCCCCC"><input name="prod_ID" type="text" id="prod_ID" value="<?php echo $row_Recordset1['prod_ID']; ?>" size="40" /></td></tr><tr> <td>Category</td> <td bgcolor="#CCCCCC"><input name="Category2" type="text" id="Category2" value="<?php echo $row_Recordset1['Category']; ?>" size="40" /></td> </tr><tr> <td width="154">Sub Category</td> <td width="315" bgcolor="#CCCCCC"><input name="sub_category" type="text" id="sub_category" value="<?php echo $row_Recordset1['sub_category']; ?>" size="40" /></td> </tr><tr> <td>Product Name</td> <td bgcolor="#CCCCCC"><input name="prod_name" type="text" id="prod_name" value="<?php echo $row_Recordset1['prod_name']; ?>" size="40" /></td></tr><tr> <td>Brand</td> <td bgcolor="#CCCCCC"><input name="brand" type="text" id="brand" value="<?php echo $row_Recordset1['brand']; ?>" size="40" /></td> </tr><tr> <td>Colour</td> <td bgcolor="#CCCCCC"><input name="colour" type="text" id="colour" value="<?php echo $row_Recordset1['colour']; ?>" size="40" /></td> </tr><tr> <td> </td> <td bgcolor="#CCCCCC"> </td></tr><tr> <td>Regular Price</td> <td bgcolor="#CCCCCC"><input name="price_r" type="text" id="price_r" value="<?php echo $row_Recordset1['price_r']; ?>" size="40" /></td></tr><tr> <td>Sale Price</td> <td bgcolor="#CCCCCC"><input name="price_s" type="text" id="price_s" value="<?php echo $row_Recordset1['price_s']; ?>" size="40" /></td></tr><tr> <td> </td> <td bgcolor="#CCCCCC"> </td> </tr><tr> <td>Description</td> <td bgcolor="#CCCCCC"><textarea name="desc" id="desc" cols="40" rows="5"><?php echo $row_Recordset1['desc']; ?></textarea></td> </tr><tr> <td> </td> <td bgcolor="#CCCCCC"> </td></tr><tr> <td>Paypal "Add to Cart" Code. This is copied stright from PayPal.</td> <td bgcolor="#CCCCCC"><textarea name="paypal" id="paypal" cols="40" rows="5"><?php echo $row_Recordset1['paypal']; ?></textarea></td></tr><tr> <td> </td> <td bgcolor="#CCCCCC"> </td> </tr><tr> <td>Select Image</td> <td bgcolor="#CCCCCC"><input name="file" type="file" id="file" value="<?php echo $row_Recordset1['img_loc']; ?>" /> <input type="hidden" name="MAX_FILE_SIZE" value="2000000" /></td> </tr><tr> <td> </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> </td> </tr><tr> <td> </td> <td> </td> </tr></table><p><br /></p><input type="hidden" name="MM_update" value="form" /><input type="hidden" name="MM_update" value="upload" /></form><br /><!-- InstanceEndEditable --><p><br /></p></div><div class="sidebar2"><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </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><?phpmysql_free_result($Recordset1);?> Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 25, 2012 Share Posted October 25, 2012 I am now totally lost in how to replace the "No Image" image with an image of a new instrument. Your code is extremely hard to follow, so I can't give specifics. But, one thing you stated that when no image is supplied a generic image is uploaded in it's place. Are you saying that you are creating multiple copies of that generic image for each item that does not have a custom image? If so, you can stop that. Instead do two checks: 1) Is there a defined image for the product and 2) Does that image exist. If the check for both is true, then use that image, ELSE default to a generic image - the same generic image used for any product that does not have an assigned image. As for allowing the user to change the image, I would do the following on the form. Either just above or below the image input field display the currently selected image (if there is one). That let's the user know if there is already an image or not. Then when the user submits the form do a check of the input field for the image. If the filed is empty then do nothing with that information (i.e. do not update/change the image value) - I think that is where you might have gotten hung up on. If there is a value, then use it to update the selected image for that item. The one trick you may want to implement is a means of allowing the user to REMOVE the current image without replacing it. So, instead of leaving the upload field empty (which will mean no change) you instead need to provide a different input/method to remove the image. Since you are only talking about one image per product, I would just add a "remove image" link next to the displayed current image (if there is one). That link can simply point to a page to remove the image (e.g. remove_image.php?id=id_of_item). So, when that page is called it will use the ID in the URL to remove the image assigned to that item then redirect the user back to the form. Quote Link to comment Share on other sites More sharing options...
MDCode Posted October 25, 2012 Share Posted October 25, 2012 No wonder you can't find the problem. There's so many <p> </p> and tags. could you please clean it up? Quote Link to comment Share on other sites More sharing options...
White_Lily Posted October 25, 2012 Share Posted October 25, 2012 lol - that code could be cleaned up so much! not just the color tags and the whats with the mass of <tr>'s and <td>'s lol... Ever heard of styling to design the look of a page? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.