The given syntax can insert the given values but i cannot retrieve the value with method. do anyone knoe how can i do that?
$supplier = array(
'cname' => $request->getPost('cname'),
'clname' => $request->getPost('clname'),
'tlidt' => $request->getPost('tlidt'),
'tledt' => $request->getPost('tledt'),
'pname' => $request->getPost('pname'),
'tla' => $request->getPost('tla'),
);
$builder = $db->table('suppliers');
$builder->insert($supplier);
$id = $this->$db->insert_id();
Try inserting like that
$this->db->insert('suppliers',$supplier);
$id = $this->db->insert_id();
or try removing $ sign from your last line 'db'.
Related
I am getting an error while inserting into my database.
Its showing column ARRAY. How to fix it? Instead of that I want to pass all dates (showing in the screenshot only dates in the date column)
I want to know in code where I did mistake. Why the array is coming like this?
PHP MODAL
$data_jobschedule = array(
'jobschedule_id' => $jobschedule_id,
'Activity_area_id' => $Activity_area_id,
'Contract_id' => $this->input->post('getcontract_id'),
'job_freq_id' => $this->input->post('getcontractbranch_freq'),
'job_schedule_dates' => $this->input->post('getschedule'),
//'job_schedule_frequency' => $this->input->post('getjob_schedule_frequency'),
'created_at' =>$created_Dt
);
$insert_id = 0;
if ($this->db->insert("activity_area", $data)) { //AM INSERTING ANOTHER RECORD ALSO TO DIFFERENT TABLE
$this->db->insert('job_schedule', $data_jobschedule); //HERE IS THE TABLE I NEED TO ADD DATES AND FREQUENCY
$insert_id = $this->db->insert_id();
}
so your post data is an array, you need looping through it.
on your php model, try change this
$data_jobschedule = array(
'jobschedule_id' => $jobschedule_id,
'Activity_area_id' => $Activity_area_id,
'Contract_id' => $this->input->post('getcontract_id'),
'job_freq_id' => $this->input->post('getcontractbranch_freq'),
'job_schedule_dates' => $this->input->post('getschedule'),
//'job_schedule_frequency' => $this->input->post('getjob_schedule_frequency'),
'created_at' =>$created_Dt
);
$insert_id = 0;
if ($this->db->insert("activity_area", $data)) { //AM INSERTING ANOTHER RECORD ALSO TO DIFFERENT TABLE
$this->db->insert('job_schedule', $data_jobschedule); //HERE IS THE TABLE I NEED TO ADD DATES AND FREQUENCY
$insert_id = $this->db->insert_id();
}
to
$insert_id = 0;
if ($this->db->insert("activity_area", $data)) { //AM INSERTING ANOTHER RECORD ALSO TO DIFFERENT TABLE
if (is_array($this->input->post('getschedule'))) {
foreach($this->input->post('getschedule') as $value) {
$this->db->insert('job_schedule', array(
'jobschedule_id' => $jobschedule_id,
'Activity_area_id' => $Activity_area_id,
'Contract_id' => $this->input->post('getcontract_id'),
'job_freq_id' => $this->input->post('getcontractbranch_freq'),
'job_schedule_dates' => $value[0], //assume array form like your screenshot
'job_schedule_frequency' => $value[1],
'created_at' =>$created_Dt
));
}
$insert_id = $this->db->insert_id();
}
}
When I insert a new record into a database table, I need to take an existing previous value of a column called el_order, add +1, and use that new el_order+1 to insert the new record with that value in the column.
I can't use autoincrement because I need to do some things with that column (reorder, move, etc) and have to use it as an integer.
Table
ID name el_order
1 1 1
21 bla 2
2 2 3
--NEW-- --NEW-- 3+1 (NEW)
I add a new record, and need to insert it with 3+1 in it's el_order column...
I have tried this, but no luck:
$this->db->select_max('el_order');
$res = $this->db->get('elem_diccio');
$eldi_key = url_title($this->input->post('id'), 'underscore', TRUE);
$el_order = $res+1;
$datos = array(
'ID' => $id,
'el_order' => $el_order,
'name' => $this->input->post('name'),
);
$this->db->insert('elem_diccio', $datos);
Just like this
$this->db->select_max('el_order');
$res = $this->db->get('elem_diccio')->row()->el_order;
$eldi_key = url_title($this->input->post('id'), 'underscore', TRUE);
$el_order = $res+1;
$datos = array(
'ID' => $id,
'el_order' => $el_order,
'name' => $this->input->post('name'),
);
$this->db->insert('elem_diccio', $datos);
$res is a CI_DB_mysqli_result Object. To get the column, you need
$this->db->select_max('el_order');
$res = $this->db->get('elem_diccio')->row();
$el_order = $res->el_order+1;
$datos = array(
'ID' => $id,
'el_order' => $el_order,
'name' => $this->input->post('name'),
);
query in plain php is
$p = "SELECT * FROM followusers WHERE (follower='$followed1 and
followed='$follower1') or (follower='$follower1' and
followed='$followed1')"
i have written query in yii2 is
$y = Followusers::find()->where(['follower' => $userid ] and
['followed' => $conid])->orwhere(['followed' => $userid ] and
['follower' => $conid])-> all();
but i m not getting the required result for the query in yii2
Try this way:
Followusers::find()->where(['follower' => $userid])
->andWhere(['followed' => $conid])
->orwhere(['AND',
['followed' => $userid],
['follower' => $conid]
])
-> all();
If followusers table model is FollowUsers (in the current namespace, otherwise you have to prefix with namespace), you can
$data = FollowUsers::find()
->where(['AND', 'follower='.$followed1, 'followed='.$follower1])
->orWhere(['AND', 'follower='.$follower1, 'followed='.$followed1])
->all();
Otherwise, you can usign parameters:
$data = FollowUsers::find()
->where('(follower=:followed1 AND followed=:follower1) OR (follower=:follower1 AND followed=:followed1)', [':followed1' => $followed1, ':follower1' => $follower1])
->all();
It seems like you have some syntax errors in your code, try formatting it as follows:
$y = Followusers::find()
->where(['follower' => $userid, 'followed' => $conid])
->orwhere(['followed' => $userid, 'follower' => $conid])
->all();
I want to display as a value of textbox the last id of my record and then increment it.
This is what I have:
view.php
<?php
$id = $this->db->insert_id();
$newId = $id + 1;
$data = array(
'name' => 'customercode',
'id' => 'inputCustomerCode',
'type' => 'text',
'readonly' => 'true',
'class' => 'form-control',
'value' => 'CUST0000' . $newId
);
echo form_input($data);
?>
But it's just displaying CUST00001? What am I doing wrong in here? Help is much appreciated and needed. Thanks.
you can get last inserted id with $this->db->insert_id(); if you want to use this for multiple places, please take this in a variable and use that variable.
you can use $this->db->insert_id() after insert query.
$id = $this->db->insert_id();
you can use a simple query to get max id
$query = $this->db->query("SELECT MAX (id) FROM table");
$id = $query->row()->id;
and use $id in form
'value' => 'CUST0000' . $id + 1
Instead of using $id = $this->db->insert_id(); you may use following code:
$query = $this->db->query("SELECT id FROM mytable ORDER BY id DESC LIMIT 1");
$id = $query->row()->id;
$newId = $id + 1;
$data = array(
'name' => 'customercode',
'id' => 'inputCustomerCode',
'type' => 'text',
'readonly' => 'true',
'class' => 'form-control',
'value' => 'CUST' . str_pad($id+1, 5, '0', STR_PAD_LEFT)
);
echo form_input($data);
Instead of
$this->db->insert_id()+1
You need to display $newId.
Make sure you have queried for the last Id. $this->db->insert_id() returns only when you have inserted some record. If you haven't , then you need to query to get the last inserted id. You can use this simple query.
SELECT id FROM mytable ORDER BY id DESC LIMIT 1
$this->db->insert_id()
The insert ID number when performing database inserts.
Looked at http://ellislab.com/codeigniter/user_guide/database/helpers.html and the first function is $this->db->insert_id();
This also works with active-record inserts...
I want to match course_id with $courseInfo which is fetching from $cLID.
When I change this $clID['Relationscl']['course_id'] line as 1 everything is ok, but the other way an error appears like Undefined index: Relationscl.
Here is my code:
$this->loadModel('Course');
$this->loadModel('Lecturer');
$clID = $this->Relationscl->find('all', array(
'conditions' => array(
'student_id' => $id
),
'fields' => array(
'course_id','lecturer_id'
)
));
$this->set('clIDs', $clID);
$courseInfo = $this->Course->find('first',array(
'conditions' => array(
'course_id' => $clID['Relationscl']['course_id']
),
'fields' => array(
'course_name','course_code','course_credit'
)
));
$this->set('cInfos', $courseInfo);
Correct me if I'm wrong, but the find('all') function returns and indexed array, so it should be something like
$clID = array( 0 => array('Relationscl'=>array('course_id'=>1 /*and more fields*/)),
1 => array('Relationscl'=>array('course_id'=>2 /*and more fields*/))
/*etc*/);
So clearly $clID['Relationscl'] is undefined. Try with $clID[0]['Relationscl']. Though even that seems weird, why would you do a find('all') if you only plan on using the one record, isn't find('first') better? Or set that $courseInfo definition inside a loop?