Uploading a picture to MySQL and the retrieving to upload to site

Can anybody please help, I am extremely new at PHP and am wanting to make a comment section for a website that its users can upload pictures and comment. This page will only be accessible to the users who log in. I have been able to code the form in html and then code the PHP to insert the info to MySQL, but the image is not being loaded to MySQL, I don’t know exactly what should be the structure of the blob for the image in MySQL. Also, the date is not loading properly in MySQL. I have the date as timestamp and update on current timestamp, null: NO, Default:Current Timestamp. Here is my code so far, if anyone can help I would appreciate it, Thanks

[php]

<?php include('config3.php'); $name=$_POST['name']; $comment=$_POST['comment']; $image=$_POST['image']; $date=$_POST['date']; $submit=$_POST['submit']; if($submit) { if($name&&$comment) { $insert=mysql_query("INSERT INTO comment1 (name,comment,image, date) VALUES ('$name','$comment', '$image', '$date') "); echo ""; } else { echo "please fill out all fields"; } } ?>
Name:
Comment:
<?php

$getquery=mysql_query(“SELECT * FROM comment1 ORDER BY date DESC”);
while($rows=mysql_fetch_assoc($getquery))
{
$id=$rows[‘id’];
$name=$rows[‘name’];
$image = addslashes(file_get_contents($_FILE[‘image’][‘tmp_name’]));
$date=$rows[‘date’];
$comment=$rows[‘comment’];
echo $name . ‘
’ . ‘
’ . $comment . ‘
’ . ‘
’ . ‘



;}
?>
[/php]

Take this how you want.

Don’t try to upload a picture into a data tables at this point, you are too new and you will likely end up corrupting the image. Instead, store the filename and location in the database. One reason being, type file does not get passed by post.

If your timestamp is being populated by the database, you don’t need a field for it. Also, the timestamp is very specific and that way for a reason, it is not wrong because that is not how you want it stored. When you get more comfortable with PHP, you can change the formatting to however you want, but MySQL stores a date in a specific way.

Next, learn mysqli_ or PDO. mysql_ is depricated and should not be used for anything other than legacy code that cannot be updated without more issues. After that, prepared statements.

Can you please explain how I would go about getting users who want to upload a picture from all different computers stored? I am not sure how to do this and thought the database would be the best place for that. Could I have a file on the server where they go and can retreive that to post on the site? How would I do this?

Thank you for all your help

sfaisal, I agree with astonecipher. Moving up to MySQLi instead of MySQL is easy. Here is a link for you
that explains it fairly simple. This is the full list of commands for MySQLi which you can look thru and use
as needed. http://www.w3schools.com/php/php_ref_mysqli.asp The following is the point to
start at as it shows how to set up a connection to you database and how to close it. It should get you
started. (Note that it also discusses PDO if you wish to go that route.) http://www.w3schools.com/php/php_mysql_connect.asp

Now as far as uploading images, normally, you do not upload to a database as it is slow and can be very
complicated. Most PHP programmers decide on a naming convention for the image instead. So, if you are
allowing members to upload, let’s say, their own picture for a profile, you can store it on your server with
a name that indicates their user ID or user name. Such as “sfaisal.jpg” where the user’s name is sfaisal.
In this manner, you do not need to do any coding except to check if they uploaded the picture or not. If the
file sfaisal.jpg does not exist in the picture folder, then, they have not uploaded it. When they upload the
image to your server, rename whatever their local file was named to their user name instead. Simple.
To retrieve the image, just insert an tag wherever you wish to show the image and steer the source
to the folder where you save the images. I hope that makes sense to you.

In your posted code, you had something like: This allows the user
to upload an image. The file name on this input will be whatever their local file was named. Therefore, you
will need to rename it when you store it on your server with PHP code. Here is a sample that I found for
you that shows how to do this: http://www.w3schools.com/php/php_file_upload.asp
Note that on this page, it shows you how to move the file to the folder where you want to save images.
You need to create a folder on your server and move the uploaded file from temporary files on the server to
the folder where you wish to save them. Then, when displayed, the tag should pull the image from
that folder with the new name you assigned to it.

Now, if you plan to allow many different images to be uploaded by one user, you would have to create a
way to list them so you know what each different user uploaded. That is a good thing to store into a DB.
You can create a table that lists each of the images uploaded and the text that goes with each. But, if you
are planning to just have one image per user, just save it with the user’s name or ID number. Simpler…

Hope that helps…

Can you please explain how I would go about getting users who want to upload a picture from all different computers stored?
This makes me think you don't understand what happens on the server. But the link below gives you code as a base on how to upload files to a server. If you want to use a database, that is fine. Just understand that storing a file as a blob causes other complications that you are versed enough in to handle. An alternative to that is to store the image name and link it to a specific user. The files would all be kept in a specific directory. When you want to display the image, you would pull the image name from the database and use the path to the file in an img tag. Like this:

[php]<?php

define( ‘PATH’, ‘/path/to/uploads/’);

?>

avatar[/php]

How to upload a file in PHP.

Ernie and I were on the same wave length! One thing to ensure though, make sure that the directory they are going to has write permission.

Good catch, Astone, I always forget to mention that to newbies… I had a horrible time getting one
image uploader to work years ago because I forgot to look at the write permissions.

Thanks for mentioning that. It might help him… And, Karma back at you…

Again thank you all for your help, I have been reading alot about this and have gotten alittle further but have again came up to a wall. So the users can upload a picture, but then I am trying to have that picture post to automaticly to the page

Here is the code, is this right, it is not working, the picture is in the directory I made and the file name is in MYSQL, but it is not retreiving it and posting it. Is there a better way?

<?php include('config3.php'); $query_image = "SELECT * FROM acc_images (acc_id='$id')"; $result = mysql_query($query_image); if(mysql_num_rows($result) > 0) { while($row = mysql_fetch_array($result)) { echo ''; } } else { echo 'File name not found in database'; } ?>

First the generic,

Stop using mysql functions use mysqli or PDO instead.
Useprepared statements.

Your issue is, the sql statement is incorrect.

Well, I was just heading out, but, saw you posted back… So, here goes…

Yes, that code should work. I will guess that either you did not store the extension of the image (.jpg) or
your formatting is slightly off.

The code looks okay. So, it does not give you the error “file name not in database”. So, it finds the name of
the image okay. You might want to just debug it like this. Change the echo of the image to die there.
So, change:
[php]
echo ‘<img src="uploads/’.$row[“image”].’">’;
[/php]
to:
[php]
die( ‘<img src="uploads/’.$row[“image”].’">’);
[/php]
This will force your page to stop processing at that point and show you what it is trying to display.
It should show you where the error is. You can post the last line it displays here if you can not figure out
the error.

I think you are almost done. Good luck…

Oh my gosh! Missed that one! Yes! You caught another one I missed today, Astone! More Karma for you!

Sponsor our Newsletter | Privacy Policy | Terms of Service