gwood_25 Posted October 11, 2007 Share Posted October 11, 2007 Hello, first I want to thank you guys for all of your help. I am new to php and I am having a problem uploading an image and storing a reference to the image name in a mysql database. I know my code may not be the best but if you could tell me what is wrong it would be greatly appreciated. The problem I am having, is that the image is never uploaded and no error is generated. Also the record is never inserted into the databae either. What am I doing wrong? here is my code..... also, the very bottom line which displays a link, never gets rendered to the browser leading me to believe there is an error but I can't see what it is.... I am hosting this site on yahoo servers and am not sure if perhaps there is a permissions issue on the target forlders? But why won't the query insert records into the database either? <?PHP header("Cache-Control: no-cache, must-revalidate"); include '../connection.php'; //This gets the information from the form $name=$_POST['Name']; $weblink=$_POST['Weblink']; $pic=($_FILES['Image']['name']); if($weblink = "") { $weblink = "none" } //We are uploading a picture if($pic != "") { //This is the directory where images will be saved $target = "../../brand_images/"; $target = $target . basename( $_FILES['Image']['name']); //Writes the information to the database mysql_query("INSERT INTO `brands` (Name, Weblink, Image) VALUES ('$name', '$weblink', '$pic')") or die(mysql_error()); //Writes the photo to the server if(move_uploaded_file($_FILES['Image']['tmp_name'], $target)) { //Tells you if its all ok echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; } else { //Gives and error if its not echo "Sorry, there was a problem uploading your file."; } } else //No picture being uploaded { //Writes the information to the database mysql_query("INSERT INTO `brands` (Name, Weblink, Image) VALUES ('$name', '$weblink', 'noimage.png')") or die(mysql_error()); } mysql_close($dbh); ?> <br /> <center><a href="brand_list.php">Back</a></center> Quote Link to comment https://forums.phpfreaks.com/topic/72807-solved-image-upload-store-reference-in-mysql-db/ Share on other sites More sharing options...
~n[EO]n~ Posted October 11, 2007 Share Posted October 11, 2007 Check this... <?php header("Cache-Control: no-cache, must-revalidate"); include '../connection.php'; //This gets the information from the form $name=$_POST['Name']; $weblink=$_POST['Weblink']; $pic=$_FILES['Image']['name']; if($weblink = "") { $weblink = "none" } //We are uploading a picture if($pic != "") { //This is the directory where images will be saved // how many folders up are you going... $target = "brand_images"; $target = $target . basename( $_FILES['Image']['name']); /////////////////////////////////////////////////////// $strfilepath=move_uploaded_file($_FILES['Image']['tmp_name'],"../../".$target); $strfile=$filePathgif; $fileUploaded = TRUE; ///////////////////////////////////////////////////// //Writes the information to the database mysql_query("INSERT INTO `brands` (Name, Weblink, Image) VALUES ('$name', '$weblink', '$strfile')") or die(mysql_error()); //Writes the photo to the server if($fileUploaded = TRUE) { //Tells you if its all ok echo "The file has been uploaded, and your information has been added to the directory"; } else { //Gives and error if its not echo "Sorry, there was a problem uploading your file."; } } //////////////CHECK HERE ////////////////////// /* you don't have to save other picture path in the db, while fetching in front end you can check if the path is empty or not and if it is empty you can show this noimage.png so this query will not be needed .... */ else //No picture being uploaded { //Writes the information to the database mysql_query("INSERT INTO `brands` (Name, Weblink, Image) VALUES ('$name', '$weblink', 'noimage.png')") or die(mysql_error()); } mysql_close($dbh); ?> <center><a href="brand_list.php">Back</a></center> Quote Link to comment https://forums.phpfreaks.com/topic/72807-solved-image-upload-store-reference-in-mysql-db/#findComment-367193 Share on other sites More sharing options...
gwood_25 Posted October 11, 2007 Author Share Posted October 11, 2007 I removed that second query but still nothing gets inserted into the db and no error is generated....the page is blank after the script is completed. Also, the very last line never gets rendered to the browser Quote Link to comment https://forums.phpfreaks.com/topic/72807-solved-image-upload-store-reference-in-mysql-db/#findComment-367205 Share on other sites More sharing options...
~n[EO]n~ Posted October 11, 2007 Share Posted October 11, 2007 I don't see your form post code, where is the code for post <?php // here sub is the name of the submit button if(isset($_REQUEST['sub'])) // or you can write this one if($_SERVER['REQUEST_METHOD'] == 'POST') ?> And check your form too... <form name="frm" method="post" enctype="multipart/form-data"> Quote Link to comment https://forums.phpfreaks.com/topic/72807-solved-image-upload-store-reference-in-mysql-db/#findComment-367212 Share on other sites More sharing options...
gwood_25 Posted October 11, 2007 Author Share Posted October 11, 2007 Here is the form page code.... <form name=form1 enctype="multipart/form-data" action="add.php" method="POST" onsubmit="return frmValidate();"> <table align=center> <tr><td> <div class="box"> <div class="bi"> <div class="bt"><div></div></div> <div class="left"><h1>Add Brand Record</h1></div> <p> <table align=center> <tr> <td>Name:</td> <td><input type=text name=Name style="width:300" maxlength:200 /></td> </tr> <tr> <td>Weblink:</td> <td><input type=text name=Weblink style="width:300" maxlength:200 /></td> </tr> <tr> <td>Image:</td> <td><input type=file name=Image style="width:300" /></td> </tr> </table> </p> <div class="bb"><div></div></div> </div> </div> </td></tr> <tr> <td align=right> <input type=button value="Cancel" class=btn onClick="document.location.href='brand_list.php'" /> <input type=submit value="Save" class="btn" /> </td> </tr> </table> </form> Quote Link to comment https://forums.phpfreaks.com/topic/72807-solved-image-upload-store-reference-in-mysql-db/#findComment-367220 Share on other sites More sharing options...
gwood_25 Posted October 11, 2007 Author Share Posted October 11, 2007 not sure if this helps but here is the directory structure.... the scripts to upload and add to db are located here... root/admin/brands/add.php the images are being uploaded to here.... root/brand_images/ There seems to be something wrong with the query as even when I only try to insert a record....using only the insert query...same result...blank page and bottom link doesn't get rendered I'm sure there is something very simple that I am doing. Also important to note that in the same brand directory is a script that uses the same connection include and it connects to the db and displays a listing of brands without a problem. Quote Link to comment https://forums.phpfreaks.com/topic/72807-solved-image-upload-store-reference-in-mysql-db/#findComment-367224 Share on other sites More sharing options...
~n[EO]n~ Posted October 11, 2007 Share Posted October 11, 2007 Try this <?php header("Cache-Control: no-cache, must-revalidate"); include '../connection.php'; //This gets the information from the form $name=$_POST['Name']; $weblink=$_POST['Weblink']; $pic=$_FILES['Image']['name']; if($weblink = "") { $weblink = "none" } //We are uploading a picture if(isset($_REQUEST["sub"])) { if($pic != "") { //This is the directory where images will be saved // how many folders up are you going... $target = "brand_images"; $target = $target . basename( $_FILES['Image']['name']); /////////////////////////////////////////////////////// $strfilepath=move_uploaded_file($_FILES['Image']['tmp_name'],"../../".$target); $strfile=$filePathgif; $fileUploaded = TRUE; ///////////////////////////////////////////////////// //Writes the information to the database mysql_query("INSERT INTO `brands` (Name, Weblink, Image) VALUES ('$name', '$weblink', '$strfile')") or die(mysql_error()); //Writes the photo to the server if($fileUploaded = TRUE) { //Tells you if its all ok echo "The file has been uploaded, and your information has been added to the directory"; } else { //Gives and error if its not echo "Sorry, there was a problem uploading your file."; } } } mysql_close($dbh); ?> And in your input button keep this <input type="submit" name="sub" value="Save" class="btn" /> Quote Link to comment https://forums.phpfreaks.com/topic/72807-solved-image-upload-store-reference-in-mysql-db/#findComment-367230 Share on other sites More sharing options...
gwood_25 Posted October 11, 2007 Author Share Posted October 11, 2007 ok I tried the code you provided...same result. blank page and bottom link is not rendered. How can I output an error message if any to see where the problem is? Quote Link to comment https://forums.phpfreaks.com/topic/72807-solved-image-upload-store-reference-in-mysql-db/#findComment-367246 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.