Jump to content

[SOLVED] Image Upload store reference in mysql db


gwood_25

Recommended Posts

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>

 

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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">

Link to comment
Share on other sites

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>

 

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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" />

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.