我有输入表格。这个表单的输入非常复杂,因为一个表单被提交到数据库中的三个以上的表。
我想在我的表单中添加上传图片,但我不知道我应该把它放在哪里。< br />
这是 控制器 的输入
function c_submit(){
$data = array(
'no_form' => $this->input->post('noform'),
'no_kon' => $this->input->post('nokon'),
'tgl_kun' => $this->input->post('tk'),
'tgl_input' => date("Y-m-d H:i:s"),
'no_lok' => $this->input->post('nolok'),
'no_obs' => $this->input->post('noobs'),
'no_koor' => $this->input->post('nokoor'),
'no_lahan' => $this->input->post('nolahan'),
't_utama' => $this->input->post('tutama'),
'pola_t' => $this->input->post('ptanam'),
'dlm_olah' => $this->input->post('kolah'),
't_tanah' => $this->input->post('ttanah'),
'no_prod' => $this->input->post('noprod'),
'np_padi' => $this->input->post('nppadi'),
'np_jagung' => $this->input->post('npjagung'),
'np_kedelai' => $this->input->post('npked'),
'np_lain' => $this->input->post('nplain'),
'catatan' => $this->input->post('cat')
);
$datakon = array(
'no_kon' => $this->input->post('nokon'),
'jabatan' => $this->input->post('jab'),
'nama' => $this->input->post('namakon'),
'telp' => $this->input->post('telp')
);
$datakoor = array(
'no_koor' => $this->input->post('nokoor'),
'utm_y' => $this->input->post('y'),
'utm_x' => $this->input->post('x')
);
$datalahan = array(
'no_lahan' => $this->input->post('nolahan'),
'jenis' => $this->input->post('jlahan'),
'penggunaan' => $this->input->post('plahan'),
'kondisi' => $this->input->post('klahan'),
'drainase' => $this->input->post('drain')
);
$dataprod = array(
'no_prod' => $this->input->post('noprod'),
'p_padi' => $this->input->post('p_padi'),
'p_jagung' => $this->input->post('p_jagung'),
'p_kedelai' => $this->input->post('p_ked'),
'pr_lain' => $this->input->post('pr_lain'),
'prod_lain' => $this->input->post('prod_lain'),
'pr_lain2' => $this->input->post('pr_lain2'),
'prod_lain2' => $this->input->post('prod_lain2')
);
$datavar = array(
'no_var' => $this->input->post('novar'),
'v_padi' => $this->input->post('v_padi'),
'v_jagung' => $this->input->post('v_jagung'),
'v_kedelai' => $this->input->post('v_ked'),
'v_lain' => $this->input->post('v_lain'),
'var_lain' => $this->input->post('var_lain'),
'v_lain2' => $this->input->post('v_lain2'),
'var_lain2' => $this->input->post('var_lain2')
);
$datalok = array(
'no_lok' => $this->input->post('nolok'),
'kabu' => $this->input->post('kabu'),
'keca' => $this->input->post('keca'),
'desa' => $this->input->post('desa'),
'desk' => $this->input->post('desk_lok')
);
$datappadi = array(
'np_padi' => $this->input->post('nppadi'),
'p_organik' => $this->input->post('padi_o'),
'p_urea' => $this->input->post('padi_u'),
'p_kcl' => $this->input->post('padi_k'),
'p_sp36' => $this->input->post('padi_s'),
'p_phonska' => $this->input->post('padi_p'),
'p_lain' => $this->input->post('pp_l'),
'pp_lain' => $this->input->post('padi_l')
);
$datapjagung = array(
'np_jagung' => $this->input->post('npjagung'),
'j_organik' => $this->input->post('jagung_o'),
'j_urea' => $this->input->post('jagung_u'),
'j_kcl' => $this->input->post('jagung_k'),
'j_sp36' => $this->input->post('jagung_s'),
'j_phonska' => $this->input->post('jagung_p'),
'j_lain' => $this->input->post('pj_l'),
'pj_lain' => $this->input->post('jagung_l')
);
$datapked = array(
'np_kedelai' => $this->input->post('npked'),
'k_organik' => $this->input->post('ked_o'),
'k_urea' => $this->input->post('ked_u'),
'k_kcl' => $this->input->post('ked_k'),
'k_sp36' => $this->input->post('ked_s'),
'k_phonska' => $this->input->post('ked_p'),
'k_lain' => $this->input->post('pk_l'),
'pk_lain' => $this->input->post('ked_l')
);
$dataplain = array(
'np_lain' => $this->input->post('nplain'),
'jenis_l' => $this->input->post('j_lain'),
'organik' => $this->input->post('lain_o'),
'urea' => $this->input->post('lain_u'),
'kcl' => $this->input->post('lain_k'),
'sp36' => $this->input->post('lain_s'),
'phonska' => $this->input->post('lain_p'),
'pupuk_lain' => $this->input->post('pl_l'),
'pem_lain' => $this->input->post('lain_l')
);
$no_obs = $this->session->userdata('no_obs');
$this->m_input->m_submit($data, $datakon, $datakoor, $datalahan, $dataprod, $datavar, $datalok, $datappadi, $datapjagung, $datapked, $dataplain);
$this->session->set_flashdata('msg', '<div class="alert alert-success"><p><b>SUKSES!</b> Data berhasil diinputkan!</p></div>');
redirect('c_read');
}
这是我的 模型
function m_submit($data, $datakon, $datakoor, $datalahan, $dataprod, $datavar, $datalok, $datappadi, $datapjagung, $datapked, $dataplain) {
$this->db->trans_start();
$this->db->insert('koor_pen', $datakoor);
$no_koor = $this->db->insert_id();
$this->db->insert('kontak', $datakon);
$no_kon = $this->db->insert_id();
$this->db->insert('lahan', $datalahan);
$no_lahan = $this->db->insert_id();
$this->db->insert('produktivitas', $dataprod);
$no_prod = $this->db->insert_id();
$this->db->insert('varietas', $datavar);
$no_var = $this->db->insert_id();
$this->db->insert('lokasi', $datalok);
$no_lok = $this->db->insert_id();
$this->db->insert('pem_padi', $datappadi);
$np_padi = $this->db->insert_id();
$this->db->insert('pem_jagung', $datapjagung);
$np_jagung = $this->db->insert_id();
$this->db->insert('pem_kedelai', $datapked);
$np_kedelai = $this->db->insert_id();
$this->db->insert('pem_lain', $dataplain);
$np_lain = $this->db->insert_id();
$data['no_kon'] = $no_kon;
$data['no_koor'] = $no_koor;
$data['no_lahan'] = $no_lahan;
$data['no_prod'] = $no_prod;
$data['no_var'] = $no_var;
$data['no_lok'] = $no_lok;
$data['np_padi'] = $np_padi;
$data['np_jagung'] = $np_jagung;
$data['np_kedelai'] = $np_kedelai;
$data['np_lain'] = $np_lain;
$this->db->insert('input_pen', $data);
$this->db->trans_complete();
return $this->db->insert_id();
}
我上传的图片的链接应该在$data
数组中。
我不知道该怎么办。谁能举个例子?
答案 0 :(得分:0)
此脚本会将您的文件上传到目标文件夹mkdir($config['upload_path'], 0777, TRUE);
Param number 3,TRUE将为您递归创建目录。
相应的行将放置数据库的名称和路径
$post_data = array(
'name' => $_FILES["__INPUT__FIELD__NAME"]['name'],
'path' => $hook
);
$this->db->insert('FILES__DB__', $post_data);
通用用法
File_handler模型
<?PHP
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class File_handler extends CI_Model {
private $base_p = "./files/";
private $ext = 'pdf|csv|doc|txt';
private $size = 5000000;
function _push_file($path, $name) {
// make sure it's a file before doing anything!
if (is_file($path)) {
// required for IE
if (ini_get('zlib.output_compression')) {
ini_set('zlib.output_compression', 'Off');
}
// get the file mime type using the file extension
$this->load->helper('file');
$mime = get_mime_by_extension($path);
// Build the headers to push out the file properly.
header('Pragma: public'); // required
header('Expires: 0'); // no cache
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($path)) . ' GMT');
header('Cache-Control: private', false);
header('Content-Type: ' . $mime); // Add the mime type from Code igniter.
header('Content-Disposition: attachment; filename="' . basename($name) . '"'); // Add the file name
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($path)); // provide file size
header('Connection: close');
readfile($path); // push it out
exit();
}
}
public function fsize($s) {
$this->size = $s;
}
public function fext($ext) {
$this->ext = $ext;
}
public function upload_file($field, $subpath) {
if (!empty($_FILES[$field]['name'])) {
$config['upload_path'] = $this->base_p . '' . $subpath;
$config['allowed_types'] = $this->ext;
$config['max_size'] = $this->size; // 5 mb
$config['encrypt_name'] = TRUE;
if (!is_dir($config['upload_path'])) {
mkdir($config['upload_path'], 0777, TRUE);
}
$this->load->library('upload', $config);
if (!$this->upload->do_upload($field)) {
$status = 'error';
$msg = $this->upload->display_errors('', '');
return json_encode(array('result' => 'error', 'msg' => $msg));
die();
} else {
$data = $this->upload->data();
}
return $path = 'files' . $subpath . '/' . $data['file_name'];
}
}
}
在逻辑模型中使用
$this->load->model("File_handler", "fh");
$this->fh->fext("jpg|jpeg|pdf|png");
$hook = $this->fh->upload_file("__INPUT__FIELD__NAME", "__PATH__TO__UPLOAD");
// Errors from File_handler controller
if (isset(json_decode($hook)->result) && json_decode($hook)->result == "error") {
$message = json_decode($hook)->msg;
}
// If we got the path all is ok
if ($hook) {
$post_data = array(
'name' => $_FILES["__INPUT__FIELD__NAME"]['name'],
'path' => $hook
);
$this->db->insert('FILES__DB__', $post_data);
$fid = $this->db->insert_id();
if (!is_numeric($fid)) {
$message = "Error, file not added";
}
}
if (strlen($message) == 0) {
// No errors in the file handler
// get your upload url path with file name from $hook
}else{
echo $message;
}.
在你的情况下:
您需要在数据库中创建一个使用此架构的表
CREATE TABLE `file_uploads` (
`id` bigint(20) NOT NULL,
`name` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`time_added` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`path` varchar(200) COLLATE utf8_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Indexes
--
ALTER TABLE `file_uploads`
ADD PRIMARY KEY (`id`);
--
-- AUTO_INCREMENT
--
ALTER TABLE `file_uploads`
MODIFY `id` bigint(20) NOT NULL AUTO_INCREMENT;COMMIT;
您的模型
function m_submit($data, $datakon, $datakoor, $datalahan, $dataprod, $datavar, $datalok, $datappadi, $datapjagung, $datapked, $dataplain) {
$this->db->trans_start();
$this->db->insert('koor_pen', $datakoor);
$no_koor = $this->db->insert_id();
$this->db->insert('kontak', $datakon);
$no_kon = $this->db->insert_id();
$this->db->insert('lahan', $datalahan);
$no_lahan = $this->db->insert_id();
$this->db->insert('produktivitas', $dataprod);
$no_prod = $this->db->insert_id();
$this->db->insert('varietas', $datavar);
$no_var = $this->db->insert_id();
$this->db->insert('lokasi', $datalok);
$no_lok = $this->db->insert_id();
$this->db->insert('pem_padi', $datappadi);
$np_padi = $this->db->insert_id();
$this->db->insert('pem_jagung', $datapjagung);
$np_jagung = $this->db->insert_id();
$this->db->insert('pem_kedelai', $datapked);
$np_kedelai = $this->db->insert_id();
$this->db->insert('pem_lain', $dataplain);
$np_lain = $this->db->insert_id();
$data['no_kon'] = $no_kon;
$data['no_koor'] = $no_koor;
$data['no_lahan'] = $no_lahan;
$data['no_prod'] = $no_prod;
$data['no_var'] = $no_var;
$data['no_lok'] = $no_lok;
$data['np_padi'] = $np_padi;
$data['np_jagung'] = $np_jagung;
$data['np_kedelai'] = $np_kedelai;
$data['np_lain'] = $np_lain;
$this->db->insert('input_pen', $data);
/* add my image */
$this->load->model("File_handler", "fh");
$this->fh->fext("jpg|jpeg|pdf|png");
$hook = $this->fh->upload_file("__INPUT__FIELD__NAME", "__PATH__TO__UPLOAD");
// Errors from File_handler controller
if (isset(json_decode($hook)->result) && json_decode($hook)->result == "error") {
$message = json_decode($hook)->msg;
}
// If we got the path all is ok
if ($hook) {
$post_data = array(
'name' => $_FILES["foto"]['name'],
'path' => $hook
);
$this->db->insert('file_uploads', $post_data);
$fid = $this->db->insert_id();
if (!is_numeric($fid)) {
$message = "Error, file not added";
}
}
if (strlen($message) == 0) {
// No errors in the file handler
// get your upload url path with file name from $hook and do here whatever u want
} else {
echo $message;
}
$this->db->trans_complete();
return $this->db->insert_id();
}
需要注意的事项HTML
您需要将表单enctype =&#34; multipart / form-data&#34;
<form id="FormId" name="Formname" method="post" enctype="multipart/form-data" accept-charset="utf-8">
输入类型文件
<input type="file" class="form-control" name="foto" id="foto">
jquery ajax处理的代码示例
$.ajax({
url: '<?PHP echo base_url(); ?>CONTROLLER/FUNCTION',
type: "POST",
mimeType: "multipart/form-data",
contentType: false,
dataType: 'json',
cache: false,
processData: false,
data: $form.serialize(),
success: function (data) {
// Your logic
}
});
答案 1 :(得分:0)
首先让我们清除代码
对于提交sevral表单,您可以轻松地在html名称标签中使用用户数组
<input name="datakon[no_kon]" >
<input name="datakon[jab]" >
<input name="datakon[namakon]" >
<input name="datakon[telp]" >
在Controller端,您可以将它们插入数据库中一行
$datakon = $this->input->post('datakon');
$this->db->insert('kontak', $datakon);
$no_kon = $this->db->insert_id();
如果您的数据库表名与输入字段相同,则完成
现在让我们开始上传文件
您不应该上传数据库中的文件。
而不是将文件上传到数据库,您可以将文件升级到目录并保存该目录地址。
要上传文件,您可以使用codeigniter上传库Codeigniter Upload File Documentation
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('no_kon'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
希望能提供帮助