Jump to content

Saving an image file to MySQL database


lauren_etherington
Go to solution Solved by Barand,

Recommended Posts

Hello PHPer's

I am having a few problems with my code.

I've probably missed something but could do with an outsiders opinion.

 

Basically, I am writing a bit of code where the user can enter some information and an image into a form and save it to the mysql database.

For the most part, the code works, If I don't add the image then the content is all saved.

However, when I do add an image I am just given my predefined error message from the code.

The php debugger is not much use as the connection is for a local host which is stored on a server and not on my PC....

 

This is the code:

$auth = $_POST['auth'];
$tit = $_POST['tit'];
$band = $_POST['band'];
$alb = $_POST['alname'];
$rel = $_POST['release'];
$stat = $_POST['stat'];
$shrt = $_POST['short'];
$art = $_POST['art'];


$conn = mysqli_connect("localhost","") or die ("Could not connect to database");

if(!is_uploaded_file($_FILES['file']['tmp_name']))
{
		$query = "INSERT INTO albumreviews (author,title,band,albumname,releasedate,shortdesc,article,albumdate,status) VALUES ('$auth','$tit','$band','$alb','$rel','$shrt','$art',CURDATE(),'$stat')";
	//echo "$naquery";
}
else
{
	if ($_FILES['file']['type'] != "image/gif"  && $_FILES['file']['type'] != "image/jpeg"  && $_FILES['file']['type'] != "image/jpg"  && $_FILES['file']['type'] != "image/x-png"  && $_FILES['file']['type'] != "image/png")
	{
			$query = "INSERT INTO albumreviews (author,title,band,albumname,releasedate,shortdesc,article,albumdate,status) VALUES ('$auth','$tit','$band','$alb','$rel','$shrt','$art',CURDATE(),'$stat')";
		//echo "$naquery";
	}
	else
	{
		$finame = $_FILES["file"]["name"];
		//$ext = end(explode(".", $finame));
		$result = move_uploaded_file($_FILES['file']['tmp_name'], "../includes/$finame");
		if ($result == 1)
		{
				$query = "INSERT INTO albumreviews (author,title,band,albumname,releasedate,shortdesc,article,albumdate,status,image) VALUES ('$auth','$tit','$band','$alb','$rel','$shrt','$art',CURDATE(),'$stat''$finame'))";
						//echo "$naquery";
		}
		else
		{
			$query = "INSERT INTO albumreviews (author,title,band,albumname,releasedate,shortdesc,article,albumdate,status) VALUES ('$auth','$tit','$band','$alb','$rel','$shrt','$art',CURDATE(),'$stat')";
			//echo "$naquery";
		}
	}
}

$result = mysqli_query($conn, $query);

if($result){

	echo "successful";
	echo "<BR>";
	echo "<a href='http://'>Back to Content Management </a>";

		}

else {
	echo "error could not upload article";
	echo "<BR>";
	echo "<a href='http://'>Back to Content Management </a>";
}
mysqli_close($conn);
?>

Any help on getting this to upload the image files would be much appreciated.

Thanks :D

Edited by lauren_etherington
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.