Uploaded File Name Into MySQL Table

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
froppo
Forum Newbie
Posts: 1
Joined: Fri Mar 25, 2011 5:01 pm

Uploaded File Name Into MySQL Table

Post by froppo »

Hello All,

I wrote a PHP file upload script and I'm trying to figure out how I can get the uploaded file name to be inserted into the MySQL table I created. I've tried several things but nothing works. This is basically an application form I'm working on where users input their info and upload a file.

I'm able to get all of the application info the user submits to the MySQL Table however the uploaded filename isn't getting inserted. Here is a stripped down example of what I have:

Code: Select all

<?php 

$target_path = "Images/";
$target_path = $target_path . basename($name = $_FILES['pic']['name']);
$first_name=$_POST['first_name'];  
$last_name=$_POST['last_name'];  
$middle_init=$_POST['middle_init'];

mysql_connect("localhost", "My_Username", "My_Password") or die(mysql_error()) ;  

mysql_select_db("My_Database") or die(mysql_error()) ;  

if (!$_POST['first_name'] | !$_POST['last_name'] | !$_POST['middle_init']) {

header('Location:http://www.mysite.com/error.html');

die();

	}

$insert = "INSERT INTO image (first_name, last_name, middle_init, name)

 
			VALUES ('".$_POST['first_name']."', '".$_POST['last_name']."', '".$_POST['middle_init']."', '".$_POST['name']."')";

	$add_member = mysql_query($insert);

move_uploaded_file($_FILES['pic']['tmp_name'], $target_path)

?>
Any help you could give would be greatly appreciated! I've posted this question on a few other forums but to no avail. :banghead: FYI this doesn't necessarily need to be a perfectly secure code...once I can get it to work the way I need it to then I'll worry about revamping it.

Thanks in advance for your help!
miramichiphoto
Forum Newbie
Posts: 14
Joined: Thu Mar 17, 2011 1:12 pm

Re: Uploaded File Name Into MySQL Table

Post by miramichiphoto »

Have you tried first declaring the $name variable outside the basename() function ?
Post Reply