为什么图像路径没有保存在数据库中?

时间:2019-02-04 11:10:37

标签: php mysql database image

我正在尝试将图像路径以及一些文本上传到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);

我希望所有的数据库列都充满给定的信息,但是,唯一缺少的当然是图像列,我想知道为什么会这样。

2 个答案:

答案 0 :(得分:0)

如果名称保存在$ image文件中,则使用

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

答案 1 :(得分:0)

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

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