Current location: Hot Scripts Forums » Programming Languages » PHP » Upload images via form and insert data into MySQL database


Upload images via form and insert data into MySQL database

Reply
  #1 (permalink)  
Old 03-18-09, 08:45 AM
mike_jandreau mike_jandreau is offline
Newbie Coder
 
Join Date: Jun 2006
Location: Boston
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Upload images via form and insert data into MySQL database

I've never had to do this before, and now that I'm trying, I can't seem to figure it out.

I have a site where users can register, and create a profile. I'm trying to add the ability for them to upload an image of themself, to store into their row in the database, which has a unique ID of "customerid".

I've found a bunch of really cool AJAX uploaders, but none of them seem to insert the data into the MySQL database, they just upload the file to a directory you set, which seems like only half the battle.

Can someone point me in the right direction? I don't necessarily need AJAX, just need to be able to allow people to upload an image, store it in the MySQL database, and then pull that data from the database, to display on their profile page.

Any help would be appreciated.
Reply With Quote
  #2 (permalink)  
Old 03-18-09, 12:05 PM
de.monkeyz's Avatar
de.monkeyz de.monkeyz is offline
Wannabe Coder
 
Join Date: Apr 2008
Location: Leeds, UK
Posts: 116
Thanks: 0
Thanked 0 Times in 0 Posts
To store pure data such as an image into a mysql db, you have to use a BLOB field which means Binary Large OBject I think.

http://www.devarticles.com/c/a/MySQL...PHP-and-MySQL/

This will help you out.
__________________
Wanna learn AJAX? Goto http://www.deathmonkeyz.com/tutorials for free video tutorials.

AJAX - Lesson 11 - AJAX Guestbook (23/8/08)
C++ - Lesson 10 - Classes (24/8/08)
JavaScript - Lesson 03 - The DOM (24/8/08)
Need an AJAX app? Look no further, I'm available for work
Reply With Quote
  #3 (permalink)  
Old 03-19-09, 04:20 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
I'd never store an image into a database, that's complete waste of space. Always store the image in a directory, but store the image information in a database, that way you can always access the image, but don't have to run heavy tasks when getting the image information.
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Reply With Quote
  #4 (permalink)  
Old 03-19-09, 06:57 AM
mike_jandreau mike_jandreau is offline
Newbie Coder
 
Join Date: Jun 2006
Location: Boston
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Unreal Ed, can you point me in the right direction?

The more I read, the more I agree with you. I've figured out how to get the images to upload to the directory I specify, but can't figure out how to insert that filename into the database, in accordance with that specific user's data.
Reply With Quote
  #5 (permalink)  
Old 03-19-09, 02:38 PM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
I suppose only registered users can upload an image, so basically you know how an image is related to the user that uploads the image. You're probably keeping track of the user through sessions as well.

Here's a simple option: after uploading the image, and moving it to the designated directory, you have to store the filename in the database. You already know the filename, as this is either the name of the file on the user's computer (accessible through $_FILES['my_file']['name']), or you created a name through your script (just keep track of it).
Suppose you have a table like this in your database:
Code:
CREATE TABLE `images` (
  `id` MEDIUMINT NOT NULL PRIMARY KEY AUTO_INCREMENT,
  `user_id` MEDIUMINT NOT NULL,
  `image_name` CHAR 32 NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=0;
Now all you have to do is store the user id and the filename into that table, and the job is done. Whenever you feel like grabbing that user's image, you just have to get his id, and there's the filename
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Reply With Quote
  #6 (permalink)  
Old 03-19-09, 06:43 PM
kOe kOe is offline
New Member
 
Join Date: Mar 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
I didnt made this ... cant remember where I got it ...

PHP Code:

<?php
// Start a session for error reporting
session_start();

// Call our connection file
require("includes/conn.php");

// Check to see if the type of file uploaded is a valid image type
function is_valid_type($file)
{
    
// This is an array that holds all the valid image MIME types
    
$valid_types = array("image/jpg""image/jpeg""image/bmp""image/gif");

    if (
in_array($file['type'], $valid_types))
        return 
1;
    return 
0;
}

// Just a short function that prints out the contents of an array in a manner that's easy to read
// I used this function during debugging but it serves no purpose at run time for this example
function showContents($array)
{
    echo 
"<pre>";
    
print_r($array);
    echo 
"</pre>";
}

// Set some constants

// This variable is the path to the image folder where all the images are going to be stored
// Note that there is a trailing forward slash
$TARGET_PATH "images/";

// Get our POSTed variables
$fname $_POST['fname'];
$lname $_POST['lname'];
$image $_FILES['image'];

// Sanitize our inputs
$fname mysql_real_escape_string($fname);
$lname mysql_real_escape_string($lname);
$image['name'] = mysql_real_escape_string($image['name']);

// Build our target path full string.  This is where the file will be moved do
// i.e.  images/picture.jpg
$TARGET_PATH .= $image['name'];

// Make sure all the fields from the form have inputs
if ( $fname == "" || $lname == "" || $image['name'] == "" )
{
    
$_SESSION['error'] = "All fields are required";
    
header("Location: index.php");
    exit;
}

// Check to make sure that our file is actually an image
// You check the file type instead of the extension because the extension can easily be faked
if (!is_valid_type($image))
{
    
$_SESSION['error'] = "You must upload a jpeg, gif, or bmp";
    
header("Location: index.php");
    exit;
}

// Here we check to see if a file with that name already exists
// You could get past filename problems by appending a timestamp to the filename and then continuing
if (file_exists($TARGET_PATH))
{
    
$_SESSION['error'] = "A file with that name already exists";
    
header("Location: index.php");
    exit;
}

// Lets attempt to move the file from its temporary directory to its new home
if (move_uploaded_file($image['tmp_name'], $TARGET_PATH))
{
    
// NOTE: This is where a lot of people make mistakes.
    // We are *not* putting the image into the database; we are putting a reference to the file's location on the server
    
$sql "insert into people (fname, lname, filename) values ('$fname', '$lname', '" $image['name'] . "')";
    
$result mysql_query($sql) or die ("Could not insert data into DB: " mysql_error());
    
header("Location: images.php");
    exit;
}
else
{
    
// A common cause of file moving failures is because of bad permissions on the directory attempting to be written to
    // Make sure you chmod the directory to be writeable
    
$_SESSION['error'] = "Could not upload file.  Check read/write persmissions on the directory";
    
header("Location: index.php");
    exit;
}
?>
I hope it will help you ...

Last edited by UnrealEd; 03-24-09 at 03:56 AM. Reason: fixed code tags
Reply With Quote
  #7 (permalink)  
Old 04-06-09, 05:49 PM
mike_jandreau mike_jandreau is offline
Newbie Coder
 
Join Date: Jun 2006
Location: Boston
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
I tried to get kOe's script to work, but couldn't. It just kept not accepting whatever file name I put into it.

I'm stumped here. I've tried a number of "scripts" online, which all work perfectly and upload the file, but I can't get them to insert the filename into the database, for that specific customerid.

The INSERT into statement works with the variables, but not when I add WHERE customerid=$customerid. It then fails.

UnrealEd, can you provide some more details on the method you mentioned above?

This isn't a deal breaker if I can't get it to work. It'd be nice, but the client can live without out. Any suggestions or help would be appreciated.
Reply With Quote
  #8 (permalink)  
Old 04-06-09, 06:05 PM
Jcbones Jcbones is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: North Carolina, USA
Posts: 516
Thanks: 5
Thanked 47 Times in 44 Posts
INSERT doesn't work with a WHERE statement.

INSERT makes a new row with 0 variables, then populates that row.

If your row already exists (the one that contains the "customerID") then use an UPDATE query.

or,

create a field for the customerID in a new table for images, and insert your customerID and your Image in one "INSERT" query...
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
MYSQL and Insert Records Issue on Multipage Form Sydney34 PHP 2 03-12-09 05:14 AM
[SOLVED] Multitype Form Data - Images and Text into SQL Server digitalkemical ASP 2 12-04-08 01:01 PM
Form to insert MySQL Hunter PHP 11 11-13-06 10:44 AM
Basic image upload and data to mysql mdhall Script Requests 3 11-05-03 05:00 PM


All times are GMT -5. The time now is 08:31 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.