如何使用php更新sql中的图像

时间:2017-08-07 06:44:17

标签: php mysql image session sql-update

我想更新或设置特定用户的照片,当我尝试上传图片时,图片未上传到我的文件夹中"上传"并且照片的名称(这是一个数字,但是例如:1.jpg)插入数据库中,数据库中缺少文件扩展名,有人可以帮我这个

这是我的代码:

<?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))
        {
    }
    else
    {
        echo "error occured : " . mysqli_error($db);
    }

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

2 个答案:

答案 0 :(得分:0)

尝试这样......如果您的字段为varchar,则意味着它可以正常工作。并且您需要使用. [concodinate]运算符声明变量。未经测试检查并告诉我。

$sql2 = mysqli_query($db, "UPDATE seller
               SET
               trade_name = '".$trade_name."',
               s_address = '".$s_address."',
               s_image = '".$filename."' 
               WHERE seller_id= '" . $_SESSION['seller_id'] . "' ");

<强>编辑:

尝试更改,因为如果您的文件上传完成,那么以正确的方式保存到您的数据库中...在您的代码中运行第二个sql2查询,如果没有文件上传到文件夹中。

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)

$ upload_dir =&#34;上传&#34 ;; //要保存的图像的目录     $ upload_path = $ upload_dir。&#34; /&#34 ;;

$userfile_tmp = $_FILES['s_image'.$i]['tmp_name'];
$filename  = basename($_FILES['s_image'.$i]['name']);
$file_Size = $_FILES['s_image']['size'];    

$extension = strtolower(substr($filename, strrpos($filename, '.') + 1));
if (empty($extension)) {$error='No extension exist!';}

if(isset($_FILES['s_image']['name']) && $_FILES['s_image']['name']==true && $file_Size >0 && $error=='')
{
        $filename = $id.'.'.$extension;
        $new_image_location=$upload_path.$filename;
        //chmod($new_image_location, 0777);

        if(move_uploaded_file($userfile_tmp, $new_image_location))
        {
            $sql2 = mysqli_query($db, "UPDATE seller
                   SET
                   trade_name = '$trade_name',
                   s_address = '$s_address',
                   s_image = '$filename' 
                   WHERE seller_id= '" . $_SESSION['seller_id'] . "' ");
        }else{
            echo 'upload folder permission required!!';
            //chmod($new_image_location, 0777);             
        }       
}