【问题标题】:Uploading file, and insert the name of file to the database上传文件,并将文件名插入数据库
【发布时间】:2018-10-01 02:55:27
【问题描述】:

又是我。我在数据库中插入上传文件的名称时遇到问题。我已成功上传文件并且文件已移动到我想要的目录,谁能帮我弄清楚我的代码有什么问题?此外,我尝试对其进行一些回声,但我得到了“不成功”,并使数据库值可以为空。这是我的代码。

  <?php
        session_start();
        include "../../3rdparty/engine.php";
        ini_set('display_errors', 1);
        ini_set('display_startup_errors', 1);
        error_reporting(E_ALL);
        if ($_SESSION['daya_user'] != '') {

            if ($_FILES['dokumen']['name'] != "") {
                //print_r($_POST);
                    $path = $_FILES['dokumen']['name'];
                    $ext = pathinfo($path, PATHINFO_EXTENSION);
                    $nama_file = 'SO'.'-'.date("Ymd").'.'.$ext;
                    @copy($_FILES['dokumen']['tmp_name'], '../../dokumen_atk/'.$nama_file);

            mysqli_query($con,"insert into tbl_atk (name_file) values ('$nama_file')");
            header("location:../../index.php?mod=atk&submod=so_upload");
    }
}
//header("location:../../index.php?mod=atk&submod=so_upload");

?>

【问题讨论】:

  • $_SESSION['daya_user'] 设置的时间和地点?
  • @Geshode 用户登录系统时。
  • 好吧,那么我们需要代码,因为它显然没有设置。
  • @Geshode 全部设置,我尝试了另一种形式,它运行顺利。
  • 我刚刚意识到 else 属于第三个 if 而不是第一个。抱歉,我因为缩进弄糊涂了。

标签: php mysql database insert


【解决方案1】:

这样做。

改变这个

$insert = $db->query("insert into tbl_atk_upload (name_file) values ('$nama_file')", 0

mysqli_query($con,"insert into tbl_atk (name_file) values ('$nama_file')");
header('location:success.php'); //redirect to your success page

注意:您需要包含您的数据库连接并在使用查询时调用它。就像上面给定的示例变量 $con 一样,不要将您的查询包含在 if/eslse 条件中,因为这是一个坏习惯。

【讨论】:

  • 包含你的数据库文件,类似这样... include('conn.php');
  • 我做到了,include "../../3rdparty/engine.php";
猜你喜欢
  • 2018-09-16
  • 1970-01-01
  • 2016-11-20
  • 1970-01-01
  • 2014-11-04
  • 1970-01-01
  • 1970-01-01
  • 2011-10-27
  • 1970-01-01
相关资源
最近更新 更多