为什么不将图像路径得到保存在数据库中?

问题描述 投票:-1回答:2

我想沿着上传图片的路径一些文字在PHP MySQL数据库,我应该如何使用move_uploaded_file与SQL语句一起?

我试着移动旁边的语句move_uploaded file。虽然无论我尝试,只有文本将被保存在数据库中,而不是图像路径。我的代码是这样的:

<?php

if (isset($_POST['listpost-submit'])) {

$target = "site_images/";
$target2 = $target. basename($_FILES['image_file']['name']);

$title = $_POST['title'];
$description = $_POST['description'];
$price = $_POST['price'];
$cat = $_POST['categories'];
$vendor = $_POST['vendor'];

elseif (move_uploaded_file($_FILES['image_file']['name'], $target2)) {
    //Database connection
    require 'dbh2.inc.php';
        $sql = "INSERT INTO listings 
(`image`,`title`,`description`,`price`,`category`,`vendor`) 
VALUES (?, ?, ?, ?, ?, ?)";
        $stmt = mysqli_stmt_init($conn);
        if (!mysqli_stmt_prepare($stmt, $sql)) {
        header("Location: ../listing1.php?error=sqlerror");
        exit();
        }
        else {
        mysqli_stmt_bind_param($stmt, "ssssss", $image, $title, 
        $description, $price, $cat, $vendor);
        mysqli_stmt_execute($stmt);
        mysqli_stmt_store_result($stmt);
        header("Location: ../index.php?listing=posted");
        exit();
         }
        }
        else {
    header("Location: ../listing1.php?file=notuploaded");
    exit();
         }
    }
    else {
        header("Location: ../listing1.php");
        exit();
    }
    mysqli_stmt_close($stmt);
    mysqli_close($conn);

我希望我所有的数据库中的列来填充,但唯一缺少的东西,当然,图像列,我想知道这是为什么,给定的信息。

php mysql database image
2个回答
0
投票

如果使用的名称保存在$图像文件中

if(!empty($_FILES['image_file']['name'])
$image = $_FILES['image_file']['name'];

0
投票

在表单标签您使用enctype='multipart/form-data'>"

<form action="" method="post" enctype="multipart/form-data">

© www.soinside.com 2019 - 2024. All rights reserved.