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
}
} ?>
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
How to insert same data into database after update in ORM?
For example i have function save() and inside this function i have update and it's working, but i don't know how to make insert with old update data. I mean it should make something like history in database. I only hope you can understand what i mean. Thanks for help.
public function save(Validation $validation = NULL)
{
if ($this->loaded()) {
// UPDATE TRIGGER
DB::update($this->_table_name)
->set(array('ud_status' => 'D'))
->where('ud_status', '=', 'A')
->where('ud_uId', '=', $this->ud_uId)
->execute($this->_db);
return false;
} else {
// INSERT TRIGGER
return parent::save($validation);
}
}
$query = DB::insert('users', array('username', 'password'))->values(array('fred', 'p#5sW0Rd'));
https://kohanaframework.org/3.3/guide/database/query/builder#insert
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'm using apache, php, mysql and code igniter and I have a database set up. In the database I have two columns that both need to be unique in combo: latitude and longitude.
So I set up the database using this call:
alter table pano_raw add unique index(lat, lng)
Now when I insert values into the two columns (lat and lng) if the value is the same, the database should reject the write correct?
function addNewSet($data)
{
$this->db->insert('sets', $data);
}
This function should return a true or false, depending on the success of the read or write correct?
So now how so I alter it so that when it will return the row_id of the inserted record or return FALSE? If the write worked, I need the row_id, if it didn't I need FALSE so I can alter the code that called this function.
Would this work? I don't think so. Ideas?
function addNewSet($data)
{
$this->db->insert('sets', $data);
return $this->db->insert_id();
}
By default it wont return false, it will have a database error before returning false.
If you really want this, you need to go to application/config/database.php and change set this to false $db['default']['db_debug'] = FALSE;
Then you should be able to do this (assuming your table has an id field set to auto increment)
function addNewSet($data) {
if ($this->db->insert('sets', $data)) {
return $this->db->insert_id();
} else {
return FALSE;
}
}
Try this:
function addNewSet($data)
{
$result = $this->db->insert('sets', $data);
if ( $result )
return $this->db->insert_id();
return false;
}
You can use $this->db->affected_rows(); after inserting the data to check if any record was added (or affected).
So
function addNewSet($data)
{
$this->db->insert('sets', $data);
if($this->db->affected_rows()==1)
{
return $this->db->insert_id();
}
else
{
return false; // nothing was added!
}
}
Should do it (untested).
I am not having much luck detecting when a database query in Codeigniter returns zero results. I have had a good read of the notes on the PHP count function but am none the wiser!
I call the query/view as follows from the controller:
$data['result'] = $this->search_model->do_search(set_value('name'));
$data['title'] = "Search results";
$this->load->view('search_view',$data);
The view generates a results table for me OK, but when I try and trap an empty result, the count always returns 1:
I have tried if count(array($result)) and just if count($result)
So what's a good way to get the count? I'm using Fedora 13 with PHP 5.3.3 on my dev laptop.
Have a look at $query->num_rows (<- clickable).
The best thing to do in your model is the following:
$query = $this->db->something()....
...
...
if ( $query->num_rows() > 0 )
{
return $query->result();
}
else
{
return FALSE;
}
Then in your controller or view you would do the following:
if ( !empty($my_db_result) )
{
......
}
This process enables you to respond on the result based on the result type. If the rows could be retrieved this will return an array of which the items can be counted by PHP's count() function. Since the second block checks if the result is empty (note that "FALSE" is treated as being empty) you won't bump into any issues (e.g. when using a foreach loop) and you can specify what to do in case there were no results.
Try if(isset($result) && count($result)) on your view file then inside the if statement you can write the code you want to executed when the inserts in your db are more than 0...good luck!
If you put count($result) in if statement then when it succeds, it returns only 1.
You can try
$query->num_rows() in a different way.
for example i count to show admins connected ans users connected
Database
in users i add table : [status] [int] [1]
in users also i have : [role] [varchar] [255]
Update statut to 1 (onligne) in login validation()
if($this->model_users->can_log_in($email,$pass)){
$update = array('status' => 1);
$this->model_users->update_onligne($email,$update);
redirect('main/members');
and the model :
public function update_onligne($email,$update){
$this->db->where('email',$email);
$this->db->update('users',$update);
return true;
}
Update status to offline in logout()
Logout controller :
public function logout(){
$id = $this->session->userdata('id');
$update = array('status' =>0);
$this->model_users->logout($id,$update);
$this->session->sess_destroy();
redirect('main/login');
}
Logout model :
public function logout($id,$update){
$this->db->where('id',$id);
$this->db->update('users', $update);
return;
}
Count Onligne :
The Controller :
$data['admin_onligne'] = $this->model_users->count_onligne_admin();
$data['user_onligne'] = $this->model_users->count_onligne_users();
$this->load->view('template/page_left',$data);
The Model :
public function count_onligne_admin(){
$query = $this->db->query('SELECT COUNT(status) AS enligneadmin FROM users WHERE status=1 AND role="admin"')->row_object();
return $query->enligneadmin;
}
public function count_onligne_users(){
$query = $this->db->query('SELECT COUNT(status) AS enligneuser FROM users WHERE status=1 AND role="etudiant"')->row_object();
return $query->enligneuser;
}
The Viewer
<span><?php echo $user_onligne ;?> User en ligne</span>
<span><?php echo $admin_onligne ;?> Admin en ligne</span>
i'm dowing this and it workd for me
Controller
$id = $this->session->userdata('id');
if($this->model_users->if_user_dont_have_email($id)){
....
}
Model Users
public function if_user_dont_have_email($id){
$query = $this->db->query("SELECT email FROM users WHERE id='$id'");
if ($query->num_rows() > 0) {
$row = $query->row_array();
if(empty($row['email'])){
return true;
}else{
return false;
}
}
}