Jump to content

Saving an image file to MySQL database


lauren_etherington

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

Link to comment
https://forums.phpfreaks.com/topic/285963-saving-an-image-file-to-mysql-database/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.