couldn't insert data in codeigniter - php

i couldn't insert data to database. i don't know where the problem but when i var_dump($this->mberita->get_berita()); the result is array(0){}. I am a newbie in Codeigniter and couldn't really figure out how to solve this.
model
function get_berita()
{
$this->db->order_by('id_berita','asc');
$data = $this->db->get('berita_ukm');
return $data->result();
}
//untuk menambah berita
function insert_berita($data)
{
$data = array(
'id_berita' => $this->input->post('id_berita'),
'tanggal' => $this->input->post('tanggal'),
'judul_berita' => $this->input->post('judul_berita'),
'content' => $this->input->post('content')
);
$this->db->insert('berita_ukm', $data);
}
function validate_berita()
{
$this->form_validation->set_rules('id_berita', 'Id Berita', 'required|numeric');
$this->form_validation->set_rules('tanggal', 'Tanggal', 'required');
$this->form_validation->set_rules('judul_berita', 'Judul Berita', 'required');
$this->form_validation->set_rules('content', 'Content', 'required');
if ($this->form_validation->run() == TRUE) {
return TRUE;
}
}
controller
function tambah_berita()
{
if ($this->mberita->validate_berita() == TRUE) {
$this->mberita->insert_berita();
redirect('admin/berita/tambah_berita');
}
$this->data['orang'] = $this->mlogin->dataPengguna($this->session->userdata('username'));
$this->data['contents'] = $this->load->view('admin/berita/tambah_berita', '', true);
$this->load->view('template/wrapper/admin/wrapper_ukm',$this->data);
}
Please help me what to do. Thank you.

<?php
#in controller page#
// notes : you should make validation in controller page
if ($this->input->post('send')){ //request submit
// make form validation
$this->form_validation->set_rules('id_berita', 'Id Berita', 'required|numeric');
$this->form_validation->set_rules('tanggal', 'Tanggal', 'required');
$this->form_validation->set_rules('judul_berita', 'Judul Berita', 'required');
$this->form_validation->set_rules('content', 'Content', 'required');
if ($this->form_validation->run() == TRUE) {
// request data then put in array
$data = array(
'id_berita' => $this->input->post('id_berita'),
'tanggal' => $this->input->post('tanggal'),
'judul_berita' => $this->input->post('judul_berita'),
'content' => $this->input->post('content')
);
$this->mberita->insert_berita($data);
}
}
//model page
function insert_berita($data)
{
$this->db->insert('berita_ukm', $data);
}

Related

Get multiple values of checkbox in codeigniter

I am currently doing a project. I have a checkbox( where the user will choose type of services provided by the company). When I try to post the service that was selected(for example 2 services is checked) in my controller, I am only getting one service. The question is how can I get the multiple values in my checkbox?
Note: I also tried to use foreach within my controller, I am getting some error like "Invalid argument supplied for foreach()".
View
<label>Desired Service</label> <br>
<?php foreach($services as $s):?>
<label><input type="checkbox" name="service_name[]" value="<?= $s->service_name?>"><?= $s->service_name?></label>
<br>
<?php endforeach?>
Controller
$this->form_validation->set_error_delimiters('<div class="alert alert-danger" role="alert">', '</div>');
$this->form_validation->set_rules('full_name', 'Fullname', 'required');
$this->form_validation->set_rules('email', 'Email', 'required');
$this->form_validation->set_rules('contact', 'Contact', 'required');
$this->form_validation->set_rules('date', 'Date', 'required');
$this->form_validation->set_rules('address', 'Address', 'required');
$this->form_validation->set_rules('zip_code', 'Zip Code', 'numeric|required');
$this->form_validation->set_rules('province', 'Province', 'required');
$this->form_validation->set_rules('date', 'Date', 'required');
$this->form_validation->set_rules('service_name', 'Service', 'required');
if ($this->form_validation->run() == FALSE) {
$this->index();
}
else {
$service_name = implode(', ', $_POST['service_name']);
$event = array(
'full_name' => $this->input->post('full_name'),
'email' => $this->input->post('email'),
'contact' => $this->input->post('contact'),
'address' => $this->input->post('address'),
'zip_code' => $this->input->post('zip_code'),
'state_province' => $this->input->post('province'),
'date' => $this->input->post('date'),
'service' => $service_name
);
$this->EventModel->add_event($event);
echo "<script>
window.alert('Your Desired Date is being Proccessed!');
location.href = '".site_url('/')."';
</script>";
}
Change from
$service_name = $_POST['service_name'];
foreach($service_name as $key =>$value)
{
echo $value;
}
die;
to
$service_name = implode(',',$_POST['service_name']);
echo $service_name;
I hope it will solve your problem
if (!empty($this->input->post('service_name'))) {
foreach ($this->input->post('service_name') as $key => $val) {
$data[] = array(
'service_name' => $_POST['service_name'][$key]
);
}
foreach ($data as $item) {
echo $item['service_name'];
}
Try:
$service_name = $this->input->post('service_name');
for($i=0;$i < count($service_name);$i++){
echo $service_name[$i];
}

Validation of an API in codeigniter not working properly

I am writing an API in codeigniter. I am validating the fields it has with codeigniter built in function but somehow it's not working as it should.
public function check_validation()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$firstname = mysql_real_escape_string($this->input->post('firstname'));
$lastname = mysql_real_escape_string($this->input->post('lastname'));
$email = mysql_real_escape_string($this->input->post('email'));*/
//$firstname = 'Numaan';
//$lastname = 'sheikh';
//$email = 'test#test.com';
$this->form_validation->set_rules('firstname', 'Username', 'required');
$this->form_validation->set_rules('email', 'Email', 'required');
if ($this->form_validation->run() == FALSE)
{
$finalResult = array('code' => 100,
'msg'=>'Field Emsdsdspty.',
'data' => array()
);
}
else
{
$finalResult = array('code' => 100,
'msg'=>'Validation Successful.',
'data' => array()
);
}
echo json_encode($finalResult);
}
I am trying to get the posted values but it is not working properly.then i also tried to assign the values to the variables and then passed through the validation it also did not work.
Try this (i not testing)
function check_validation()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
//default result
$finalResult = array('code' => 500, 'msg' => 'Unknow Error', 'data' => array());
if($this->input->post()) {
//set validaton
$this->form_validation->set_rules('firstname', 'Username', 'trim|strip_tags|required');
$this->form_validation->set_rules('lastname', 'Lastname', 'trim|strip_tags|required');
$this->form_validation->set_rules('email', 'Email', 'trim|strip_tags|required');
//check form
if($this->form_validation->run() == TRUE) {
$finalResult = array('code' => 200, 'msg' => 'Success', 'data' => array());
}else {
$finalResult = array('code' => 400, 'msg' => validation_errors(), 'data' => array());
}
}
echo json_encode($finalResult);
}
ps. api status code: http://www.restapitutorial.com/httpstatuscodes.html

Returning info to controller from model in codeigniter

I've got this model function:
public function set_news()
{
$this->load->helper('url');
$slug = url_title($this->input->post('title'), 'dash', TRUE);
$data = array(
'title' => $this->input->post('title'),
'slug' => $slug,
'text' => $this->input->post('text')
);
return $this->db->insert('news', $data);
}
Where, as you can see, there is a variable called $slug.
The way I call this function is through a controller:
public function create()
{
$this->load->helper('form');
$this->load->library('form_validation');
$data['title'] = 'Create a news item';
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('text', 'Text', 'required');
if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('news/create');
$this->load->view('templates/footer');
}
else
{
$var = $this->news_model->set_news();
$this->load->view('news/SLUG/');
}
}
Now, what I'm trying to figure out how to do is use that $slug variable in the controller, to load the view of the article that was just posted - but I can't figure out how to access that variable after the data is submitted to the database.
you can try with this
controller
public function create()
{
$this->load->helper('form');
$this->load->library('form_validation');
$data['title'] = 'Create a news item';
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('text', 'Text', 'required');
if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('news/create');
$this->load->view('templates/footer');
}
else
{
$var['r'] = $this->news_model->set_news();
$this->load->view('news/SLUG', $var);
}
}
model news_model.php
public function set_news()
{
$this->load->helper('url');
$slug = url_title($this->input->post('title'), 'dash', TRUE);
$data = array(
'title' => $this->input->post('title'),
'slug' => $slug,
'text' => $this->input->post('text')
);
$this->db->insert('news', $data);
$id = $this->db->insert_id();
return $this->db->get_where('news', array('id' => $id))->result();
}
view news/SLUG.php
<pre>
<?php var_dump($r);?>
</pre>

CodeIgniter - update single record by ID - failing

Am a new user to CodeIgniter, have looked at other posts and the answers given sadly don't solve this issue.
Am using a form to create records (also uploads image file and creates link to image as field in db) This all works. I am now trying to edit a particular record. I have a view with the form and this works and is pre-filled from the db using $id = $this->uri->segment(3) to grab the correct record - this all works.
Where it fails is then in updating the record.
My controller...
function edit_stock_vehicle_record()
{
$this->load->library('form_validation');
// field name, error message, validation rules
$this->form_validation->set_rules('title', 'Title', 'trim|required');
$this->form_validation->set_rules('make', 'Make', 'trim|required');
$this->form_validation->set_rules('model', 'Model', 'trim|required');
$this->form_validation->set_rules('fuel', 'Fuel Type', 'trim|required');
$this->form_validation->set_rules('enginesize', 'Engine Size', 'trim|required');
$this->form_validation->set_rules('trans', 'Transmission', 'trim|required');
$this->form_validation->set_rules('reg_no', 'Registration Number', 'trim|required');
$this->form_validation->set_rules('year', 'Year', 'trim|required');
$this->form_validation->set_rules('colour', 'Colour', 'trim|required');
$this->form_validation->set_rules('mileage', 'Mileage', 'trim|required');
$this->form_validation->set_rules('long_desc', 'Description', 'trim|required');
$this->form_validation->set_rules('purchase_price', 'Purchase Price', 'trim|required');
$this->form_validation->set_rules('sale_price', 'Sale Price', 'trim|required');
if($this->form_validation->run() == FALSE)
{
$data['main_content'] = 'vehicle_display2';
$this->load->view('includes/template', $data);
}
else
{
$this->load->model('manage_model');
if($query = $this->manage_model->edit_stock_vehicle_record())
{
$data['main_content'] = 'vehicle_added';
$this->load->view('includes/template', $data);
}
else
{
$data['main_content'] = 'add_vehicle4';
$this->load->view('includes/template', $data);
}
}
}
My model....
function edit_stock_vehicle_record()
{
$this->load->helper('array');
$this->load->helper('html');
$id = $this->uri->segment(3);
$config['upload_path'] = 'c:/wamp/www/honest/images';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '5000';
$config['overwrite'] = TRUE;
$config['remove_spaces'] = TRUE;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$data['main_content'] = 'imageupload';
$this->load->view('includes/template', $data);
echo "FAILED";
}
else
{
$image_data = $this->upload->data();
$vehicle_update_data = array(
'title' => $this->input->post('title'),
'make' => $this->input->post('make'),
'model' => $this->input->post('model'),
'fuel' => $this->input->post('fuel'),
'enginesize' => $this->input->post('enginesize'),
'trans' => $this->input->post('trans'),
'reg_no' => $this->input->post('reg_no'),
'year' => $this->input->post('year'),
'colour' => $this->input->post('colour'),
'mileage' => $this->input->post('mileage'),
'long_desc' => $this->input->post('long_desc'),
'purchase_price' => $this->input->post('purchase_price'),
'sale_price' => $this->input->post('sale_price'),
'image' => $image_data['file_name']
);
$this->load->helper('url');
$id = $this->uri->segment(3);
$update = $this->db->update('stock_vehicles', $vehicle_update_data, array('id' => $id));
return $update;
}
}
I have also tried versions using....
$this->db->where('id', $id);
$this->db->update('stock_vehicles', $vehicle_update_data);
But this also fails, it seems as if the $id is not being recognized correctly.
If I remove the above and just use $this->db->update('stock_vehicles', $vehicle_update_data); it does indeed update every record in the db (doh!) Fortunately there was no 'real' data in there when I discovered this!
Not sure why it is not picking up the $id - all input gratefully received.
Cheers,
DP.
I have a view with the form and this works and is pre-filled from the
db using $id = $this->uri->segment(3) to grab the correct record -
this all works.
Do NOT do this for the form update. Just include the id in a hidden form field. Much easier, safer, etc.
echo form_hidden( 'id', $id );
first you must load the url helper in your Controller:
$this->load->helper('url');
then you should try this code:
$id = $this->uri->segment(3);
$vehicle_update_data =array(
'title' => $this->input->post('title'),
'make' => $this->input->post('make'),
'model' => $this->input->post('model'),
'fuel' => $this->input->post('fuel'),
'enginesize' => $this->input->post('enginesize'),
'trans' => $this->input->post('trans'),
'reg_no' => $this->input->post('reg_no'),
'year' => $this->input->post('year'),
'colour' => $this->input->post('colour'),
'mileage' => $this->input->post('mileage'),
'long_desc' => $this->input->post('long_desc'),
'purchase_price' => $this->input->post('purchase_price'),
'sale_price' => $this->input->post('sale_price'),
'image' => $image_data['file_name']
);
$this->db->WHERE('id',$id)->update('your_table',$vehicle_update_data);
return true;

Undefined method in Code Igniter framework

I've problem in CodeIgniter framework,
Controller :
public function tambah() {
$this->form_validation->set_rules('judul', 'Judul', 'required');
$this->form_validation->set_rules('deskripsi', 'Deskripsi','required');
$this->form_validation->set_rules('isi', 'Isi', 'required');
if ($this->form_validation->run() === FALSE) {
$data=array('title'=>'Menambah Berita',
'isi' =>'admin/berita/tambah_berita'
);
$this->load->view('admin/layout/wrapper',$data);
}else{
$tag = url_title($this->input->post('judul'), 'dash', TRUE);
$data = array(
'judul' => $this->input->post('judul'),
'tag' => $tag,//edited
'deskripsi' => $this->input->post('deskripsi'),
'isi' => $this->input->post('isi'),
'status' => $this->input->post('status'),
'id_admin' => $this->input->post('id_admin')
);
$this->berita_model->tambah($data);
redirect(base_url().'admin/berita/');
}
}
Model :
public function tambah($data) {
return $this->db->insert('lm_destination', $data);
}
View : view file has fixed.
Those code yields :
Fatal error: Call to undefined method Berita_model::tambah() in C:.\application\controllers\admin\file.php on line 41
Anybody can help me fix it? thanks
You must write
$this->load->model('berita_model');
before use
$this->berita_model->tambah($data);
I hope this will help YOu
Controller :
public function tambah() {
$this->form_validation->set_rules('judul', 'Judul', 'required');
$this->form_validation->set_rules('deskripsi', 'Deskripsi','required');
$this->form_validation->set_rules('isi', 'Isi', 'required');
if ($this->form_validation->run() === FALSE) {
$data=array('title'=>'Menambah Berita',
'isi' =>'admin/berita/tambah_berita'
);
$this->load->view('admin/layout/wrapper',$data);
}else{
$tag = url_title($this->input->post('judul'), 'dash', TRUE);
$data = array(
'judul' => $this->input->post('judul'),
'tag' => $tag,//edited
'deskripsi' => $this->input->post('deskripsi'),
'isi' => $this->input->post('isi'),
'status' => $this->input->post('status'),
'id_admin' => $this->input->post('id_admin')
);
$this->load->model('berita_model');
$this->berita_model->tambah($data);
redirect(base_url().'admin/berita/');
}
}
Model :
public function tambah($data) {
$this->db->insert('lm_destination', $data);
return true;
}

Categories