0

I'm learning how to upload a photo with an insert. So far I got the image uploaded to the "photo" folder. That works. I created a blob field named "image".

Am I supposed to see a path in the blob field or an actual photo? Confused.

http://jsfiddle.net/zEZD7/

<?
$order = "INSERT INTO reg_add (connect_date, 
   reg, 
   first_name, 
   last_name,
   image)

VALUES

('$_POST[connect_date]', 
     '{$_POST[reg]}nv', 
     '$_POST[first_name]', 
     '$_POST[last_name]',
     '$_POST[image]')";

$new_image = 'photos/'.basename( $_FILES['image']['name']);
    if(move_uploaded_file($_FILES['image']['tmp_name'], $new_image)) {
        // The images was uploaded
  } else{
header("location: reg_add_fail_IMAGE.php"); 
}

$sql = "INSERT INTO image (path) VALUES ('" . mysql_real_escape_string($path) . "')";

$result = mysql_query($order);
?>

FORM:

<form id="form_register" method="POST" action="reg_add.php">

   <input class="req-string bx short" type="text" name="connect_date" id="connect_date">
   <input type="hidden" name="MAX_FILE_SIZE" value="100000">
   <input class="req-string bx long caps" type="text" name="reg" id="reg">
   <input class="req-string bx long" type="text" name="first_name">
   <input class="bx long" type="text" name="last_name">

   Choose a image to upload: <input name="image" type="file">
   <input id="rbSubmit" class="rb2 rbSubmit" type="submit" value="submit">
</form>
Erik
  • 47
  • 2
  • 10
  • You can do it either way, and there have been numerous questions on this site weighing up the pros and cons of each. – eggyal Apr 28 '12 at 15:59
  • Is it possible to see the image in the browse of the phpMyAdmin? or can I show the link? – Erik Apr 28 '12 at 16:00
  • If you want to put the link in the database (which I would recommend), you will have to add it to your `INSERT` statement (i.e. add the column name to the list before the `VALUES` keyword and the image path to the corresponding place in the list after the `VALUES` keyword): a string-type column (such as `VARCHAR`) would be more appropriate for this purpose than `BLOB`. If you want to store the image itself in the `BLOB` (which is possible, but I don't recommend), then you would have to do something similar using the contents of the image file as the value to be inserted. – eggyal Apr 28 '12 at 16:05
  • By the way, your code is vulnerable to SQL injection. You **really** should be using prepared statements, into which you pass your variables as parameters that do not get evaluated for SQL. If you don't know what I'm talking about, or how to fix it, read the story of [Bobby Tables](http://bobby-tables.com). – eggyal Apr 28 '12 at 16:06
  • I will eventually use prepared statements later. Right now I'm trying to understand the basics for the upload. – Erik Apr 28 '12 at 16:07
  • see this question - http://stackoverflow.com/questions/2879266/upload-file-with-php-and-save-path-to-sql – eggyal Apr 28 '12 at 16:12
  • I'm getting a failure: http://jsfiddle.net/zEZD7/ – Erik Apr 28 '12 at 16:28

1 Answers1

0

I take it you want to have a form and get the user to fill in some details and upload a picture, then display it back?

If so, I would not store the image in MySQl. I'd upload the image to the server's file system, somewhere off the WWW root (a directory that is accessible to the browser) and store the path of the image file in a char column.

The when you want to display it, just print out an tag with the source of the image taken from your DB query.

Also, you code seems to suggest you are taking user input and directly adding it to the database, this is very bad from a security point of view. You need to do input validation first (google that and SQL injection attack)

KevInSol
  • 2,560
  • 4
  • 32
  • 46
  • Sorry, am not familiar with jsfiddle.net, not sure what I'm looking at. I can post some sample code on Monday if you are not sorted out by then – KevInSol Apr 28 '12 at 17:31