Reputation: 47
I have an existing mySQL database that's used for basic client profiles. I need to have a picture added for each record.
I have a form with inputs and textareas to create a record. I know the photo field is a blob, but how do I create an upload in the existing form? Any suggestions on where to start?
Will jquery make this easier to accomplish?
thanks.
Erik
Upvotes: 0
Views: 1292
Reputation: 1289
For performance purposes, I never upload a photo as a blob to mySql. Instead, I upload the path to the picture to mySql and upload the picture to the server using an upload class (something like this http://www.verot.net/php_class_upload.htm). Just add an existing input, add "enctype="multipart/form-data"" at the end of your form tag (form enctype="multipart/form-data" method="post"), implement the class, and then do your validation checks on a POST back to make sure the user added the file so it successfully updates to the database.
PHP:
if(isset($_POST['upload']))
{
include_once('class.upload.php');
if($_FILES['photo']['size'][0] != 0) //check to see if photo is empty{
//place existing code for data upload here but add photo filepath here
$handle = new Upload($file);
if ($handle->uploaded) { //if upload was successful
$handle->file_new_name_body = $file['name'];
$handle->Process("./photos/");
}
}
HTML:
<form name="form" id="form" action="form.php" enctype="multipart/form-data" method="post">
<input type="file" id="photo" name="photo" accept="gif|jpg|jpeg|png"/>
<button type="submit" class="button" name="upload" id="upload">Upload</button>
For existing photos, just have their profile pictures as default.png and direct them to an update form that does the same thing as above. Should work out fine.
Upvotes: 0
Reputation: 944
What I do is have something like a table called users with a column called image_id
and then another table called images_users with the columns id
, user_id
and file
. I have a php form where users can upload their image. The form will rename the file name (ex: mypicture.jpg, but keep the extension) to the next row id of the images_users table.
$getnewimageid = mysql_query("SELECT id FROM images_users ORDER BY id DESC LIMIT 1", $connection);
$rownewimageid = mysql_fetch_assoc($getnewimageid);
$new_image_id = $rownewimageid['id'];
$new_image_id = ($new_image_id + 1);
Determine the file extension in the form and then set $new_image_file = "$new_image_id.$extension"
So file gets renamed from mypicture.jpg to $new_image_id.jpg (ex: 102.jpg);
(I know that's not an explanation of how to actually do it, I'm just trying to explain how to start)
Then the form does 2 mysql queries to store the image and assign the image to the user
INSERT INTO images_users (id, user_id, file) VALUES ($new_image_id, $user_id, $new_file_name)
and
UPDATE users SET image_id = $new_image_id
Then later you can easily refence what file to show for the specific user. Users can even have multiple images by having multiple rows in the images_users table where user_id = their user id. You can display all of their images by
SELECT * FROM images_users WHERE user_id = $user_id
etc
Upvotes: 1
Reputation: 980
I think better way would be to upload the image for each profile than using blob in mysql db for storing image. You can check copy
or move_uploaded_file
php function for uploading file. You can later resize that images if required. You have to change your pictute field from blob to varchar .
Thanks.
Upvotes: 1