CodeIgniter insert two rows instead one - php

I have an issue with CodeIgniter. For some reason it insert two rows in database instead one.
Below is code from model:
/**
* Add new fuel
*/
public function addNewFuel($id = NULL, $data) {
if ($id) {
var_dump('TEST');
$data = array(
'price' => '333' ,
'fuel' => 'dizelka' ,
'petrol_id' => '66'
);
echo '<pre>';
print_r($data);
echo '</pre>';
$this->db->insert('prices', $data);
echo $this->db->last_query();
$this->db->insert('prices_archive',$data);
return;
}
}
And here is output:
string(4) "TEST"
Array
(
[price] => 333
[fuel] => dizelka
[petrol_id] => 66
)
EDIT:
This is code from controller which calls model's function:
function addnewfuel() {
if ($this->session->userdata('logged_in')) {
$this->load->helper('form');
$id = $this->uri->segment(3);
$fuel = $this->input->post('fuel');
$price = $this->input->post('price');
$addNewFuel = array('fuel' => $fuel, 'price' => $price);
$data['addNewFuel'] = $this->Adminarea_model->addNewFuel($id,$addNewFuel);
$data['getFuels'] = $this->Adminarea_model->getFuels();
$data['getPetrolNameByID'] = $this->Adminarea_model->getPetrolNameByID();
$data['getPetrolInformation'] = $this->Adminarea_model->getPetrolInformation();
$this->load->view('admin/edit', $data);
} else {
//If no session, redirect to login page
redirect('login', 'refresh');
}
}
For some reasons in a table 'prices' I'm getting two rows with same data, and I really don't have idea why that happens.
Thanks

You are inserting data both from controller as well as from model.
remove this from model, hope it ill solve the problem. Thanks
$data = array(
'price' => '333' ,
'fuel' => 'dizelka' ,
'petrol_id' => '66'
);

Related

How can I get the value from another form

now i'm trying to get the value from another form. But i don't know how to make it.
This is my first form:
and this is method, when we click submit in the the form1, the page will redirect to this method.
public function input_data_inserted()
{
$id_data_inserted = $this->input->post('id_data_inserted');
$date = $this->input->post('date');
$id_data = $this->input->post('id_data');
$amount= $this->input->post('amount');
$id_room = $this->input->post('id_room');
$passed = FALSE;
$checkIdData = $this->modelku->cek_idData()->result_array();
$checkIdRoom = $this->modelku->cek_idRoom()->result_array();
foreach ($cekIdData as $cID)
{
foreach ($cekIdRoom as $cIR)
{
if ($id_data == $cID['id_data'] && $id_room == $cIR['id_room'])
{
$data = array
(
'id_data_inserted' => $id_data_inserted,
'date' => $date,
'id_data' => $cID['id_data'],
'amount' => $amount,
'id_room' => $cIR['id_room'],
);
$this->modelku->input_data($data, 'table_data_inserted');
$data['table_data_inserted'] = $this->modelku->show_data('table_data_inserted')->result();
$this->modelku->increase_value($amount, $id_data);
//redirect to second form
redirect('admin/Data/data\view_detail_data');
$passed = TRUE;
break;
}
}
}
if(!$passed)
{
echo "ERORR";
}
}
And this is my second form:
Method for second form:
public function input_detail_barang()
{
$id_data = $this->input->post('id_data');
$no_inv = $this->input->post('no_inv');
$condition = $this->input->post('condition');
$data = array
(
'id_data' => $id_data,
'no_inv' => $no_inv,
'condition' => $condition
);
//I need amount from first form, because i will use it. How can i do?
}
Any Solution? Thx
After this line you can save it to session:
$data = array (
'id_data_inserted' => $id_data_inserted,
'date' => $date,
'id_data' => $cID['id_data'],
'amount' => $amount,
'id_room' => $cIR['id_room'], // You missed single quote here
);
$this->load->library('session');
$this->session->set_userdata("Firstformdata",$data);
And for show this data add this line:
$this->session->userdata("Firstformdata");

how to display dynamic array values to view from model in codeigniter

here is my model code:
foreach ($query->result() as $row)
{
$data = array(
'ptitle' =>$row->ptitle,
'technology' => $row->technology,
'description' => $row->description,
);
$this->session->set_userdata('project',$data);
}
here is my View code:
<?php
if (isset($this->session->userdata['project']))
{
$ptitle = ($this->session->userdata['project']['ptitle']);
$technology = ($this->session->userdata['project']['technology']);
$description = ($this->session->userdata['project']['description']);
}
?>
when i print array it displays
Array ( [ptitle] => opmp [technology] => hbh [description] => kg ) Array ( [ptitle] => icicse [technology] => vv [description] => bhjv ) .can someone help me to print this values in view
Setting session should be like this
$data = array(
'ptitle' => $row['ptitle'],
'technology' => $row['technology'],
'description' => $row['description'],
);
$this->session->set_userdata($data);
To retrive them use
if (isset($this->session->userdata['project']))
{
$ptitle = $this->session->userdata('ptitle');
$technology = $this->session->userdata('technology');
$description = $this->session->userdata('description');
}
If your are storing multiple as array in SESSION then you surely need foreach in your view too.
Change from
if(isset($this->session->userdata['project']))
{
$ptitle = ($this->session->userdata['project']['ptitle']);
$technology = ($this->session->userdata['project']['technology']);
$description = ($this->session->userdata['project']['description']);
}
into
if(isset($this->session->userdata['project']))
{
foreach($this->session->userdata['project'] as $project)
{
$ptitle = $project['ptitle'];
$technology = $project['technology'];
$description = $project['description'];
echo $ptitle.'<br>'.$technology.'<br>'.$description.'<br>';
}
}
Try setting your array like bellow and then pass it to session set data.
$project = array("project" =>
array(
'ptitle' => "ptitle",
'technology' => "technology",
'description' => "description",
)
);
$this->session->set_userdata($project);

How to create multiple conditioner login in codeigniter

my model is here.....but i need to select status of admin ....... but
i m new in codeigniter....and don't no how to select... my need is...
select admin whole detail from table on condition admin status =
active and id=1...
my model is :
public function login($value) {
$query = $this->db->get_where('tbl_admin', $value, 1, 'active');
if ($query->num_rows() > 0) {
$row = $query->row_array();
$sess_arr = array(
'admin_user' => $row['fld_admin_username'],
'adm_key' => $row['fld_admin_key'],
'admin_type' => $row['fld_admin_type'],
'admin_id' => $row['fld_admin_id'],
'admin_logged_in' => TRUE
);
$this->session->set_userdata($sess_arr);
//echo "<pre>";print_r($this->session->all_userdata());exit;
}
else{
$this->session->set_flashdata('error', 'Invalid username/password');
redirect('adminzone');
}
}
The correct syntax for the first line would be:
$query = $this->db->get_where('tbl_admin', array('id' => 1, 'status' => 'active'));
I.e. The second parameter to get_where is an associative array of fields and their values..
Edit: Or perhaps it should be
$query = $this->db->get_where('tbl_admin', array('id' => $value, 'status' => 'active'));
(I am not sure what the $value variable is for here).

Issue with saving an update information in Codeigniter

My goal is to update job_contract.
There are two ways that this SHOULD be done. 1. through the client's page AND 2. through the provider's page.
What I currently have is in my job model is:
public function update_job_contract($post_obj)
{
$id = $post_obj['id'];
$data = array
(
'client_feedback' => $post_obj['client_feedback'],
'client_notetoself' => $post_obj['client_notetoself'],
'contract_status' => $post_obj['contract_status'],
'client_id' => $this->auth_model->get_user_id()
);
$this->db->insert('job', $data);
}
public function provider_update_job_contract($post_obj)
{
$id = $post_obj['id'];
$data = array
(
'provider_feedback' => $post_obj['provider_feedback'],
'provider_notetoself' => $post_obj['provider_notetoself'],
'provider_id' => $this->auth_model->get_user_id()
);
$this->db->insert('job', $data);
}
I have the following lines in my client controller page:
public function update_job_contract()
{
$this->validateRole('client');
$this->load->model('job_model');
$id = $this->uri->segment(3,0);
$data['job'] = $this->job_model->get_job($id);
$this->load->view('client/update_job_contract', $data);
}
public function update_job_contract_submit()
{
$this->validateRole('client');
$this->load->model('job_model');
if ( '0' == $_POST['id'] ) {
$this->job_model->update_job_contract($_POST);
//}
redirect('client/manage_job_contracts?message=Congratulations!');
}
And this in my provider controller page:
public function provider_update_job_contract()
{
$this->validateRole('provider');
$this->load->model('job_model');
$id = $this->uri->segment(3,0);
$data['job'] = $this->job_model->get_job($id);
$this->load->view('provider/provider_update_job_contract', $data);
}
public function provider_update_job_contract_submit()
{
$this->validateRole('provider');
$this->load->model('job_model');
if ( '0' == $_POST['id'] ) {
$this->job_model->provider_update_job_contract($_POST);
}
redirect('provider/job_contracts?message=Congratulations!');
}
Problem is, they don't really update the entries. Please help..
You are using $this->db->insert(), but with Active Record to update the row you should be using $this->db->update()
$data = array(
'title' => $title,
'name' => $name,
'date' => $date
);
$this->db->where('id', $id);
$this->db->update('mytable', $data);
Another words your functions should look like this:
public function update_job_contract($post_obj)
{
$id = $post_obj['id'];
$data = array
(
'client_feedback' => $post_obj['client_feedback'],
'client_notetoself' => $post_obj['client_notetoself'],
'contract_status' => $post_obj['contract_status'],
'client_id' => $this->auth_model->get_user_id()
);
$this->db->where('id', $id);
$this->db->update('job', $data);
}
public function provider_update_job_contract($post_obj)
{
$id = $post_obj['id'];
$data = array
(
'provider_feedback' => $post_obj['provider_feedback'],
'provider_notetoself' => $post_obj['provider_notetoself'],
'provider_id' => $this->auth_model->get_user_id()
);
$this->db->where('id', $id);
$this->db->update('job', $data);
}
Also, inside the controllers $_POST should be replaced with $this->input->post()
Using this one as example:
public function provider_update_job_contract_submit()
{
$this->validateRole('provider');
$this->load->model('job_model');
$post = $this->input->post();
if ( '0' == $post['id'] )
{
$this->job_model->provider_update_job_contract($post);
}
redirect('provider/job_contracts?message=Congratulations!');
}
Make sure you update other controllers in the same way

How to add Array to database in codeigniter?

I have a form with a multiselect field. I choose 3 categories for each company, but only one is added to the database.
How can I add an Array of 3 categories to my database? I use a joined table to add multiple categories to a company.
My table structure:
companies
---------
companyid
companyname
etc etc
categories
---------
categoryid
categoryname
companycategories
----------------
companycategoryid
categoryid
companyid
My controller:
function update()
{
$id = $this->uri->segment(3);
$data = array(
'Bedrijfsnaam' => $this->input->post('Bedrijfsnaam'),
'Postcode' => $this->input->post('Postcode'),
'Plaats' => $this->input->post('Plaats'),
'Telefoonnummer' => $this->input->post('Telefoonnummer'),
'Email' => $this->input->post('Email'),
'Website' => $this->input->post('Website'),
'Profiel' => $this->input->post('Profiel'),
'Adres' => $this->input->post('Adres'),
);
if($this->input->post('logo')) { $data['logo'] = $this->input->post('logo'); }
$this->members_model->updatebedrijf($id, $data);
$b = $this->session->userdata('idbedrijven');
redirect("members/$b");
}
My model:
function updatebedrijf($id, $data)
{
$this->db->where('idbedrijven', $id);
$this->db->update('bedrijven', $data);
$to_bedrijfcategorieen2['idcategorieen'] = $this->input->post('categorieen');
$this->insert_bedrijfcat1($to_bedrijfcategorieen2);
}
function insert_bedrijfcat1($data1)
{
echo '<pre>';
print_r($data1);
echo '</pre>';
$id = $this->uri->segment(3);
$this->db->where('idbedrijven', $id);
$this->db->update('bedrijfcategorieen', $data1);
return $this->db->affected_rows() >= 1 ? TRUE : FALSE;
}
My form:
<tr>
<td><?= form_label('Categorieen'); ?></td>
<td><?= form_multiselect('categorieen[]', $opties, key($selectie)); ?></td>
</tr>
The output of print_r($data1); gives me this:
Array
(
[idcategorieen] => Array
(
[0] => 11
[1] => 12
[2] => 13
)
)
Hope it is clear.
To Update one to many relation here is the technique
function insert_bedrijfcat1($data1)
{
$delete = $this->db->query("DELETE FROM `bedrijfcategorieen` WHERE `idbedrijven` = '" . $id. "'");
$id = $this->uri->segment(3);
foreach($data1['idcategorieen'] as $cat_id){
$this->db->query("INSERT INTO `bedrijfcategorieen` (`idbedrijven`,`idcategorieen` ) VALUES ('".$id."','".$cat_id."')");
}
}

Categories