我已经编写了一些代码来使用 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/