Jekyl1 Posted July 17, 2008 Share Posted July 17, 2008 Okay, so here goes... I have two MySQL tables (cookies and images). cookies contains info about cookies (name, description, price) and images contains image info for that cookie (path, name, size). I have a form, add.php which allows me to add new records. I can choose an image from my computer, upload it to the server, and then the filename is stored in the images table, while the other cookie info is stored in cookies. This part works perfectly. I have another form, edit.php, where I want to be able to update this info. The user clicks on a link, which passes an id to edit.php and populates all the text boxes with the correct information for that record. Then, the user can change the info, click Submit, and the record is updated with the new info. This too works perfectly... as long as I do not include the image portion. What I want is for the current image associated with the chosen record to be displayed (that part works, too!) and for the user to be able to change the image if desired. Either by uploading a new file, or by choosing an already uploaded file. Then the new file info should be updated in the images table. This is the part that isn't working. :-\ Here's what I got. The problem lies somewhere around: if(isset($_POST['update'])) because if I take that out, I get the "Error Uploading File" error. If I leave it in, everything is skipped and nothing gets updated. And like I said, if I take out the "image uploading" portions of the code, it works just fine. Ripping my hair out now... <?php // form not yet submitted // display initial form with values pre-filled if (!$_POST['update']) { // check for record ID if ((!isset($_GET['id']) || trim($_GET['id']) == '')) { die('Missing record ID!'); } // open database connection include '../includes/connect.php'; include '../includes/functions.php'; // generate and execute query $id = $_GET['id']; //this gets the selected record and all related values $query = "SELECT cookies.c_name, cookies.description, cookies.signature, cookies.price, images.name_img FROM cookies LEFT JOIN images ON cookies.id = images.id WHERE cookies.id = '$id'" OR DIE ('Unable to connect to database! Please try again later.'); $result = mysql_query($query) or die ("Error in query: $query. " . mysql_error()); // if a result is returned if (mysql_num_rows($result) > 0) { // turn it into an object $row = mysql_fetch_object($result); // print form with values pre-filled ?> <table cellspacing="5" cellpadding="5" bgcolor="#FFE8B7"> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST"> <input type="hidden" name="id" value="<?php echo $id; ?>"> <tr> <td valign="top"><h1>Cookie</h1></td> <td colspan="2"> <input size="50" maxlength="250" type="text" name="c_name" value="<?php echo $row->c_name; ?>"> </td> </tr> <tr> <td valign="top"><h1>Description</h1></td> <td colspan="2"><textarea name="description" cols="38" rows="10"><?php echo $row->description; ?></textarea></td> </tr> <tr> <td valign="top"><h1>Price</h1></td> <td colspan="2"> <input size="50" maxlength="250" type="text" name="price" value="<?php echo $row->price; ?>"> </td> </tr> <tr> <td valign="top"><h1>Featured?</h1></td> <td colspan="2"> <select maxlength="3" type="text" name="signature"> <option value="Current"><?php echo $row->signature; ?></option> <option value="Yes">Yes</option> <option value="No">No</option> </select> </td> </tr> <tr> <td width="129" valign="middle"><input type="hidden" name="MAX_FILE_SIZE" value="2000000"> <h1>Image</h1></td> <td width="240" valign="middle"><input name="userfile" type="file" id="userfile"> <?php echo $row->name_img; ?> <td width="100" valign="top"> Current Image <br /> <?php echo "<img class='thumbs' src='http://www.cookiesbycourtney.com/cookie_pics/" . $row->name_img . "' />" ; ?> </td> </tr> <tr> <td></td> <td> <input type="Submit" name="update" value="Update"> </td> </tr> </form> </table> <? //////////////////////////////////////////////////////////////////////////////////////////////////////////// //update image // you can change this to any directory you want // as long as php can write to it $uploadDir = '/home/content/c/o/u/courtscookies/html/cookie_pics/'; if(isset($_POST['update'])) { $fileName = $_FILES['userfile']['name']; $tmpName = $_FILES['userfile']['tmp_name']; $fileSize = $_FILES['userfile']['size']; $fileType = $_FILES['userfile']['type']; $filePath = $uploadDir . $fileName; $result = move_uploaded_file($tmpName, $filePath); if (!$result) { echo "<div class='resultBox'>"; echo "Error uploading file"; echo "</div>"; exit; } ///////////////////////////////////////////////////////////////////////////////////// } // no result returned // print graceful error message else { echo '<h2>That cookie could not be located in our database.</h2>'; } } // form submitted // start processing it else { // set up error list array $errorList = array(); $c_name = $_POST['c_name']; $description = $_POST['description']; $price = $_POST['price']; $signature = $_POST['signature']; $id = $_POST['id']; // check for record ID if ((!isset($_POST['id']) || trim($_POST['id']) == '')) { die ('Missing record ID!'); } // validate text input fields if (trim($_POST['c_name']) == '') { $errorList[] = 'Invalid entry: Cookie'; } if (trim($_POST['description']) == '') { $errorList[] = "Invalid entry: Description"; } if (trim($_POST['signature']) == '') { $errorList[] = "Invalid entry: Featured"; } // set default value for price if (trim($_POST['price']) == '') { $price = $def_price; } // check for errors // if none found... if (sizeof($errorList) == 0) { include '../includes/connect.php'; // generate and execute query $query = "UPDATE cookies SET c_name = '$c_name', description = '$description', price = '$price', signature = '$signature' WHERE id = '$id'"; $result = mysql_query($query) or die ("Error in query: $query. " . mysql_error()); //////////////////////////////////////////////////////////// include '../includes/connect.php'; if(!get_magic_quotes_gpc()) { $fileName = addslashes($fileName); $filePath = addslashes($filePath); } $query = "UPDATE images SET (name_img, size, type, path) ". "VALUES ('$fileName', '$fileSize', '$fileType', '$filePath') WHERE id = '$id'"; mysql_query($query) or die('Error, query failed : ' . mysql_error()); ///////////////////////////////////////////////////////////////////////////////////////////////////////////// // print result echo '<div class="resultBox"><span class="resultText">Update Successful! <br /><br /><a href=list_cookies.php><u>Back to cookies List</u></a> <br /> <a href="index.php"><u>Back to Main Menu</u></a> <br /> <a href="../index2.php"><u>Back to Site</u></a> </span></div>'; // close database connection mysql_close; } else { // errors occurred // print as list echo '<font size=-1>The following errors were encountered:'; echo '<br>'; echo '<ul>'; for ($x=0; $x<sizeof($errorList); $x++) { echo "<li>$errorList[$x]"; } echo '</ul>'; } } } ?> Quote Link to comment Share on other sites More sharing options...
l0ve2hat3 Posted July 17, 2008 Share Posted July 17, 2008 try adding this to the form tag enctype="multipart/form-data" sooo... <form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST"> Quote Link to comment Share on other sites More sharing options...
Jekyl1 Posted July 19, 2008 Author Share Posted July 19, 2008 Thanks for the help... But it didn't work. 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.