簡體   English   中英

PHP MySQLi將文件重命名為Insert上的行ID

[英]PHP MySQLi Rename File to Row ID on Insert

程序員大家好,

在SQL中插入時如何將文件重命名為行ID?

讓我給你舉個例子:

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

?>

所有輸入都表示贊賞。

下面給出了幾種處理這種情況的方法..

  1. 首先,您需要在mysql中找到下一個自動增量值以獲取當前圖像行ID
  2. 將文件移動到具有表行標識的服務器上的所需位置
  3. 在mysql中插入記錄以在mysql中存儲$ file_destination。

如下......

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

?>

要么

  1. 首先你需要執行沒有行id的mysql插入。
  2. 使用mysqli_insert_id()獲取最后一個插入ID以獲取mysql行id。
  3. 將文件移動到具有表行標識的服務器上的所需位置。
  4. 在mysql中更新最后插入的記錄以在mysql中存儲$ file_destination。

如下......

<?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