Image uploading to directory but file path not uploading to database

I created a database with 2 fields

Id INT(11) Primary Auto_Increment not null
photo Text Null

The code below successfully uploads the image to the directory however the image path doesn’t save to my database. Does anybody have any idea what im doing wrong. Sorry if it is something obvious im a php beginner and im new to uploading images with php. Thanks.

<?php $pic=($_FILES['photo']['name']); if((!empty($_FILES['photo'])) && ($_FILES['photo']['error'] == 0)) { $filename = basename($_FILES['photo']['name']); $ext =substr($filename, strrpos($filename, '.') + 1); if (($ext == "jpg" || $ext == "JPG" || $ext == "jpeg" || $ext == "JPEG" || $EXT == "gif" || $ext =="GIF" || $ext =="PNG" || $ext =="png") && ($_FILES['photo']['size'] <1000000)) { $newname = dirname(__FILE__).'/images/'.$filename; if (!file_exists($newname)) { if ((move_uploaded_file($_FILES['photo']['tmp_name'],$newname))) { echo "The file has been saved as: ".$newname; } else { echo "Error file not uploaded"; } }else { echo "Error: File ".$_FILES["photo"]["name"]." already exists"; } } else { echo "Error only jpg png and gif under 1mb are accepted"; } } else { echo "Image uploaded"; } mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("accounts") or die(mysql_error()); mysql_query("INSERT INTO `post_photo`(`photos`) VALUES ([value-2])"); ?>

He’s a tutorial on how to accomplish what you want to do.

http://www.phpro.org/tutorials/Storing-Images-in-MySQL-with-PHP.html

[php]mysql_query(“INSERT INTO post_photo (photos) VALUE ($newname)”);[/php]

Now…

First and foremost, if you are just beginning, STOP!
Go back to the docs and learn mysqli.
(PDO is also an option and down to personal preference…)

Stop copy/pasting code! You’ll learn quicker if you type it each and every time - yes I know it’s boring but it’s the best way in the long term.

Don’t allow files to uploaded without some form of checks being done on them beforehand.

Hope that helps,
Red :wink:

Sponsor our Newsletter | Privacy Policy | Terms of Service