|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Need help with image upload
Hi
Im trying to upload and update the image after uploading to the database... but i can only be able to upload the image and am not able to update the image... can anyone help me out with this ... the full code is pasted below: <? include("../application.php"); require_login(); require_priv("admin"); include("template/header.php"); switch (nvl($mode)) { case "add" : print_add_product_form(nvl($category_id, 0)); break; case "edit" : print_edit_product_form($id); break; case "del" : delete_product($id); print_product_list(); break; case "insert" : insert_product($id, $_POST, $HTTP_POST_FILES); print_product_list(); break; case "update" : update_product($id, $_POST, $HTTP_POST_FILES); print_product_list(); break; case "recomend": recommend_product($_POST); break; case "del_recom": delete_recommended_items($HTTP_GET_VARS); break; case "add_opt": add_options($_POST); break; case "modify_opt": modify_options($_POST); break; case "del_opt": del_options($HTTP_GET_VARS); break; default : print_product_list(); break; } include("template/footer.php"); function print_add_product_form($category_id = 0) { global $CFG, $ME; $frm["categories"] = array($category_id); $frm["name"] = ""; $frm["description"] = ""; $frm["brief_desc"] = ""; $frm["price"] = ""; $frm["weight"] = ""; $frm["stock"] = ""; $frm["on_special"] = ""; $frm["image_file"] = ""; $frm["product_code"] = ""; $frm["newmode"] = "insert"; $frm["submit_caption"] = "Add Product"; build_category_tree($category_options, $frm["categories"]); include("template/product_form.php"); } function print_edit_product_form($id) { global $CFG, $ME; $qid = db_query(" SELECT * FROM products WHERE id = $id "); $frm = db_fetch_array($qid); $qid = db_query(" SELECT category_id FROM products_categories WHERE product_id = $id "); $frm["categories"] = array(); while ($cat = db_fetch_object($qid)) { $frm["categories"][] = $cat->category_id; } /* load product options */ $qid_op = db_query("SELECT * FROM product_options where productid ='$id'"); /* set values for the form */ $frm["newmode"] = "update"; $frm["submit_caption"] = "Save Changes"; /* build the categories listbox options, preselect the selected item */ build_category_tree($category_options, $frm["categories"]); include("template/product_form.php"); } function delete_product($id) { global $CFG, $ME; /* load up the information for the product */ $qid = db_query(" SELECT name FROM products WHERE id = $id "); $prod = db_fetch_object($qid); /* delete this product */ $qid = db_query(" DELETE FROM products WHERE id = $id "); $qid = db_query(" DELETE FROM products_categories WHERE product_id = $id "); include("template/product_deleted.php"); } function insert_product($id, $frm,$form) { global $CFG, $ME; $on_special = checked($frm["on_special"]); $date = date("Y-m-d"); $qid = db_query(" INSERT INTO products (product_code, name,brief_desc, description, price, stock,weight, on_special, status, dateadded) VALUES ('$frm[product_code]', '$frm[name]', '$frm[brief_desc]','$frm[description]','$frm[price]','$frm[stock]','$frm[weight]', '$on_special', '$frm[status]','$date') "); $product_id = db_insert_id(); for ($i = 0; $i < count($frm["categories"]); $i++) { $qid = db_query(" INSERT INTO products_categories (category_id, product_id) VALUES ('{$frm["categories"][$i]}', '$product_id') "); } if(isset($form["userfile"])){ $image = upload_image($form); db_query("INSERT INTO thumbnails(productid, image, image_type) VALUES('$product_id','$image','".$form["userfile"]["type"]."')"); } } function update_product($id, $frm, $form) { global $CFG, $ME; checked($frm["on_special"]); $qid = db_query(" UPDATE products SET product_code = '$frm[product_code]' ,name = '$frm[name]' ,brief_desc = '$frm[brief_desc]' ,description = '$frm[description]' ,price = '$frm[price]' ,on_special = '$frm[on_special]' ,weight = '$frm[weight]' ,stock = '$frm[stock]' ,status = '$frm[status]' WHERE id = $id "); $qid = db_query(" DELETE FROM products_categories WHERE product_id = $id "); if (count($frm["categories"]) == 0) { $frm["categories"][] = 0; } for ($i = 0; $i < count($frm["categories"]); $i++) { $qid = db_query(" INSERT INTO products_categories (category_id, product_id) VALUES ('{$frm["categories"][$i]}', '$id') "); } if(isset($form["userfile"])){ $image = upload_image($form); $qid_im = db_query("SELECT * FROM thumbnails WHERE productid='$id'"); $res = db_num_rows($qid_im); if($res){ db_query("UPDATE thumbnails SET image = '$image' ,image_type ='".$form["userfile"]["type"]."' WHERE productid = '$id' "); }else{ db_query("INSERT INTO thumbnails(productid, image, image_type) VALUES('$id','$image','".$form["userfile"]["type"]."')"); } } } function print_product_list() { global $CFG, $ME; require($CFG->libdir.'/pagedresults.php'); $sql = "SELECT p.id, p.name, p.brief_desc , p.price,p.on_special, p.dateadded, c.name AS category FROM products p, products_categories pc, categories c WHERE p.id = pc.product_id AND c.id = pc.category_id ORDER BY p.dateadded desc "; $rs = new MySQLPagedResultSet($sql,10,$CFG->cnx); include("template/product_list.php"); } function recommend_product($frm){ global $CFG, $ME; if ($frm["selected_productid"] && $frm["productid"]!=$frm["selected_productid"]){ db_query("INSERT INTO product_links(productid1, productid2) VALUES ('$frm[productid]', '$frm[selected_productid]')"); if (isset($frm["bi_directional"])&& $frm["bi_directional"]== "on") db_query("INSERT INTO product_links(productid1, productid2) VALUES ('$frm[selected_productid]', '$frm[productid]')"); } print_edit_product_form($frm["productid"]); } function delete_recommended_items($frm){ db_query("DELETE FROM product_links WHERE productid1='$frm[productid]' AND productid2='$frm[product_link]'"); print_edit_product_form($frm["productid"]); } function add_options($frm){ if ($frm["optclassnew"] && $frm["opttextnew"]) db_query("insert into product_options (productid, optclass, opttext, options) values ('$frm[productid]','$frm[optclassnew]','$frm[opttextnew]','$frm[optionsnew]')"); print_edit_product_form($frm["productid"]); } function del_options($frm){ db_query("delete from product_options where optionid='$frm[optionid]'"); print_edit_product_form($frm["productid"]); } function modify_options($frm){ while(list($key,$val)=each($frm)) { if (strstr($key,"-")) { list($field,$optionid)=split("-",$key); db_query("update product_options set $field='$val' where optionid='$optionid'"); } } print_edit_product_form($frm["productid"]); } function upload_image($frm){ global $CFG, $ME, $image_x, $image_y; $file_temp_dir = "$CFG->imagedir"; if (func_is_image_userfile($frm["userfile"]["tmp_name"], $frm["userfile"]["size"], $frm["userfile"]["type"])) { move_uploaded_file($frm["userfile"]["tmp_name"], $file_temp_dir."/".$frm["userfile"]["name"]); $frm["userfile"]["tmp_name"] = $file_temp_dir."/".$frm["userfile"]["name"]; list($image_size, $image_x, $image_y) = func_get_image_size($frm["userfile"]["tmp_name"]); $fd = fopen($frm["userfile"]["tmp_name"], "rb"); $image = addslashes(fread($fd, $image_size)); fclose($fd); unlink($frm["userfile"]["tmp_name"]); return $image; } } ?> right now using this code im uploading images to database as blogs but if anyone can help me out in uploading to a specific directory it would be much better Regards |
![]() |
| Viewing: Dev Articles Community Forums > Programming > PHP Development > Need help with image upload |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|