简体   繁体   English

PHP MySQLi将文件重命名为Insert上的行ID

[英]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? 在SQL中插入时如何将文件重命名为行ID?

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. 所有输入都表示赞赏。

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 首先,您需要在mysql中找到下一个自动增量值以获取当前图像行ID
  2. move file to desired location on server with table row id 将文件移动到具有表行标识的服务器上的所需位置
  3. Insert record in mysql to store $file_destination in mysql. 在mysql中插入记录以在mysql中存储$ file_destination。

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. 首先你需要执行没有行id的mysql插入。
  2. fetch last insert id using mysqli_insert_id() to get mysql row id. 使用mysqli_insert_id()获取最后一个插入ID以获取mysql行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. 在mysql中更新最后插入的记录以在mysql中存储$ file_destination。

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);
}

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM