scmeeker Posted July 6, 2010 Share Posted July 6, 2010 I'm working on a user page that will add/edit information. Once the user is satisfied with addind/updating their details they move on to a new page where they will upload an image. I'm able to upload the image just fine but I'm trying to get the file name of the image to be inserted into the associated product record in the table. When I click it, it returns my image to the browser (good) and uploads it (good) but does not put the name in the product record. Here is the form action code for this page <?php session_start (); ?> <?php ini_set("memory_limit", "200000000"); // for large images so that we do not get "Allowed memory exhausted"?> <?php mysql_connect("localhost", "", "", "") or die(mysql_error()); mysql_select_db("store") or die(mysql_error()); $item_image = $_FILES['image_upload_box']['name']; $item_id = mysql_real_escape_string($_POST[id]); $sql="UPDATE product SET image_upload_box='$item_image' WHERE id = '$item_id'"; if (!mysql_query($sql)) { die('Error: ' . mysql_error()); } else { header("Location: add_new_product4.php?id={$item_id}=success&show_image=".$_FILES["image_upload_box"]["name"]); exit; } else{ header("Location: add_new_product.php4?upload_message=make sure the file is jpg, gif or png and that is smaller than 4MB&upload_message_type=error"); exit; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/206943-not-updating-correctly/ Share on other sites More sharing options...
Pikachu2000 Posted July 6, 2010 Share Posted July 6, 2010 At the top, right before mysql_connect(); echo "<pre>"; print_r($_FILES); echo "</pre>"; And after the query string is built: echo $sql; Let's see what's going on in that script. Quote Link to comment https://forums.phpfreaks.com/topic/206943-not-updating-correctly/#findComment-1082191 Share on other sites More sharing options...
hamza Posted July 7, 2010 Share Posted July 7, 2010 always print your query see values are there or not .and if getting values from page print post or get arrays on page. Quote Link to comment https://forums.phpfreaks.com/topic/206943-not-updating-correctly/#findComment-1082195 Share on other sites More sharing options...
scmeeker Posted July 7, 2010 Author Share Posted July 7, 2010 Okay...Pikachu2000, after inputting what you suggested, this is what came up in the browser: Array ( [image_upload_box] => Array ( [name] => resized_June_2010_007.jpg [type] => image/jpeg [tmp_name] => C:\Users\Desktop\xampp\tmp\php3295.tmp [error] => 0 [size] => 54351 ) ) UPDATE product SET image_upload_box='resized_June_2010_007.jpg' WHERE id = '' What do you make of that? It looks as though my id is not carrying over, although its up in the URL. ??? Quote Link to comment https://forums.phpfreaks.com/topic/206943-not-updating-correctly/#findComment-1082198 Share on other sites More sharing options...
Pikachu2000 Posted July 7, 2010 Share Posted July 7, 2010 You've just answered your own question. A variable passed through the URL is a GET var, not a POST var. Also, IF it's expected to be an integer, you should cast it as an integer, and not bother with mysql_real_escape_string() $item_id = (int) ($_GET[id]); // For an integer value. Quote Link to comment https://forums.phpfreaks.com/topic/206943-not-updating-correctly/#findComment-1082203 Share on other sites More sharing options...
scmeeker Posted July 7, 2010 Author Share Posted July 7, 2010 Okay...I updated that but the correct ID still isn't passing through. It's showing up as "0". Hmmm. So I plugged in your previous code to see what it was doing. Here is what it says: UPDATE product SET image_upload_box='resized_June_2010_015.jpg' WHERE id = '0' Now...before I click this update button to upload and update the image, this is what it says in the URL: http://localhost/add_new_product4.php?id=65 It says the correct ID number. Any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/206943-not-updating-correctly/#findComment-1082222 Share on other sites More sharing options...
Pikachu2000 Posted July 7, 2010 Share Posted July 7, 2010 I just noticed something I overlooked. I left an extra set of parentheses on $_GET['id'], but I tested that locally to see if it made a difference, and it doesn't. The var echoed fine with or without them. Can you post the revised code, please? I'll have a look through it again. Quote Link to comment https://forums.phpfreaks.com/topic/206943-not-updating-correctly/#findComment-1082230 Share on other sites More sharing options...
scmeeker Posted July 7, 2010 Author Share Posted July 7, 2010 Here is the revised code: (thanks!) <?php session_start (); ?> <?php ini_set("memory_limit", "200000000"); // for large images so that we do not get "Allowed memory exhausted"?> <?php mysql_connect("localhost", "", "", "store") or die(mysql_error()); mysql_select_db("store") or die(mysql_error()); $item_image = $_FILES['image_upload_box']['name']; $item_id = (int) ($_GET[id]); $sql="UPDATE product SET image_upload_box='$item_image' WHERE id = '$item_id'"; if (!mysql_query($sql)) { die('Error: ' . mysql_error()); } else { header("Location: add_new_product4.php?id={$item_id}=success&show_image=".$_FILES["image_upload_box"]["name"]); exit; } else{ header("Location: add_new_product.php4?upload_message=make sure the file is jpg, gif or png and that is smaller than 4MB&upload_message_type=error"); exit; } Quote Link to comment https://forums.phpfreaks.com/topic/206943-not-updating-correctly/#findComment-1082249 Share on other sites More sharing options...
Pikachu2000 Posted July 7, 2010 Share Posted July 7, 2010 Oddly, it tests fine here. The only thing I can think would be this line, adding the trim() (although that shouldn't make any difference), and leaving the single-quotes on the array index, but I tested both ways here, and both were fully functional. $item_id = (int) trim($_GET['id']); There's nothing else at all that goes in this part of the script is there? <?php session_start (); ?> <?php ini_set("memory_limit", "200000000"); // for large images so that we do not get "Allowed memory exhausted"?> <?php mysql_connect("localhost", "", "", "store") or die(mysql_error()); mysql_select_db("store") or die(mysql_error()); $item_image = $_FILES['image_upload_box']['name']; $item_id = (int) ($_GET[id]); $sql="UPDATE product SET image_upload_box='$item_image' WHERE id = '$item_id'"; Quote Link to comment https://forums.phpfreaks.com/topic/206943-not-updating-correctly/#findComment-1082264 Share on other sites More sharing options...
scmeeker Posted July 7, 2010 Author Share Posted July 7, 2010 This code has all the inbetween stuff: <?php session_start (); ?> <?php ini_set("memory_limit", "200000000"); // for large images so that we do not get "Allowed memory exhausted"?> <?php mysql_connect("localhost", "", "", "store") or die(mysql_error()); mysql_select_db("store") or die(mysql_error()); $item_image = $_FILES['image_upload_box']['name']; $item_id = (int) ($_GET[id]); $sql="UPDATE product SET image_upload_box='$item_image' WHERE id = '$item_id'"; if (!mysql_query($sql)) { die('Error: ' . mysql_error()); } else { // upload the file if ((isset($_POST["submitted_form"])) && ($_POST["submitted_form"] == "image_upload_form")) { // file needs to be jpg,gif,bmp,x-png and 4 MB max if (($_FILES["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg" || $_FILES["image_upload_box"]["type"] == "image/gif" || $_FILES["image_upload_box"]["type"] == "image/x-png") && ($_FILES["image_upload_box"]["size"] < 4000000)) { // some settings $max_upload_width = 500; $max_upload_height = 500; // if user chosed properly then scale down the image according to user preferances if(isset($_REQUEST['max_width_box']) and $_REQUEST['max_width_box']!='' and $_REQUEST['max_width_box']<=$max_upload_width){ $max_upload_width = $_REQUEST['max_width_box']; } if(isset($_REQUEST['max_height_box']) and $_REQUEST['max_height_box']!='' and $_REQUEST['max_height_box']<=$max_upload_height){ $max_upload_height = $_REQUEST['max_height_box']; } // if uploaded image was JPG/JPEG if($_FILES["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg"){ $image_source = imagecreatefromjpeg($_FILES["image_upload_box"]["tmp_name"]); } // if uploaded image was GIF if($_FILES["image_upload_box"]["type"] == "image/gif"){ $image_source = imagecreatefromgif($_FILES["image_upload_box"]["tmp_name"]); } // BMP doesn't seem to be supported so remove it form above image type test (reject bmps) // if uploaded image was BMP if($_FILES["image_upload_box"]["type"] == "image/bmp"){ $image_source = imagecreatefromwbmp($_FILES["image_upload_box"]["tmp_name"]); } // if uploaded image was PNG if($_FILES["image_upload_box"]["type"] == "image/x-png"){ $image_source = imagecreatefrompng($_FILES["image_upload_box"]["tmp_name"]); } $remote_file = "image_files/".$_FILES["image_upload_box"]["name"]; imagejpeg($image_source,$remote_file,100); chmod($remote_file,0644); // get width and height of original image list($image_width, $image_height) = getimagesize($remote_file); if($image_width>$max_upload_width || $image_height >$max_upload_height){ $proportions = $image_width/$image_height; if($image_width>$image_height){ $new_width = $max_upload_width; $new_height = round($max_upload_width/$proportions); } else{ $new_height = $max_upload_height; $new_width = round($max_upload_height*$proportions); } $new_image = imagecreatetruecolor($new_width , $new_height); $image_source = imagecreatefromjpeg($remote_file); imagecopyresampled($new_image, $image_source, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height); imagejpeg($new_image,$remote_file,100); imagedestroy($new_image); } imagedestroy($image_source); header("Location: add_new_product4.php?id={$item_id}=success&show_image=".$_FILES["image_upload_box"]["name"]); exit; } else{ header("Location: add_new_product.php4?upload_message=make sure the file is jpg, gif or png and that is smaller than 4MB&upload_message_type=error"); exit; } } } ?> Here is the form code: <form action="submit.php" method="post" enctype="multipart/form-data" name="image_upload_form" id="image_upload_form" style="margin-bottom:0px;"> <br /><label></label><br /> <input name="image_upload_box" type="file" id="image_upload_box" size="40" /> <input type="submit" name="submit" value="Upload image" /> <br /><br /> <input name="submitted_form" type="hidden" id="submitted_form" value="image_upload_form" /> </form> Thanks for your help! Quote Link to comment https://forums.phpfreaks.com/topic/206943-not-updating-correctly/#findComment-1082526 Share on other sites More sharing options...
scmeeker Posted July 7, 2010 Author Share Posted July 7, 2010 I just ran it again and after the image is uploaded, this is what is in the URL: http://localhost/add_new_product4.php?id=0=success&show_image=resized_June_2010_020.jpg See how the ID equals zero? Quote Link to comment https://forums.phpfreaks.com/topic/206943-not-updating-correctly/#findComment-1082530 Share on other sites More sharing options...
Pikachu2000 Posted July 7, 2010 Share Posted July 7, 2010 You aren't getting the value of ID in that form, and passing it along to the script. What is the process by which you arrive at that form? Is there another form you submit first to get there? If so, does that form have the ID value in it somewhere? Quote Link to comment https://forums.phpfreaks.com/topic/206943-not-updating-correctly/#findComment-1082554 Share on other sites More sharing options...
scmeeker Posted July 7, 2010 Author Share Posted July 7, 2010 Yes, there is a form page just previous to that. Here is what I have: 1st the user goes to add a new item by giving a description. They click next. When this happens, it adds the new item, giving them a new product ID. Then it brings them to the next page where they can review the information they just put in and edit it if necessary. When finished they click next to go to where they add the image of their product. This is where I'm at. For some reason the ID isn't carrying over completely. Just for testing, I tried inputted the username into the update and of course it worked but updated ALL my image titles to that name. Now...to narrow it down to the product ID.... Quote Link to comment https://forums.phpfreaks.com/topic/206943-not-updating-correctly/#findComment-1082562 Share on other sites More sharing options...
Pikachu2000 Posted July 7, 2010 Share Posted July 7, 2010 If you post the previous form and PHP code that processes it, I'll bet I can work it out pretty fast. Quote Link to comment https://forums.phpfreaks.com/topic/206943-not-updating-correctly/#findComment-1082570 Share on other sites More sharing options...
scmeeker Posted July 7, 2010 Author Share Posted July 7, 2010 Thanks. Okay...here it is: Here is the update/edit form code that comes just before that upload image page: <?php session_start(); //connect to database $mysqli = mysqli_connect("localhost", "", "", "store"); //validate item $get_item_sql = "SELECT id, title, description, price, location, ship_to, shipping_cost, international, in_shipping FROM product WHERE username = '".$_GET["username"]."'"; $get_item_res = mysqli_query($mysqli, $get_item_sql) or die(mysqli_error($mysqli)); if (mysqli_num_rows($get_item_res) < 1) { //invalid item $display_block .= "<p><em>Invalid item selection.</em></p>"; } else { //valid item, get info while ($item_info = mysqli_fetch_array($get_item_res)) { $item_title = $item_info['title']; $item_description = $item_info['description']; $item_price = $item_info['price']; $item_location = $item_info['location']; $item_ship_to = $item_info['ship_to']; $item_shipping_cost = $item_info['shipping_cost']; $item_international = $item_info['international']; $item_in_shipping = $item_info['in_shipping']; $item_id = $item_info['id']; $content .= ""; } } ?> <!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"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <link href="styles/llery2.css" rel="stylesheet" type="text/css" /> <style type="text/css"> <!-- a:link { color: #09C; text-decoration: none; } a:visited { text-decoration: none; color: #09C; } a:hover { text-decoration: underline; } a:active { text-decoration: none; } --> </style></head> <div align="center"> <body> <div class="mainbody"> <div class="header1"><a href="http://www.ne.com"><img src="Logo.gif" width="200" height="75" border="0" /></a> <a href="galleries.php">Galleries</a> <a href="join.php">Join</a> Community <a href="about.php">About</a> <a href="faq.php">FAQ</a> <a href="user_login.php">LOGIN</a> <span class="orangefont">My CART</span> My Store Welcome: <?php echo $_SESSION['username'] ?></div> <div class="storeeditleftsidebar"><br /> <br /> <span class="storeheaderfont">Your Products</span><br /> <br /> <?php echo $display_block = "<a href=\"add_new_product.php?username=".$_SESSION['username']."\">Add New</a>" ?><br /> <?php echo $display_block = "<a href=\"current_products.php?username=".$_SESSION['username']."\">Current</a>" ?><br /> <?php echo $display_block = "<a href=\"sold_products.php?username=".$_SESSION['username']."\">Sold</a>" ?><br /> <br /> <span class="storeheaderfont">Your Store</span><br /><br /> <?php echo $display_block = "<a href=\"edit_artist_profile.php?username=".$_SESSION['username']."\">Your Artist Profile</a>" ?><br /> <?php echo $display_block = "<a href=\"edit_store_profile.php?username=".$_SESSION['username']."\">Your Store Profile</a>" ?><br /> <?php echo $display_block = "<a href=\"edit_store_policies.php?username=".$_SESSION['username']."\">Your Store Policies</a>" ?><br /> <br /> <?php echo $display_block = "<a href=\"edit_artist_account.php?username=".$_SESSION['username']."\">Account Information</a>" ?><br /> <?php echo $display_block = "<a href=\"_bill.php?username=".$_SESSION['username']."\">Your Bill</a>" ?><br /> <br /> <a href="seller_faqs.php">Seller FAQ's</a><br /> </div> <br /> <div class="storeeditright"> <br /> <span class="titlefont">Add a new product to your store</span><br /> <br /> <br /> <span class="itemmenufont">1. Add Item Information</span> 2. Confirm Item Info <span class="itemmenufont">3. Add Image</span> <span class="itemmenufont">4. Finalize Listing</span><br /> <br /> <br /> <span class="titles">Title:</span> <?php echo "$item_title" ?><br /> <br /> <span class="titles">Item Description:</span> <?php echo "$item_description" ?><br /> <br /> <span class="titles">Price:</span> <?php echo "$item_price" ?><br /> <br /> <span class="titles">Shipping From:</span> <?php echo "$item_location" ?><br /> <br /> <span class="titles">Ship To:</span> <?php echo "$item_ship_to" ?><br /> <br /> <span class="titles">Shipping Cost:</span> <?php echo "$item_shipping_cost" ?><br /> <br /> <span class="titles">Ship Internationally?:</span> <?php echo "$item_international" ?><br /> <br /> <span class="titles">International Shipping Cost:</span> <?php echo "$item_in_shipping" ?><br /> <br /> <br /><?php echo "$item_id" ?><br /> <span class="storeheaderfont">Edit your information here</span> <br /> <br /> Edit your information here.</span><br /><br /> <br /> <form action="update_new.php" method="post" enctype="multipart/form-data" name="update_new" id="update_new"> <span class="titles">Title:</span> <input type="text" name="title" size="40" value="<?php echo $item_title ?>" /><br /> <br /> <span class="titles">Item Description:</span> <br /> <p> <textarea name="description" rows=10 cols=60><?php echo $item_description ?></textarea></p><br /> <table width="542" height="225" border="0"> <tr> <td width="251" height="13" valign="bottom"><span class="titles">Price: </span>(US Dollars)</td> <td width="281" height="13" align="right" valign="bottom">$<input type="text" name="price" size="40" value="<?php echo $item_price ?>"?>"/></td> </tr> <tr> <td height="13" valign="bottom" class="titles">Shipping from:</td> <td height="13" align="right" valign="bottom"><input type="text" name="location" size="40" value="<?php echo $item_location ?>"/></td> </tr> <tr> <td height="13" valign="bottom" class="titles">Ship to:</td> <td height="13" align="right" valign="bottom"><input type="text" name="ship_to" size="40" value="<?php echo $item_ship_to ?>"/></td> </tr> <tr> <td height="13" valign="bottom" class="titles">Shipping Cost:</td> <td height="13" align="right" valign="bottom">$<input type="text" name="shipping_cost" size="40" value="<?php echo $item_shipping_cost ?>"/></td> </tr> <tr> <td height="13" valign="bottom" class="titles">Ship Internationally?:</td> <td height="13" valign="bottom"> <input type="radio" name="international" value="yes" />yes <input type="radio" name="international" value="no" />no </td> </tr> <tr> <td height="10" valign="bottom" class="titles">International Shipping Cost:</td> <td height="13" align="right" valign="bottom">$<input type="text" name="in_shipping" size="40" value="<?php echo $item_international ?>"/></td> </tr> </table> <br /><input name="id" type="hidden" id="id" value="<?php echo $item_id ?>" /> <br /> <input type="submit" name="submit" value="UPDATE" /> </form> <br /><br /> <br /> <?php echo $display_block = "<a href=\"add_new_product4.php?id={$item_id}username=".$_SESSION['username']."\">Add New Image</a>" ?></div> <div class="footergallery"><br /> <br /> Contact About Terms of Use Privacy Policy Copyright Policy </div> </div> </body></div> </html> Here is the php form action code: <?php session_start ()?> <?php mysql_connect("localhost", "", "", "store") or die(mysql_error()); mysql_select_db("store") or die(mysql_error()); $item_title = mysql_real_escape_string($_POST[title]); $item_description = mysql_real_escape_string($_POST[description]); $item_price = mysql_real_escape_string($_POST[price]); $item_location = mysql_real_escape_string($_POST[location]); $item_ship_to = mysql_real_escape_string($_POST[ship_to]); $item_shipping_cost = mysql_real_escape_string($_POST[shipping_cost]); $item_international = mysql_real_escape_string($_POST[international]); $item_in_shipping = mysql_real_escape_string($_POST[in_shipping]); $item_id = mysql_real_escape_string($_POST[id]); $username = $_SESSION['username']; $sql="UPDATE product SET title='$item_title', description='$item_description', price='$item_price', location='$item_location', ship_to='$item_ship_to', shipping_cost='$item_shipping_cost', international='$item_international', in_shipping='$item_in_shipping' WHERE id = $item_id"; if (!mysql_query($sql)) { die('Error: ' . mysql_error()); }else { header("location:add_new_product3.php?id={$item_id}"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/206943-not-updating-correctly/#findComment-1082588 Share on other sites More sharing options...
marcus Posted July 7, 2010 Share Posted July 7, 2010 You just need to set the action of the form to act with the item_id... <form action="submit.php?id=<?php echo $variable_of_id_being_passed_in_previous_form; ?>"> Quote Link to comment https://forums.phpfreaks.com/topic/206943-not-updating-correctly/#findComment-1082599 Share on other sites More sharing options...
scmeeker Posted July 7, 2010 Author Share Posted July 7, 2010 I also tested the second page (edit/update information) of the form by trying to echo the ID to a place onto the next page (image upload page) and nothing came up so that shows the variable is not being passed correctly. Strange though how it shows up in the URL. Quote Link to comment https://forums.phpfreaks.com/topic/206943-not-updating-correctly/#findComment-1082615 Share on other sites More sharing options...
Pikachu2000 Posted July 7, 2010 Share Posted July 7, 2010 If it's in the URL, it's being passed. It just isn't being accessed correctly. A var passed in the URL is accessed with $_GET['var'], not $_POST['var']. In the script, you're using GET to pass the id, but POST for everything else, except the username, for which you're using SESSIONs. You have to use the proper method to access the variable you need. In the form code above, you need to pass the id via the url again to the form processing script. The <form> tag should be: <form action="update_new.php?id="<?php echo $item_id; ?> method="post" enctype="multipart/form-data" name="update_new" id="update_new"> In the script that processes it, you need to change the assignment to $item_id = mysql_real_escape_string($_GET[id]); Then each subsequent script that need the id needs to access it from $_GET['id'], and pass it along via the URL. Quote Link to comment https://forums.phpfreaks.com/topic/206943-not-updating-correctly/#findComment-1082627 Share on other sites More sharing options...
scmeeker Posted July 7, 2010 Author Share Posted July 7, 2010 When I tried this suggestion: In the script that processes it, you need to change the assignment to $item_id = mysql_real_escape_string($_GET[id]); it came back with this error: Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 3 Quote Link to comment https://forums.phpfreaks.com/topic/206943-not-updating-correctly/#findComment-1082759 Share on other sites More sharing options...
scmeeker Posted July 7, 2010 Author Share Posted July 7, 2010 Okay, fixed that. But, now the id doesn't show up in the URL string with those changes. Hmmm...?? Quote Link to comment https://forums.phpfreaks.com/topic/206943-not-updating-correctly/#findComment-1082767 Share on other sites More sharing options...
scmeeker Posted July 7, 2010 Author Share Posted July 7, 2010 Hallelujah! I finally got it!! Yes...it works! Thank you everyone for your help!!!!! Quote Link to comment https://forums.phpfreaks.com/topic/206943-not-updating-correctly/#findComment-1082782 Share on other sites More sharing options...
Pikachu2000 Posted July 8, 2010 Share Posted July 8, 2010 Oh, good. I was just taking another look through it. What did you find? Quote Link to comment https://forums.phpfreaks.com/topic/206943-not-updating-correctly/#findComment-1082795 Share on other sites More sharing options...
scmeeker Posted July 8, 2010 Author Share Posted July 8, 2010 What I did was decided to create a blank test page to pass the information to until I could get it to work. Then, when I finally did get it to pass the id, I did the same for the image upload page. With a combination of everything you said and a little tweaking...I got it to work. Quote Link to comment https://forums.phpfreaks.com/topic/206943-not-updating-correctly/#findComment-1082810 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.