Hi all I am a new codeigniter .I have create code update image with form hidden but I have problam the from hidden not passing value to form image userfile :( Iam very need help now ?or could anyone give the source code about update image in to database on codigniter.
<form action="<?php echo base_url()."./site_admin/update_menu/".$this->uri->segment(3); ?>" method="post" enctype="multipart/form-data">
<?php
$id = $this->uri->segment(3);
$sql = $this->db->get_where('menu',array('id_menu' => $id));
$row = $sql->row(); ?>
<?php $idcat = $row->cate_id ?>
<table class="tab" style="width:100%">
<tr>
<td>Categories Name:</td>
<td>
<select style="padding:6px;background:#C2C2C2;font-weight: bold;" name="cate">
<?php
$query = $this->db->get('categories_menu');
foreach($query->result() as $lazy){
$select = "";
if($lazy->id == $idcat){
$select = "selected";
}
echo "<option $select value='".$lazy->id."'>".$lazy->cate_name_menu."</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td>Upload image:</td>
<td><input type="file" name="userfile" style="width:190px;height:35px;font-weight: bold;background:#C2C2C2;" ></td>
<td><input type="hidden" name="mono" value="<?php echo $row->image; ?>"></td>
</td>
</tr>
<tr>
<td>Title:</td>
<td><textarea class="title" name="title" cols="100" rows="4"><?php echo $row->title; ?></textarea></td>
</tr>
<tr>
<td>Description:</td>
<td><textarea rows="15" name="description" class="dess" ><?php echo $row->description; ?></textarea><td>
</tr>
<tr>
<td>Prices</td>
<td><textarea class="price" name="price" cols="40" rows="4"><?php echo $row->prices; ?></textarea></td>
</tr>
<tr>
<td></td>
<td>
<input style="width:100px; padding:10px;font-weight: bold;background:#1F1F1F;color:white;" type="submit" value="Submit" name="submit">
</td>
</tr>
</table>
</form>
Controller:
function update_menu(){
$id = $this->uri->segment(3);
$img1 = $this->input->post('userfile');
$img2 = $this->input->post('mono');
if($img1 == ""){
$img1 = $img2;
$datas = array(
'cate_id' => $this->input->post('cate'),
'title' => $this->input->post('title'),
'description' => $this->input->post('description'),
'prices' => $this->input->post('price'),
'image' => $img1
);
$this->db->where('id_menu', $id);
$this->db->update('menu',$datas);
redirect('./site_admin/edit_menu/'.$id);
}else{
$id = $this->uri->segment(3);
$updata = array(
'upload_path' => './images/',
'allowed_types' => 'gif|jpg|png',
'max_size' => '5000',
'max_width' => '4000',
'max_height' => '2800'
);
$this->load->library('upload',$updata);
$this->upload->do_upload('userfile');
$nampic = $this->upload->data();
$data = array(
'cate_id' => $this->input->post('cate'),
'title' => $this->input->post('title'),
'description' => $this->input->post('description'),
'prices' => $this->input->post('price'),
'image' => $nampic['file_name']
);
$this->db->where('id_menu', $id);
$this->db->update('menu',$data);
redirect('./site_admin/edit_menu/'.$id);
}
}
Related
I need to copy table contents from table: user_openletter to table: country on clicking archive button. How to write a query to copy from,to,title,openletter,open_id from user_openletter to another table country. This a codeigniter app.
my controller code is :
public function store_user_data_archieve()
{
$match = $this->input->post('submit');
echo $match;
if($match == "Archieve")
{
$data = array(
'open_id' => $this->input->post('open_id'),
'featured' => '1',
'from' => $this->input->post('from'),
'to' => $this->input->post('to'),
'title' => $this->input->post('title'),
'archieve' => '1',
'latest' => '0',
'sponsor' => 'images/sponsor.png'
);
$this->load->database();
//load the model
$this->load->model('select');
//load the method of model
$data['r']=$this->select->store_user_data_archieve($data);
echo "success";
}
else if(!$match == "new")
{
$data = array(
'open_id' => $this->input->post('open_id'),
'featured' => '1',
'from' => $this->input->post('from'),
'to' => $this->input->post('to'),
'title' => $this->input->post('title'),
'openletter' => $this->input->post('openletter'),
'archieve' => '0',
'latest' => '1',
'sponsor' => 'images/sponsor.png'
);
$this->load->database();
//load the model
$this->load->model('select');
//load the method of model
$data['r']=$this->select->store_user_data_archieve($data);
echo "success";
}
else if(!$match == "Discard")
{
echo "failure";
}
}
My view code is:
<?php
foreach ($r->result() as $row)
{
?>
<table border="1" cellpadding="4" cellspacing="0">
<tr>
<td>from</td>
<td>to</td>
<td>title</td>
<td>openletter</td>
<td>Date & Time</td>
<td>open_id</td>
</tr>
<tr>
<form action="/index.php/welcome/store_user_data_archieve" method="post">
<td><input type="text" name="from" value="<?php echo $row->from;?>" /></td>
<td><input type="text" name="to" value="<?php echo $row->to;?>" /></td>
<td><input type="text" name="title" value="<?php echo $row->title;?>" /></td>
<td><input type="text" name="openletter" value="<?php echo $row->openletter;?>" /></td>
<td><input type="text" name="datetime" value="<?php echo $row->datetime;?>" /></td>
<td><input type="text" name="open_id" value="<?php echo $row->open_id;?>" /></td>
<td><div><input type="submit" name="submit" value="Archieve" /></div></td>
<td><div><input type="submit" name="new" value="new" /></div></td>
</form>
</tr>
</table>
<?php } ?>
My model code is:
public function store_user_data_archieve($data)
{
//data is retrieved from this query
$this->db->insert('country', $data);
$this->db->set('openletter');
$this->db->select('openletter');
$this->db->where('open_id', $data[open_id]);
$this->db->from('user_openletter');
// Produces: INSERT INTO mytable (title, name, date) VALUES ('{$title}', '{$name}', '{$date}')
}
As per our discussion you need to copy from,to,title,openletter,open_id from table user_openletter to country table
$this->db->select('from,to,title,openletter,open_id');// select your filed
$q = $this->db->get('user_openletter')->result(); // get result from table
foreach ($q as $r) { // loop over results
$this->db->insert('country', $r); // insert each row to country table
}
$this->db->select('from, to, title,openletter, open_id');
$result_set = $this->db->get('user_openletter')->result();
if(count($result_set) > 0) {
$this->db->insert_batch('country', $result_set);
}
i am not using captcha in zend form i am directly writting zend captch code on controller. My code is:
IndexController.php
public function loginAction(){
$this->view->assign('action',"/sabkuch/index/auth");
$this->view->assign('user','username');
$this->view->assign('passwd','Password');
//$a=$this->_helper->Comman->textEmail("hi........","htm body.......","pkr","from#gm.com","xyz","xyz#gm.com");
//echo $a; die;
// Our form object...
// And here's our captcha object...
$captcha = new Zend_Form_Element_Captcha('captcha',
array('label' => 'Write the chars to the field',
'captcha' => array( // Here comes the magic...
// First the type...
'captcha' => 'Image',
'useNumbers' => true,
'fontSize' => '22',
'wordLen' => '4',
'height' => '57',
'width' => '235',
// Captcha timeout, 5 mins
'timeout' => 300,
// What font to use...
'font' => APPLICATION_PATH . '/../public/font/6.ttf',
// Where to put the image
'imgDir' => APPLICATION_PATH . '/../public/captcha/',
// URL to the images
// This was bogus, here's how it should be... Sorry again :S
'imgUrl' => 'http://localhost/sabkuch/public/captcha/',
)));
$this->view->captcha = $captcha;
}
Login.phml
<table width="100%" border="0" cellspacing="0" cellpadding="1">
<form class="form_align" name="login" method="post" action="<?php echo $this->escape($this->action);?>">
<tbody>
<tr>
<td align="left"><strong><?php echo $this->escape($this->user);?></strong></td>
</tr>
<tr>
<td align="left"><?php echo $this->formText('username', '', array(
'class' => 'textfield',
'alt' => 'NOBLANK~loginEmail~DM~',
'size' => '30',
)
); ?>
</td>
</tr>
<tr>
<td align="left"><strong><?php echo $this->escape($this->passwd);?></strong></td>
</tr>
<tr>
<td align="left"><?php echo $this->formPassword('passwd', '', array(
'class' => 'textfield',
'alt' => 'NOBLANK~loginEmail~DM~',
'size' => '30',
)
); ?>
</td>
</tr>
<tr>
<td align="left"><strong>Set Captcha</strong></td>
</tr>
<tr>
<td align="left"><?php
echo $this->captcha->render($this, null) ;
?>
</td></tr>
<tr>
<td align="left" style="padding-top:5px">
<?php echo $this->formHidden('id', 'login'); ?>
<input type="submit" name="submit" value="Submit"> </td>
</tr>
</tbody>
</form>
</table>
captch code output is
<dd id="captcha-element">
<img width="235" height="57" alt="" src="http://localhost/sabkuch/public/captcha/770eec95f4b247754b4b6c30cd74a4a4.png" />
<input type="hidden" name="captcha[id]" value="770eec95f4b247754b4b6c30cd74a4a4" id="captcha-id" />
<input type="text" name="captcha[input]" id="captcha-input" value="" /></dd>
i need captcha html output as per my requirement like
<tr><td> <dd id="captcha-element">
<img width="235" height="57" alt="" src="http://localhost/sabkuch/public/captcha/770eec95f4b247754b4b6c30cd74a4a4.png" />
<input type="hidden" name="captcha[id]" value="770eec95f4b247754b4b6c30cd74a4a4" id="captcha-id" />
</tr></td>
<tr><td>
<input type="text" name="captcha[input]" id="captcha-input" value="" /></dd>
</tr></td>
in zend form we cn add decoratro but i am directly using this captcha on controller How i change this ouput of html
I'm trying to upload an image with a car make, year, lease and payment data
along with the image upload. My image path is also inserted to the DB but i still cant figure how to combine both.
Im getting an error Array to string conversion and its not inputting my data and image upload.
cars table:
car_make | car_model | car_year | car_lease | car_payment | imgpath
acura | G37 Sedan | 2009 | 48 | 350 | assets/images/acura_1.jpg
controller:
public function car_validation_do_upload() {
$this->load->library('form_validation');
$this->form_validation->set_rules('car_make', 'Car Make', 'required');
$this->form_validation->set_rules('car_model', 'Car Model', 'required');
$this->form_validation->set_rules('car_year', 'Car Year', 'required');
$this->form_validation->set_rules('car_lease', 'Car Lease', 'required');
$this->form_validation->set_rules('car_payment', 'Car Payment', 'required');
if($this->form_validation->run() == TRUE) {
$config['upload_path'] = './assets/images/';
$config['allowed_types'] = 'gif|jpeg|jpg|png';
$this->load->library('upload', $config);
$newRow = array("car_make" => $this->input->post('car_make'),
"car_model" => $this->input->post('car_model'),
"car_year" => $this->input->post('car_year'),
"car_lease" => $this->input->post('car_lease'),
"car_payment" => $this->input->post('car_payment'));
$data = array('upload' => $this->upload->data());
$result = array_merge($newRow, $data);
$this->load->model("model_users");
$this->model_users->insert1($result);
$this->session->set_flashdata('message', 'car inserted into database!');
redirect('main/insertCar');
}
else {
$this->load->view('insert_car');
}
}
Model:
public function insert1($data) {
$this->db->insert("cars", $data);
}
View
<?php
//echo form_open('main/car_validation_insert');
echo form_open_multipart('main/car_validation_do_upload');
echo validation_errors();
if($this->session->flashdata('message')){
echo $this->session->flashdata('message');
}
?>
<table width="504" style="margin-left:15px;">
<tr>
<td colspan="2"><p class="table-title">Insert Car</p></td>
<td></td>
</tr>
<tr>
<td>Car Make: </td>
<td><input type="text" name="car_make" class="textbox" tabindex="1"/></td>
<td> </td>
</tr>
<tr>
<td>Car Model:</td>
<td><input type="text" name="car_model" class="textbox" tabindex="2"/></td>
<td> </td>
</tr>
<tr>
<td>Car Year:</td>
<td><input type="text" name="car_year" class="textbox" tabindex="3"/></td>
<td> </td>
</tr>
<tr>
<td>Car Lease:</td>
<td><input type="text" name="car_lease" class="textbox" tabindex="4"/></td>
<td> </td>
</tr>
<tr>
<td>Car Payment:</td>
<td><input type="text" name="car_payment" class="textbox" tabindex="5"/></td>
<td> </td>
</tr>
<tr>
<td>Car Image:</td>
<td><input type="file" name="userfile" size="20" /></td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</table>
<input type="submit" name="submit" value="submit" id="button" class="button" style="margin-left:140px;" />
<?php
echo form_close();
?>
Change this:
$data = array('upload' => $this->upload->data());
$result = array_merge($newRow, $data);
To this:
if ($this->upload->do_upload()){
$image_data = $this->upload->data();
//this will create a new key with the name upload in your newRow
//so theres no need to use the function array_merge
$newRow['imgpath'] ='assets/images/'.$image_data['file_name'];
}
else{
//do something here
echo 'error uploading image'
}
The $image_data variable will have this information about your uploaded image
Array
(
[file_name] => mypic.jpg
[file_type] => image/jpeg
[file_path] => /path/to/your/upload/
[full_path] => /path/to/your/upload/jpg.jpg
[raw_name] => mypic
[orig_name] => mypic.jpg
[client_name] => mypic.jpg
[file_ext] => .jpg
[file_size] => 22.2
[is_image] => 1
[image_width] => 800
[image_height] => 600
[image_type] => jpeg
[image_size_str] => width="800" height="200"
)
You can find all the information about the file upload class on the user guide of codeingiter:
http://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html
I have 3 pieces of the column, which I will inserting in one table in my database. The name of each form like this:
<h3><?= $title; ?></h3><?php echo form_open("admin/artikel/buat/");?>
<table width="95%">
<tr>
<td><b>Pilih Referensi Jurnal</b></td>
<td>
<input type="hidden" name="IDJurnal" id="IDJurnal" value="<?php echo $IDJurnal; ?>" />
<input type="text" name="volume" id="volume" value="<?php echo $volume; ?>" readonly="readonly" class="sedang" />
<?php echo anchor_popup('admin/artikel/popup', 'Referensi Jurnal', array('class' => 'button')); ?>
</td>
</tr>
<tr>
<td><b>Kategori</b></td>
<td>
<?php
echo form_dropdown('IDKategori', $kategori) . "";
?>
</td>
</tr>
<tr>
<td width="125"><strong>Judul</strong></td>
<td><input type="text" name="judul" class="panjang"></td>
</tr>
<tr>
<td width="125"><strong>Pengarang</strong></td>
<td>
<input type="text" name="pengarang" class="panjang">
<input type="text" name="pengarang" class="panjang">
<input type="text" name="pengarang" class="panjang">
</td>
</tr>
<tr>
<td><b>Abstract</b></td>
<td>
<?php
$data = array('name' => 'abstract');
echo $this->ckeditor->editor($data['name']);
?>
</td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" class="button" value="Simpan">
<input type="button" class="button" value="Batal" onClick="javascript: history.go(-1)" />
</td>
</tr>
<?php
echo form_close();
?>
i want inserting the value from each form name to my sql table, like this:
CREATE TABLE `pengarang`(
`IDPengarang` INT(11)NOT NULL AUTO_INCREMENT,
`IDArtikel` INT(11)NOT NULL,
`nama_pengarang` VARCHAR(255)NOT NULL,
PRIMARY KEY(`IDPengarang`))ENGINE = INNODB DEFAULT CHARSET = utf8;
in my model like this:
function addArtikel() {
$now = date("Y-m-d H:i:s");
$data = array(
'IDJurnal' => $this->input->post('IDJurnal'),
'IDKategori' => $this->input->post('IDKategori'),
'judul' => $this->input->post('judul'),
'abstract' => $this->input->post('abstract'),
'created_time' => $now,
'created_by' => $_SESSION['username']
);
// $data1 = array(
// 'IDJurnal' => $this->input->post('IDJurnal'),
// 'IDKategori' => $this->input->post('IDKategori'),
// 'judul' => $this->input->post('judul'),
// 'abstract' => $this->input->post('abstract'),
// 'created_time' => $now,
// 'created_by' => $_SESSION['username']
// );
// $data2 = array(
// 'IDArtikel' => $this->input->post('IDArtikel'),
// 'nama_pengarang' => $this->input->post('pengarang')
// );
$this->db->insert('artikel', $data);
// $this->db->insert_batch('artikel', $data1);
// $this->db->insert_batch('pengarang', $data1);
}
in my controller
function buat() {
if ($this->input->post('judul')) {
$this->MArtikel->addArtikel();
$this->session->set_flashdata('message', 'Artikel telah di buat !');
redirect('admin/artikel/index', 'refresh');
} else {
// konfigurasi ckfinder dengan ckeditor
$this->load->library('ckeditor');
$this->load->library('ckfinder');
$this->ckeditor->basePath = base_url() . 'asset/ckeditor/';
$this->ckeditor->config['toolbar'] = 'Full';
$this->ckeditor->config['language'] = 'en';
$this->ckfinder->SetupCKEditor($this->ckeditor, '../../../asset/ckfinder/');
$data['title'] = "Tambah Artikel";
$data['main'] = 'admin/artikel/artikel_buat';
$data['jurnal'] = $this->MJurnal->getJurnalDropDown();
$data['kategori'] = $this->MKategori->getKategoriDropDown();
$this->load->vars($data);
$this->load->view('dashboard/template');
}
}
What should I add to my controller and my model, that would make the results in my table looks like this
IDPengarang IDArtikel nama_pengarang
1 1 testing 1
2 1 testing 2
3 1 testing 3
thank's before
Making a form that will accept a bunch of textual data and an image upload.
Keep getting this error when adding new item to both tables:
Error Number: 1048
Column 'id_path' cannot be null
INSERT INTO thumbnails (id_path, id_data_row) VALUES (NULL, 17)
Filename: C:\wamp\www\project\system\database\DB_driver.php
Line Number: 330
What I'm trying to do is add text data(stuff like title, text, price, etc.) into one table(data) and then upload an image and add its full upload path(as primary key in thumbnails table) and the id of the inserted text data row(to link it with the thumbnail in thumbnails table) into thumbnails table.
For some reason it keeps passing NULL as the full upload path to thumbnails table.
crud.php(the controller):
function add()
{
//Set validation properties
$this->_set_fields();
//Set common properties
$data['title'] = 'Add new data row';
$data['message'] = '';
$data['action'] = site_url('crud/addDataRow');
$data['link_back'] = anchor('crud/index', 'Back to list', array('class' => 'back'));
//Load the view
$this->load->view('templates/header', $data);
$this->load->view('pages/crud_edit', $data);
$this->load->view('templates/footer');
}
function addDataRow()
{
//Set common properties
$data['title'] = 'Add new data row';
$data['action'] = site_url('crud/addDataRow');
$data['link_back'] = anchor('crud/index/', 'Back to list', array('class' => 'back'));
//Set validation properties
$this->_set_fields();
$this->_set_rules();
//Run validation
if($this->form_validation->run() == FALSE)
{
$data['message'] = '';
}
else
{
//Save the data
$full_file_path = null;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$path_to_uploads='./uploads';
$config['upload_path'] = $path_to_uploads;
$this->load->library('upload', $config);
//add this
$this->upload->initialize($config);
if (!$this->upload->do_upload()){
$error = $this->upload->display_errors();
echo "<script>alert($error);</script>";
}else{
$upload_data=$this->upload->data();
$file_name=$upload_data['file_name'];
$full_file_path = $path_to_uploads.'/'.$file_name;
}
$data_row = array(
'title' => $this->input->post('title'),
'text' => $this->input->post('text'),
'price' => $this->input->post('price'),
'status' => $this->input->post('status'),
'type' => $this->input->post('type')
);
$id = $this->crud_model->save($data_row);
$thumbnail_row = array(
'id_path' => $full_file_path,
'id_data_row' => $id
);
$this->crud_model->save_thumbnail($thumbnail_row);
//Set form input name="id"
$this->form_validation->id = $id;
//Set user message
$data['message'] = '<div class="success">New data row added!</div>';
}
$this->load->view('templates/header', $data);
$this->load->view('pages/crud_edit', $data);
$this->load->view('templates/footer');
}
crud_model.php(the model):
//Add new data row
function save($data)
{
$this->db->insert($this->tbl_data, $data);
return $this->db->insert_id();
}
//Add the thumbnail upload path and id of the row in data table to link them
function save_thumbnail($data)
{
$this->db->insert($this->tbl_thumbnails, $data);
return $this->db->insert_id();
}
EDIT(html form code):
crud_edit.php(html form code for adding a new item or updating an existing one):
<div id="contentColumn">
<h1><?php echo $title; ?></h1>
<?php echo $message; ?>
<form method="post" action="<?php echo $action; ?>" multipart="multipart">
<div class="data">
<table>
<tr>
<td width="30%">ID</td>
<td>
<input type="text" name="id" disabled="disabled" class="text" value="<?php echo set_value('id'); ?>"/>
<input type="hidden" name="id" value="<?php echo set_value('id',$this->form_data->id); ?>"/>
</td>
</tr>
<tr>
<td valign="top">Title<span style="color:red;">*</span></td>
<td>
<input type="text" name="title" class="text" value="<?php echo set_value('title',$this->form_data->title); ?>"/>
<?php echo form_error('title'); ?>
</td>
</tr>
<tr>
<td valign="top">Text<span style="color:red;">*</span></td>
<td>
<textarea name="text" class="text">
<?php echo set_value('text',$this->form_data->text); ?>
</textarea>
<?php echo form_error('text'); ?>
</td>
</tr>
<tr>
<td valign="top">Price<span style="color:red;">*</span></td>
<td>
<input type="text" name="price" class="text" value="<?php echo set_value('price',$this->form_data->price); ?>"/>
<?php echo form_error('price'); ?>
</td>
</tr>
<tr>
<td valign="top">Thumbnail<span style="color:red;">*</span></td>
<td>
<?php echo form_upload(array('name'=>'thumb', 'type'=>'file', 'accept'=>'image/*'))?>
<!--<input type="file" name="thumb" size="20" />-->
<?php echo form_error('thumb'); ?>
</td>
</tr>
<!--
<tr>
<td valign="top">Images<span style="color:red;">*</span></td>
<td>
<input type="text" name="images" class="text" value="<?php echo set_value('images',$this->form_data->images); ?>"/>
<?php echo form_upload(array('name'=>'images', 'type'=>'file', 'multiple'=>'multiple', 'accept'=>'image/*'))?>
<?php echo form_error('images'); ?>
</td>
</tr>
-->
<tr>
<td valign="top">Status</td>
<td>
<input type="text" name="status" class="text" value="<?php echo set_value('status',$this->form_data->status); ?>"/>
<?php echo form_error('status'); ?>
</td>
</tr>
<tr>
<td valign="top">Type<span style="color:red;">*</span></td>
<td>
<input type="text" name="type" class="text" value="<?php echo set_value('type',$this->form_data->type); ?>"/>
<?php echo form_error('type'); ?>
</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="Save" /></td>
</tr>
</table>
</div>
</form>
<br />
<?php echo $link_back; ?>
</div>
Change php code as below
$image1=$this->input->post('thumb');
if (!$this->upload->do_upload($image1)){
if (!$this->upload->do_upload()){
$error = $this->upload->display_errors();
echo "<script>alert($error);</script>";
}else{
$upload_data=$this->upload->data();
$file_name=$upload_data['file_name'];
$full_file_path = $path_to_uploads.'/'.$file_name;
$thumbnail_row = array(
'id_path' => $full_file_path,
'id_data_row' => $id
);
$this->crud_model->save_thumbnail($thumbnail_row);
}
My guess is the upload is failing, since you're trying to do the save regardless of the upload failing the value for full_file_path is going to be empty. By the way save ONLY the file name and put the path in your display code. Saving the path makes it a huge headache to move those files to another location