-1

I have a small problem with my PHP image upload code, I`m not getting the correct file name to be saved to my MySQL database.

Please tell me what im doing wrong.

PHP:

// Upload File to Directory
$code = md5(time());
$image = $_FILES['userfile']['name'];
$imagename = $code.$image;
$uploaddir = '../_gallery/' . $imagename;
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile);

MySQL query:

$sql="UPDATE b_events 
      SET ename = '$event_name', 
          edescription = '$event_description', 
          edate = '$event_date', 
          etime = '$event_time', 
          ecost = '$event_cost', 
          eimage = '$imagename' 
      WHERE id = '$update'";
$result=mysql_query($sql);
3
  • 1
    what result do you expect to get and what result are you getting ? Commented Jul 20, 2012 at 10:49
  • Im getting the file name, trying to rename it with md5, file is uploaded correctly with new name, but not to the database. Commented Jul 20, 2012 at 10:52
  • 3
    Please don't use mysql_* functions in new code. They were removed from PHP 7.0.0 in 2015. Instead, use prepared statements via PDO or MySQLi. See Why shouldn't I use mysql_* functions in PHP? for more information. Commented Jul 20, 2012 at 10:55

1 Answer 1

1

you need to insert this name to your database becuase you upload file into directory by this name

or by assigning values to new variable

$sql="UPDATE b_events 
          SET ename = '$event_name', 
              edescription = '$event_description', 
              edate = '$event_date', 
              etime = '$event_time', 
              ecost = '$event_cost', 
              eimage = '$uploadfile' 
        WHERE id = '$update'";

OR

  $file_name =basename($_FILES['userfile']['name'];

$sql="UPDATE b_events 
          SET ename = '$event_name', 
              edescription = '$event_description', 
              edate = '$event_date', 
              etime = '$event_time', 
              ecost = '$event_cost', 
              eimage = '$file_name' 
        WHERE id = '$update'";
Sign up to request clarification or add additional context in comments.

5 Comments

Im still getting only the file name "image.jpg", it hould be something like "0fec90395c188efea02300c1dabf7950image.jpg".
ok u need to use this variable for insertion $imagename which you make by concatenation of md5 and file
tried that on my post, not working, not getting the full name to be inserted to the database, just that the file uploads 100% correctly with the name change and not the the database itself.
Used variable $uploadfile and now working correctly by uploading the renamed file to the directory and saving the file name to the database.
I have it solved, is there a way to resize the image though the code above? need it resized.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.