doulei6330 2019-02-04 11:10 采纳率: 0%
浏览 59

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

I'm trying to upload an image path along with some text to a MySQL database in PHP, how should I use the move_uploaded_file along with an SQL statement?

I've tried to move the move_uploaded file next to the statement. Though whatever I try, only the text will be saved in the database and not the image path. The code I have is this:

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

I expect all of my database columns to be filled with the given information but, the only thing missing is, of course, the image column and I want to know why that is.

  • 写回答

2条回答 默认 最新

  • douxin2003 2019-02-04 11:22
    关注

    Used if name saved in the $image file

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

报告相同问题?