Hi Guys

I am working on a php page that allows the user to upload files to thei proile. I managed to fina an open source upload class. Currently I have the code uploading the file to an images folder on the web server and that file is renamed to the name specified on the input form.

I have joined the users table and media table in the db. I want the uploaded file to be inserted into the mdeia table. Do I need to have the files uploading to an images file, or can I just insert them straight into the database?

ON the users table i have a PK of user_id and on the media table i have a PK media_id and a FK user_id referencing users(user_id). When I insert files into the media table I want the user_id to be taken from the session variable

(isset($_SESSION['user_id']) && ($_SESSION['user_id'] == '1'))

See the form below:

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
       <label for="name">Name:</label>
       <input type="text" id="file_name" name="file_name" value="<?php if (!empty($file_name)) echo $file_name; ?>"/>
       <br />
       <label for="file">Media File:</label>
       <input type="file" id="file" name="file" />
       <br />
       <input type="submit" value="Upload" name="submit" />

The code that processes this form is:

$file_name = mysqli_real_escape_string($dbc, trim($_POST['file_name']));
$media_type = mysqli_real_escape_string($dbc, trim($_POST['media_type']));
//$file = mysqli_real_escape_string($dbc, trim($_POST['file']));
$handle = new Upload($_FILES['file']);
if ($handle->uploaded) {
  // save uploaded image with no changes
  $handle->file_new_name_body = $file_name;
  if ($handle->processed) {
    echo "file renamed " .$file_name. " and uploaded to profile"; 
  } else {
    echo 'error : ' . $handle->error;

//update the database

$query = "INSERT INTO media (file_name, media_type, user_id) VALUES ('$file_name', '$media_type', '$user_id')";
$data = mysqli_query($dbc, $query) or die ('db error');

To get the user_id into the media table I used the above insert statement, but this didnt work.

I also tried

$query = "INSERT INTO media (file_name, media_type, user_id) VALUES ('$file_name', '$media_type', '$_SESSION['user_id']')";

but this gave me an error.

can anyone help explain what I need to do so that I can insert the user_id into the media table. Once I can do this I can then run a select statement to view a users prfile with the images they have uploaded. If anyone has any suggestions on other ways of achieving what I am trying to do, I would like to hear them as I am very new to php. Thanks for your time in advace.


8 Years
Discussion Span
Last Post by fawkman

Two things to note -

There's no media type in the form which becomes invalid to use as $_POST

And The insert statement has a small error -

$query = "INSERT INTO media (file_name, media_type, user_id) VALUES ('$file_name', '$media_type', '$_SESSION['user_id']')";

should be changed to

$query = "INSERT INTO media (file_name, media_type, user_id) VALUES ('$file_name', '$media_type', '{$_SESSION['user_id']}')";

Thanks for your reply, this did the trick nicely

kind regards

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.