Jens Kvist Jens Kvist - 1 month ago 6
MySQL Question

PHP MySQLi Rename File to Row ID on Insert

Hello fellow programmers,

How do I rename my file to the row id while inserting in SQL?

Let me give you an example:

<?php

// File Properties
$file_name = $file['name'];
$file_tmp = $file['tmp_name'];

// File extension
$file_ext = explode('.', $file_name);
$file_ext = strtolower(end($file_ext));

// File destination
$file_rename = 'ROW ID' . $file_ext; // THIS IS WHERE THE ROW ID ARE NEEDED
$file_destination = 'uploads/' . $file_rename;

if(move_uploaded_file($file_tmp, $file_destination)) {
$connect = mysqli_connect('localhost', 'root', '', 'database');
$query = "INSERT INTO images (image_id, image_url) VALUES ('', '$file_destination')"; // THIS IS THE INSERT WHERE I NEED THE ROW ID
$result = mysqli_query($connect, $query);
}

?>


All inputs are appreciated.

Answer

Couple of ways to handle this scenario which are given below..

  1. First you need to find next auto incremental value in mysql to get current image row id
  2. move file to desired location on server with table row id
  3. Insert record in mysql to store $file_destination in mysql.

Like below...

<?php

// File Properties
$file_name = $file['name'];
$file_tmp = $file['tmp_name'];

// File extension
$file_ext = explode('.', $file_name);
$file_ext = strtolower(end($file_ext));

//Perform mysql 
$connect = mysqli_connect('localhost', 'root', '', 'database');
$query = "SELECT AUTO_INCREMENT FROM information_schema.tables WHERE table_name = 'images' AND table_schema = DATABASE( )"; // THIS IS WHERE YOU GET NEXT INCREMENTAL ROW ID VALUE
$next_insert_id = mysqli_query($connect, $query);

// File destination
$file_rename = $next_insert_id . $file_ext; // THIS IS WHERE THE ROW ID ARE NEEDED
$file_destination = 'uploads/' . $file_rename;

if(move_uploaded_file($file_tmp, $file_destination)) {
    //Do update for that record in the database
    $query = "INSERT INTO images (image_url) VALUES ('$file_destination')"; // THIS IS THE UPDATE
    $result = mysqli_query($connect, $query);
}

?>

OR

  1. first you need to perform mysql insert without row id.
  2. fetch last insert id using mysqli_insert_id() to get mysql row id.
  3. move file to desired location on server with table row id.
  4. Update last inserted record in mysql to store $file_destination in mysql.

Like below...

<?php

// File Properties
$file_name = $file['name'];
$file_tmp = $file['tmp_name'];

// File extension
$file_ext = explode('.', $file_name);
$file_ext = strtolower(end($file_ext));

//Perform mysql 
$connect = mysqli_connect('localhost', 'root', '', 'database');
$query = "INSERT INTO images (image_url) VALUES ('')"; // THIS IS THE INSERT WHERE I NEED THE ROW ID
$result = mysqli_query($connect, $query);
$last_insert_id = mysqli_insert_id();

// File destination
$file_rename = $last_insert_id . $file_ext; // THIS IS WHERE THE ROW ID ARE NEEDED
$file_destination = 'uploads/' . $file_rename;

if(move_uploaded_file($file_tmp, $file_destination)) {
    //Do update for that record in the database
    $query = "UPDATE images SET (image_url) VALUES ($file_destination) WHERE image_id = $last_insert_id"; // THIS IS THE UPDATE
    $result = mysqli_query($connect, $query);
}

?>