php - 使用php文件上传错误

标签 php mysql image file-upload upload

我已经编写了一些代码来使用 php 将文件上传到 MySQL 数据库中。 以下是我上传文件的代码。

$data['report_type']     = $this->input->post('report_type');
        $data['document_type']   = $this->input->post('document_type');
        $data['prescription_id'] = $this->input->post('prescription_id');
        $data['description']     = $this->input->post('description');
        $data['timestamp']       = strtotime(date('Y-m-d') . ' ' . date('H:i:s'));
        $data['laboratorist_id'] = $this->session->userdata('laboratorist_id');
        move_uploaded_file($_FILES["userfile"]["tmp_name"], "uploads/diagnosis_report/" . $_FILES["userfile"]["name"]);
        $data['file_name'] = $_FILES["userfile"]["name"];

        $this->db->insert('diagnosis_report', $data);
        $this->session->set_flashdata('flash_message', get_phrase('diagnosis_report_created'));
        redirect(base_url() . 'index.php?laboratorist/manage_prescription/edit/' . $this->input->post('prescription_id'), 'refresh'); 

每当我尝试上传任何文件时。它给出的错误为

Column 'file_name' cannot be null

INSERT INTO diagnosis_report (report_type, document_type, prescription_id, description, timestamp, laboratorist_id, file_name) VALUES (0, 0, 0, 0, 1392206996, '1', NULL)

我的代码有什么问题吗?

最佳答案

SQL 错误:

Column 'file_name' cannot be null

指的是插入语句中的最后一列。当 file_name 列的列定义中包含 NOT NULL 时,您尝试将其设置为 NULL

两种解决方案:

一,在数据库表上运行 alter table 命令以允许 NULL 值。

ALTER TABLE diagnosis_report MODIFY COLUMN file_name [datatype] DEFAULT NULL [etc]

如果它是 CMS 的一部分,或者您不维护的软件包,通常不建议这样做。如果是独立代码,则可以使用此解决方案。

或者两个,修改您的 PHP,不要将 file_name 设置为 NULL,而只是设置一个空字符串:

INSERT INTO diagnosis_report (report_type, document_type, prescription_id, description, timestamp, laboratorist_id, file_name) VALUES (0, 0, 0, 0, 1392206996, '1', '')

关于php - 使用php文件上传错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21727672/

相关文章:

php - 如何使用 Sencha Touch 2 (Native Packaging) 创建 Widget?

php - 是否可以通过编程方式从命令行访问 MongoDB 配置文件?

css - 如何在 CSS 中对图像应用淡入淡出的叠加层?

安卓阴影图像效果

mysql - 为什么 ALTER TABLE 用于更改 AUTO_INCREMENT 变量?

html - 如何正确复制 float 显示为内联 block

php - 实时位置跟踪 - 基于 Windows 程序还是浏览器?

PHP connection_aborted() 无法正常工作

php - 评论系统添加回复

MySQL IF/WHEN/ELSE/OR