Facing problem to used multiple Where condition at update_batch in CodeIgniter.
No error message is not showing . As well as Data is not updating at database.
But after click Submit Button is giving me flash message as Data is submitted successfully. But Database could not be able to Update.
Please suggest me what should I do !
Controller ------------------------------------
public function masterPrice_update($m_fran_id = null)
{
$sID = $this->input->post('m_test_id');
$sAmt = $this->input->post('m_updated_test_price');
$sFranId = $this->input->post('m_fran_id');
for ($i= 0; $i < count($sID); $i++)
{
$edited_test[] = array(
'm_test_id' => $sID[$i],
'm_updated_test_price' => $sAmt[$i],
'm_fran_id' => $sFranId[$i]
);
}
if ($this->form_validation->run() === true) {
$this->franchise_price_model->singlebatch_Test_updt($edited_test);
$this->session->set_flashdata('message', display('save_successfully'));
redirect('branch/franchise_price/masterPrice_update');
}
}
Modal ---------------------
public function singlebatch_Test_updt($edited_test =[], $sFranId ='')
{
$this->db
->where('m_fran_id',$sFranId)
->update_batch($this->fran_test_pricemaster, $edited_test , 'm_test_id' );
}
You can use $this->db->last_query() to print query. I think you have issue in where condition from controller you are not passing $sFranId to model.
Use this type of Query
$this->db->query("UPDATE table_name SET column1 = value1, column2 = value2 WHERE condition AND condition2");
Related
I'm trying to insert array data into database using codeigniter. when I print that values using print_r() fun.
It shows correct result, but when I click on save button it inserts blank values in table.
I just want to store multiple medicine names in prescription table for particular prescription_id i am trying to insert data using insert_batch function but it saves blank records
this is my model code:
public function add_prescription($data) {
$data['medicine_name'] = $data['medicine_nm[]'];
print_r($data['medicine_name']);
$this->db->insert_batch('pre',$data);
return $this->db->insert_id($pre_id);
}
controller code--
public function prescription($patient_id = NULL, $app_date = NULL, $hour = NULL , $min = NULL) {
//Check if user has logged in
if (!$this->session->userdata('user_name') || $this->session->userdata('user_name') == '') {
redirect('login/index/');
} else {
if ($this->form_validation->run() === FALSE) {
$data['medicine_nm[]']=$this->input->post('medicine_nm[]');
$this->patient_model->add_prescription($data);
}
}
}
You need to json_encode that array and then insert it into the database like
$data = json_encode($array);
$this->db->insert('column_name',$data);
And while fetching you need to decode it
$data = json_decode($column_result);
Hope this helps you.
try this
public function add_prescription($name) {
$data = array(
'medicine_name' => $name
);
$query = $this->db->insert('table_name',$data);
return $query->result_array();
}
Hope this will help you
I trying to implement database trigger in codeigniter, I want to update one specific table after the insert operation on other specific table. I am trying the following code but it not work for me.
this is function I written in my model:
<?php
public function enter_follower($follower_id, $following_id) {
$data = array('follower_user' => $follower_id,
'following_user' => $following_id
);
$this->db->insert('followers', $data);
//return $this->db->insert_id();
$this->db->query("
CREATE TRIGGER `UpdateFollower` AFTER INSERT ON followers
FOR EACH ROW
UPDATE business_users SET followerCount = followerCount + 1 WHERE user_id = $following_id
END");
if ($this->affected_rows() > 0) {
return true
} else {
return false
}
} ?>
I have to search for multiple values in a field using mysql in codeigniter. Here follows my code.
In Controller
public function vpsearch()
{
$data['info'] = $this->psearch_m->emp_search_form();
$this->load->view("employer/result",$data);
}
IN Model
public function emp_search_form()
{
$skill = $this->security->xss_clean($this->input->post('ps_skills'));
$jrole = $this->input->post('ps_jobrole'));
if ( $jrole !== NULL)
{
return $this->db->get('js_edu_details');
$this->db->like('js_skills','$skill');
}
}
In view i.e, (../employer/result)
foreach($info->result() as $row)
{
echo $row->js_id."<br/><br/>" ;
}
However I am getting all the records in 'js_edu_details' table instead of fields having searched 'skills'.
Where I am going wrong? Any help wud b appreciated, thanx in advance.
Try:
public function emp_search_form()
{
$skill = $this->security->xss_clean($this->input->post('ps_skills'));
//$skill = $this->input->post('ps_skills', true); other short way of getting the above result with `xss clean`
if ( $jrole !== NULL)
{
$this->db->like('js_skills',$skill); #remove the single quote around the `$skill`
$res = $this->db->get('js_edu_details');
echo $this->db->last_query(); #try to print the query generated
return $res;
}
}
Return statement should be after the like statement
You should arrange the code properly like this
public function emp_search_form()
{
$ps_skills = $this->input->post('ps_skills')
$skill = $this->security->xss_clean($ps_skills);
if ( $jrole !== NULL)
{
$this->db->like('js_skills','$skill');
return $this->db->get('js_edu_details');
}
}
Also you should note the condition will never meet. It will always give error undefined variable $jrole
I am trying to get the below to work correctly. I have the else working correctly, but I am running into problems in the if part. What it's doing is deleting the row and inserting 0 into the hottest_cat row. It should be inserting the value from $dd. Any ideas as to what I'm doing wrong?
public function change_category() {
$dd = $this->input->post('dd');
$sql = $this->db->get('default_hottest_cat');
if ($sql->num_rows() > 0) {
$row = $sql->row();
$old = $row->hottest_cat;
// Stuff is found in this table. Needs to be deleted first, then inserted.
$this->db->delete('default_hottest_cat', array('hottest_cat' => $old));
$this->db->insert('default_hottest_cat', array('hottest_cat' => $dd));
} else {
// Nothing is found in this table. Needs only to be inserted.
$this->db->insert('default_hottest_cat', array('hottest_cat' => $dd));
}
}
Your is quite similiar to a problem I had answered.
Please check this for more info
How to check if id already exists - codeigniter
The logic is
1.Select "the database table" with the value you had posted.(search)
2.Check the count of result_row(if found >0 , if not found =0)
3.If not found 'insert', if found 'update',( in your case delete)
Model
<?php
class Cars_model extends CI_Model
{
function __construct()
{
parent:: __construct();
$this->load->database();
}
function check()
{
$query=null; //emptying in case
$dd= $_POST['dd']; //getting from post value
$query = $this->db->get_where('default_hottest_cat', array('dd' => $dd)); ၊၊၊။။//making selection
$count= $query->num_rows(); //counting result from query
if ($count === 0) // data is not present
{
$data = array(
'dd' => $dd
);
$this->db->insert('default_hottest_cat', $data);
}
else //data is present
{
$this->db->delete('default_hottest_cat', array('dd'=>$dd));
}
}
}
?>
i started using cakephp, but now i encountered a problem which i am not able to solve.
I have got the following model relations which are relevant:
Exercise hasMany Points
Student hasMany Points,
now i want to check in the studentsController if for every exercise there is a Point data set, and iff not insert a new one.
When starting with no Point datasets, the function adds a new Point dataset for the first exercise correct, but after this it only updates the erxercise_id of this dataset, instead of creating new ones.
The controller function looks like this:
public function correct($id = null) {
$this->Student->id = $id;
$this->Student->recursive = 2;
if ($this->request->is('get')) {
$data = $this->Student->Exam->Exercise->find('all');
foreach($data as $exercise)
{
$exerciseID = $exercise['Exercise']['id'];
$this->Student->Point->recursive = 0;
$foundPoints = $this->Student->Point->find('all', array('conditions' => array('exercise_id' => $exerciseID)));
if($foundPoints == null)
{
$emptyPoints = array ('Point' => array(
'exercise_id' => $exerciseID,
'student_id' => $id
)
);
$this->Student->Point->save($emptyPoints);
}
else{
}
}
}
else //POST
{
}
}
if you have to insert a data you to use create() method like this:
This is only an example, with this line you create every time a new record into your database
and save data
$this->Student->Point->create();
$this->Student->Point->save($emptyPoints);
$this->Student->Point->id = '';
$this->Student->Point->save($emptyPoints);
or
$this->Student->Point->saveAll($emptyPoints);