Techmate Posted August 8, 2011 Share Posted August 8, 2011 After the image is created I need to upload it into the database. I saves it into a subdirectory I just need it to go from the subdirectory to the database. Got stuck here and can not wrap my head around it. <?php ini_set('display_errors', 1 ); //upload image $to = $_FILES['image']['name']; $where = $_FILES['image']['tmp_name']; //move uploaded image move_uploaded_file($where, "images/$to"); // call function mark("images/$to"); //add mark()here later function mark($location){ $watermark = imagecreatefromgif("watermark.gif"); $watermarkheight = imagesy($watermark); //height of watermark $watermarkwidth = imagesx($watermark); //width of watermark $image = imagecreatetruecolor($watermarkwidth, $watermarkheight); $image = imagecreatefromjpeg($location); // get image size $size = getimagesize($location); $xpos = $size[0] - $watermarkwidth - 10; //10px away from right $ypos = $size[1] - $watermarkheight - 10; //10px away from bottom // merge watermark with original image imagecopymerge($image, $watermark, $xpos, $ypos, 0, 0, $watermarkwidth, $watermarkheight, 100); // save watermarked image imagejpeg($image, $location); //clearing buffer imagedestroy($image); imagedestroy($watermark); } echo "<img src='images/$to'>"; ?> <form action="watermark.php" method ="POST" enctype="multipart/form-data"> <input type="file" name="image"> <input type="submit" value="Watermark"> </form> Thank you in advance. Quote Link to comment https://forums.phpfreaks.com/topic/244167-need-help-uploading-image-into-database/ Share on other sites More sharing options...
trq Posted August 8, 2011 Share Posted August 8, 2011 Can you at least post the relevant code that you have tried? Quote Link to comment https://forums.phpfreaks.com/topic/244167-need-help-uploading-image-into-database/#findComment-1253945 Share on other sites More sharing options...
Techmate Posted August 8, 2011 Author Share Posted August 8, 2011 Can you at least post the relevant code that you have tried? Sure it is rather sorry . Complete script not working: <html> <head> <body> <form action:"imguploader.php" method ="POST" enctype="multipart/form-data"> File: <input type="file" name="image"><input type="submit" value="upload"> </form> <?php $dbc=$_GET['dbchange']; $tablec = $dbc; $tablecl = array(); $tablecl[0] = '/_/'; $replace[0] = ' '; $remix = preg_replace($tablecl,$replace, $tablec); // connect to database //database connection stuff I removed it for now //error(); //uncomment to check for errors in the script some errors my only occur during file ch $result = mysql_query ("SELECT position FROM images ORDER BY position DESC LIMIT 1 "); // Generate the liks from user created galleries from db while($row = mysql_fetch_array($result)){ $name = $row['position']; } $name2 = ++$name; $lastid = mysql_insert_id(); $lastidp = ++$lastid; //file properties $file =$_FILES['image'] ['tmp_name']; $checkit = $_FILES['image']['name']; $result = mysql_query ("SELECT name FROM images"); // Generate the liks from user created galleries from db while($row = mysql_fetch_array($result)){ $name = $row['name']; // Check if image excists or not if ($name == $checkit){ die ("$checkit already excists please delete it or change the files name."); } else { } } if (!isset($file)) echo "Please select a image to upload into category: <font color=\"#ff0000\">$remix</font>"; else{ $to = $_FILES['image']['name']; $where = $_FILES['image']['tmp_name']; //move uploaded image move_uploaded_file($where, "images/$to"); // call function mark("images/$to"); //add mark()here later function mark($location){ $watermark = imagecreatefromgif("watermark.gif"); $watermarkheight = imagesy($watermark); //height of watermark $watermarkwidth = imagesx($watermark); //width of watermark $image = imagecreatetruecolor($watermarkwidth, $watermarkheight); $image = imagecreatefromjpeg($location); // get image size $size = getimagesize($location); $xpos = $size[0] - $watermarkwidth - 10; //10px away from right $ypos = $size[1] - $watermarkheight - 10; //10px away from bottom // merge watermark with original image imagecopymerge($image, $watermark, $xpos, $ypos, 0, 0, $watermarkwidth, $watermarkheight, 100); // save watermarked image imagejpeg($image, $location); //clearing buffer imagedestroy($image); imagedestroy($watermark); //echo "<img src='images/$to'>"; //$galleryids= $_POST['galleryid']; if ($image_size == FALSE) echo "That's not an image"; else { if (!$insert = mysql_query("INSERT INTO images VALUES ('','$to','$where','$name2')")) echo "Problem uploading image. Contact web administrator."; else{ $lastid= mysql_insert_id(); echo "$image_name uploaded into $remix Gallery. <p / > <p/> <img src=get.php?id=$lastid&dbchange=$dbc&cat=$image_name>"; } } } } //var_dump($_FILES); ?> </body> </head> </html> Quote Link to comment https://forums.phpfreaks.com/topic/244167-need-help-uploading-image-into-database/#findComment-1253963 Share on other sites More sharing options...
phpSensei Posted August 8, 2011 Share Posted August 8, 2011 Hi There, You havn't specified WHICH fields you want to insert what into if (!$insert = mysql_query("INSERT INTO images VALUES ('','$to','$where','$name2')")) Here's an example of INSERT INSERT INTO tbl_name (field1,field2,field2) VALUES ('a','b','c') Also this line checks if the variable has been set, but for security reasons please also check if its empty or not if (!isset($file)) to if ((!isset($file)) || ($file == "")) Quote Link to comment https://forums.phpfreaks.com/topic/244167-need-help-uploading-image-into-database/#findComment-1253997 Share on other sites More sharing options...
Techmate Posted August 8, 2011 Author Share Posted August 8, 2011 Hi There, You havn't specified WHICH fields you want to insert what into if (!$insert = mysql_query("INSERT INTO images VALUES ('','$to','$where','$name2')")) Here's an example of INSERT INSERT INTO tbl_name (field1,field2,field2) VALUES ('a','b','c') Also this line checks if the variable has been set, but for security reasons please also check if its empty or not if (!isset($file)) to if ((!isset($file)) || ($file == "")) I checked it over and I think it is fine but based on my script can you revise it how it should be? Quote Link to comment https://forums.phpfreaks.com/topic/244167-need-help-uploading-image-into-database/#findComment-1254001 Share on other sites More sharing options...
phpSensei Posted August 8, 2011 Share Posted August 8, 2011 My pleasure to help, Can you simply post the structure of your table? Quote Link to comment https://forums.phpfreaks.com/topic/244167-need-help-uploading-image-into-database/#findComment-1254004 Share on other sites More sharing options...
Techmate Posted August 8, 2011 Author Share Posted August 8, 2011 The tables consist of id auto incremented name, image blob, position. Thank you again for your help Quote Link to comment https://forums.phpfreaks.com/topic/244167-need-help-uploading-image-into-database/#findComment-1254005 Share on other sites More sharing options...
phpSensei Posted August 8, 2011 Share Posted August 8, 2011 Oh okay, so those are the names, sorry about that.. When you say IMAGE BLOB, is that the field name or field type... Quote Link to comment https://forums.phpfreaks.com/topic/244167-need-help-uploading-image-into-database/#findComment-1254012 Share on other sites More sharing options...
Techmate Posted August 8, 2011 Author Share Posted August 8, 2011 Oh okay, so those are the names, sorry about that.. When you say IMAGE BLOB, is that the field name or field type... No worries IMAGE BLOB is the field type.. Quote Link to comment https://forums.phpfreaks.com/topic/244167-need-help-uploading-image-into-database/#findComment-1254015 Share on other sites More sharing options...
phpSensei Posted August 8, 2011 Share Posted August 8, 2011 Alright what is the NAME of the image field, not the type.. here how you would do it, I just need the image fields name also.. if (!$insert = mysql_query("INSERT INTO images (`image`,`position`,`name`) VALUES ('$to','$where','$name2')")) Quote Link to comment https://forums.phpfreaks.com/topic/244167-need-help-uploading-image-into-database/#findComment-1254018 Share on other sites More sharing options...
Techmate Posted August 8, 2011 Author Share Posted August 8, 2011 In this order: ________________________ |id | name | image | position| ________________________ Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/244167-need-help-uploading-image-into-database/#findComment-1254024 Share on other sites More sharing options...
phpSensei Posted August 8, 2011 Share Posted August 8, 2011 Yup, I had it right the first type, but Techmate, do you now understand how INSERT works? Quote Link to comment https://forums.phpfreaks.com/topic/244167-need-help-uploading-image-into-database/#findComment-1254029 Share on other sites More sharing options...
Techmate Posted August 8, 2011 Author Share Posted August 8, 2011 Yup, I had it right the first type, but Techmate, do you now understand how INSERT works? I thought I did lol. I tried your insert and nothing inserted? Is that weird? Quote Link to comment https://forums.phpfreaks.com/topic/244167-need-help-uploading-image-into-database/#findComment-1254042 Share on other sites More sharing options...
trq Posted August 8, 2011 Share Posted August 8, 2011 You do not need to specify the names of the fields if you have a value set for all of them. The first query looked fine. op: What error are you getting? Quote Link to comment https://forums.phpfreaks.com/topic/244167-need-help-uploading-image-into-database/#findComment-1254044 Share on other sites More sharing options...
Techmate Posted August 8, 2011 Author Share Posted August 8, 2011 You do not need to specify the names of the fields if you have a value set for all of them. The first query looked fine. op: What error are you getting? Well I would like to make it work with this basic piece of code first because I know it puts my watermark on my image but it does not upload it into the database when I add the my_query string: <?php $dbc=$_GET['dbchange']; include ("../confffee.php"); confff("$dbc"); ini_set('display_errors', 1 ); //error(); //uncomment to check for errors in the script some errors my only occur during file ch $result = mysql_query ("SELECT position FROM images ORDER BY position DESC LIMIT 1 "); // Generate the liks from user created galleries from db while($row = mysql_fetch_array($result)){ $name = $row['position']; } $name2 = ++$name; //upload image $to = $_FILES['image']['name']; $where = $_FILES['image']['tmp_name']; //move uploaded image move_uploaded_file($where, "images/$to"); // call function mark("images/$to"); //add mark()here later function mark($location){ $watermark = imagecreatefromgif("watermark.gif"); $watermarkheight = imagesy($watermark); //height of watermark $watermarkwidth = imagesx($watermark); //width of watermark $image = imagecreatetruecolor($watermarkwidth, $watermarkheight); $image = imagecreatefromjpeg($location); // get image size $size = getimagesize($location); $xpos = $size[0] - $watermarkwidth - 10; //10px away from right $ypos = $size[1] - $watermarkheight - 10; //10px away from bottom // merge watermark with original image imagecopymerge($image, $watermark, $xpos, $ypos, 0, 0, $watermarkwidth, $watermarkheight, 100); // save watermarked image imagejpeg($image, $location); //clearing buffer imagedestroy($image); imagedestroy($watermark); mysql_query("INSERT INTO images VALUES ('','$to','$where','$name2')")) } echo "<img src='images/$to'>"; ?> <form action="watermark.php?dbchange=Picuters" method ="POST" enctype="multipart/form-data"> <input type="file" name="image"> <input type="submit" value="Watermark"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/244167-need-help-uploading-image-into-database/#findComment-1254050 Share on other sites More sharing options...
phpSensei Posted August 8, 2011 Share Posted August 8, 2011 Heres the updated code for debugging, let me know the error <html> <head> <body> <form action:"imguploader.php" method ="POST" enctype="multipart/form-data"> File: <input type="file" name="image"><input type="submit" value="upload"> </form> <?php $dbc=$_GET['dbchange']; ini_set("display_errors","1"); error_reporting(E_ALL); $tablec = $dbc; $tablecl = array(); $tablecl[0] = '/_/'; $replace[0] = ' '; $remix = preg_replace($tablecl,$replace, $tablec); // connect to database //database connection stuff I removed it for now //error(); //uncomment to check for errors in the script some errors my only occur during file ch $result = mysql_query ("SELECT position FROM images ORDER BY position DESC LIMIT 1 ") or die(mysql_error()); // Generate the liks from user created galleries from db while($row = mysql_fetch_array($result)){ $name = $row['position']; } $name2 = ++$name; $lastid = mysql_insert_id(); $lastidp = ++$lastid; //file properties $file =$_FILES['image'] ['tmp_name']; $checkit = $_FILES['image']['name']; $result = mysql_query ("SELECT name FROM images"); // Generate the liks from user created galleries from db while($row = mysql_fetch_array($result)){ $name = $row['name']; // Check if image excists or not if ($name == $checkit){ die ("$checkit already excists please delete it or change the files name."); } else { } } if (!isset($file)) echo "Please select a image to upload into category: <font color=\"#ff0000\">$remix</font>"; else{ $to = $_FILES['image']['name']; $where = $_FILES['image']['tmp_name']; //move uploaded image move_uploaded_file($where, "images/$to"); // call function mark("images/$to"); //add mark()here later function mark($location){ $watermark = imagecreatefromgif("watermark.gif"); $watermarkheight = imagesy($watermark); //height of watermark $watermarkwidth = imagesx($watermark); //width of watermark $image = imagecreatetruecolor($watermarkwidth, $watermarkheight); $image = imagecreatefromjpeg($location); // get image size $size = getimagesize($location); $xpos = $size[0] - $watermarkwidth - 10; //10px away from right $ypos = $size[1] - $watermarkheight - 10; //10px away from bottom // merge watermark with original image imagecopymerge($image, $watermark, $xpos, $ypos, 0, 0, $watermarkwidth, $watermarkheight, 100); // save watermarked image imagejpeg($image, $location); //clearing buffer imagedestroy($image); imagedestroy($watermark); $insert = mysql_query("INSERT INTO ('image','position','name') VALUES ('$to','$where','$name2')") or die(mysql_error()); //echo "<img src='images/$to'>"; //$galleryids= $_POST['galleryid']; if ($image_size == FALSE) echo "That's not an image"; else { if (!$insert) echo "Problem uploading image. Contact web administrator."; else{ $lastid= mysql_insert_id(); echo "$image_name uploaded into $remix Gallery. <p / > <p/> <img src=get.php?id=$lastid&dbchange=$dbc&cat=$image_name>"; } } } } //var_dump($_FILES); ?> </body> </head> </html> Quote Link to comment https://forums.phpfreaks.com/topic/244167-need-help-uploading-image-into-database/#findComment-1254054 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.