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

时间:2016-10-12 12:01:26

标签: php mysql mysqli insert

Hello其他程序员,

在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 个答案:

答案 0 :(得分:0)

下面给出了处理这种情况的几种方法..

  1. 首先,您需要在mysql中找到下一个自动增量值以获取当前图像行ID
  2. 将文件移动到具有表格行ID
  3. 的服务器上的所需位置
  4. 在mysql中插入记录以在mysql中存储$ file_destination。
  5. 如下所示......

    <?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. 首先你需要执行没有行id的mysql插入。
    2. 使用mysqli_insert_id()获取最后一个插入ID以获取mysql行ID。
    3. 将文件移动到具有表行标识的服务器上的所需位置。
    4. 更新mysql中最后插入的记录,以便在mysql中存储$ file_destination。
    5. 如下所示......

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