【问题标题】:Why doesn't the image path get saved in the database?为什么图像路径没有保存在数据库中?
【发布时间】:2019-02-04 11:10:37
【问题描述】:

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

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

【问题讨论】:

  • 据我所知,您还没有在任何地方定义$image。错误报告也会通知您这一点。 :-)
  • 您尝试过什么来发现问题 - 任何调试尝试?你被困在哪里了?
  • 抱歉,忘记删除了,我之前将$image定义为$_FILES,这不起作用
  • $image = $_FILES['image_file']['name']。它必须在某个地方定义,然后才能使用它。 :-) $_FILES 是一个数组,而不是名称。
  • 我是调试新手,如何处理图像或文件?

标签: php mysql database image


【解决方案1】:

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

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

【讨论】:

  • 您能否详细说明您的答案,尝试过但仍然没有结果?
  • 我删除了感叹号,这确实保存了图像路径但不在服务器目录中
【解决方案2】:

在表单标签中你使用的是enctype='multipart/form-data'&gt;"吗?

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

【讨论】:

    猜你喜欢
    • 2016-08-14
    • 1970-01-01
    • 1970-01-01
    • 2012-01-27
    • 2019-04-02
    • 2015-10-12
    • 1970-01-01
    • 1970-01-01
    • 2021-02-07
    相关资源
    最近更新 更多