2017-08-07 63 views
0

Evertime我上傳了一張圖片,FILENAME沒有改變在數據庫中插入的靜態值(文件名)始終是「0.png」我不知道如何是發生了什麼,請幫助我如何解決這個問題。我上傳的圖片的文件名不變

這裏是我的代碼:

<?php 
session_start(); 
include("../db_connection.php"); 

$seller_id = $_SESSION['seller_id']; 
$trade_name = $_POST ['trade_name']; 
$s_address = $_POST ['s_address']; 
$opening_time = $_POST ['opening_time']; 
$opening_days = $_POST ['opening_days']; 
$order_cutoff = $_POST ['order_cutoff']; 
$seller_delivery_time = $_POST ['seller_delivery_time']; 
$area_covered_delivery = $_POST ['area_covered_delivery']; 
$delivery_fee = $_POST ['delivery_fee']; 

$extension = pathinfo($_FILES['s_image']['name'], PATHINFO_EXTENSION); 

$sql = mysqli_query($db, "UPDATE selling_details 
        SET 
        opening_time = '$opening_time', 
        opening_days = '$opening_days', 
        order_cutoff = '$order_cutoff', 
        seller_delivery_time = '$seller_delivery_time', 
        area_covered_delivery = '$area_covered_delivery', 
        delivery_fee = '$delivery_fee' 
        WHERE seller_id= '" . $_SESSION['seller_id'] . "' "); 
      if ($sql) 
    { 
     $id = mysqli_insert_id($db); 
     $filename = $id.'.'.$extension; 

     if(move_uploaded_file($_FILES['s_image']['tmp_name'], 'upload/'.$filename)) 
    { 
     $sql2 = mysqli_query($db, "UPDATE seller 
       SET 
       trade_name = '".$trade_name."', 
       s_address = '".$s_address."', 
       s_image = '".$filename."' 
       WHERE seller_id= '" . $_SESSION['seller_id'] . "' "); 
     if ($sql2) 
     { 

      header('location: seller_menu.php'); 
     } 
     else 
     { 
      echo "error occured : " . mysqli_error($db); 
     } 
    } 
    else 
    { 
     echo "error occured : " . mysqli_error($db); 
    } 
    } 
    ?> 
+2

您使用mysqli_insert_id($分貝);在'UPDATE'查詢上。這將無法正常工作,並將始終返回0. – IsThisJavascript

+0

您的代碼易受SQL注入影響。查看[本頁](http://bobby-tables.com/)瞭解更多詳情。 – apokryfos

回答

2

mysqli_insert_id返回您剛剛插入到數據庫的行的ID,因爲你不插入任何內容(您只需更新)的值,函數返回功能是0,所以您的圖像的名稱是$id.'.'.$extension ==>0.png

既然你更新seller_id,和你有它裏面$_SESSION['seller_id'],你可以在你的代碼中使用它:

$filename = $_SESSION['seller_id'].'.'.$extension; 
+0

哦,我沒有意識到這一點,非常感謝! –

相關問題