Jump to content

image path inserting into database on the same column


gadgetster
Go to solution Solved by gadgetster,

Recommended Posts

im trying to upload a cover image for a posting and it seems to go through but on the database i see one line that shows the path with all other rows empty and another column with all the rows filled out except the image one

 

I have a function that adds the image when called

function add_cover($id, $file_temp, $file_extn){
	$file_path = 'images/cover/' . substr(md5(time()), 0, 10) . '.' . $file_extn;
	move_uploaded_file($file_temp, $file_path);
	mysql_query("UPDATE `classifieds` SET `cover` = '" . mysql_real_escape_string($file_path) . "' WHERE `id` = " . (int)$id);
}

and the the actual php page

if (isset($_FILES['cover']) === true){
			$allowed = array('jpg', 'jpeg', 'gif', 'png');
			$file_name = $_FILES['cover']['name'];
			$file_size = getimagesize($_FILES['cover']['name']);
			$file_extn = strtolower(end(explode('.', $file_name)));
			$file_temp = $_FILES['cover']['tmp_name'];
			if(in_array($file_extn, $allowed) === true){
				add_cover($_SESSION['id'], $file_temp, $file_extn);
			}else{
					$errors[] = 'Incorrect file type. Only allowed: ' . implode(', ', $allowed) . '';										
				}
		}	

it won't work. i know it's probably the something to do with the way i am trying to get $_SESSION['id'] to match the ad id but i don't know how else to call that ad...

any help is appreciated!

 

ps. this is what happens when i use mysql_query("INSERT INTO `classifieds` (`cover`) VALUES ('$file_path')"); instead of the add_cover function:

yzst.jpg

Edited by gadgetster
Link to comment
Share on other sites

Well, have you taken some debugging steps?

 

Strart with php error_reporting functions and mysql_error.

 

So on the top of your application php file put the following

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
ini_set('output_buffering', 'Off');
error_reporting(-1);

And for debugging your query use mysql_error() function

 

Something like

$sql = "UPDATE `classifieds` SET `cover` = '" . mysql_real_escape_string($file_path) . "' WHERE `id` = " . (int)$id;
$result = mysql_query($sql) or die('<pre> SQL: '.$sql .'ERROR: '. mysql_error() . '</pre>' );

Also, show us the string valueof $file_path and post the errors.

Link to comment
Share on other sites

the code you have posted doesn't contain any code that is responsible for the title/price/description data, so it will be really hard to help you with what is wrong with your code.

 

first, you need to define what your intent is. an INSERT query inserts a new row, an UPDATE query allows you to change values in an existing row. what are you trying to do?

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.