我上传的图片的文件名不会改变

时间:2017-08-07 08:36:24

标签: php session mysqli static

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

1 个答案:

答案 0 :(得分:2)

函数mysqli_insert_id返回刚刚插入数据库的行的id,由于你没有插入任何东西(你只是更新),函数返回的值是0,所以你的图像名称是$id.'.'.$extension ==&gt; 0.png

由于您更新了seller_id,并且您已在$_SESSION['seller_id']内进行更新,因此您可以在代码中使用它:

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