[php] Updating records codeigniter

I am trying to update a table where id of the row is entered and title is selected through a dropdown list, but I have two problems. Firstly Im not sure how to pass the data to the model, and the second problem is actually updating the table using active record.

controller class

class Update extends CI_Controller {

public function __construct()
{
        parent::__construct();
        //$this->load->model('addmodel');
        //$this->load->helper('form');
}

public function index()
{
    $this->load->view('updview');

}

public function updtitle() 
{   

    $data = array(
    'id' => $this->input->post('id'),
    'title' => $this->input->post('title') );
    //$data1['data'] = $data;

    $data1['data'] = $this->updmodel->upddata($data);
    $this->load->view('updview', $data1);
}
}

?>

model class

class Updmodel extends CI_Model {
// model constructor function
function __construct() {
    parent::__construct(); // call parent constructor
    $this->load->database();
}

public function upddata($data) {

    $this->db->where('emp_no', $data['id']);
    $this->db->update('title', $data['title']);

    return;
}
}

?>

view

Update employee title

<form action="http://localhost/ecwm604/index.php/update/updtitle" method="POST">
    employee id: 
        <input type=text name="id"><br />
    change title to: 
        <select name="title">
            <option value="Assistant Engineer">Assistant Engineer</option>
            <option value="Engineer">Engineer</option>
            <option value="Senior Engineer">Senior Engineer</option>
            <option value="Senior Staff">Senior Staff</option>
            <option value="Staff">Staff</option> 
        </select><br />
    <input type="submit" value="submit"/>
    <br />
<?php
    print_r($data);
    //echo $data['title'];
?>

</body>
</html>

This question is related to php database codeigniter sql-update

The answer is


In your_controller write this...

public function update_title() 
{   
    $data = array
      (
        'table_id' => $this->input->post('table_id'),
        'table_title' => $this->input->post('table_title')
      );

    $this->load->model('your_model'); // First load the model
    if($this->your_model->update_title($data)) // call the method from the controller
    {
        // update successful...
    }
    else
    {
        // update not successful...
    }

}

While in your_model...

public function update_title($data)
{
   $this->db->set('table_title',$data['title'])
         ->where('table_id',$data['table_id'])
        ->update('your_table');
}

This will works fine...


In codeigniter doc if you update specific field just do this

$data = array(
    'yourfieldname' => value,
    'name' => $name,
    'date' => $date
);

$this->db->where('yourfieldname', yourfieldvalue);
$this->db->update('yourtablename', $data);

How to update in codeignitor?

whenever you want to update same status with multiple rows you use where_in insteam of where or if you want to change only single record can use where.

below is my code

$conditionArray = array(1, 3, 4, 6);
$this->db->where_in("ip_id", $conditionArray);
$this->db->update($this->table, array("status" => 'active'));

its working perfect.


Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to database

Implement specialization in ER diagram phpMyAdmin - Error > Incorrect format parameter? Authentication plugin 'caching_sha2_password' cannot be loaded Room - Schema export directory is not provided to the annotation processor so we cannot export the schema SQL Query Where Date = Today Minus 7 Days MySQL Error: : 'Access denied for user 'root'@'localhost' SQL Server date format yyyymmdd How to create a foreign key in phpmyadmin WooCommerce: Finding the products in database TypeError: tuple indices must be integers, not str

Examples related to codeigniter

DataTables: Cannot read property 'length' of undefined How to set time zone in codeigniter? php codeigniter count rows CodeIgniter: 404 Page Not Found on Live Server Only variable references should be returned by reference - Codeigniter How to pass a parameter like title, summary and image in a Facebook sharer URL How to JOIN three tables in Codeigniter how to get the base url in javascript How to get id from URL in codeigniter? How to disable PHP Error reporting in CodeIgniter?

Examples related to sql-update

Update some specific field of an entity in android Room How to perform update operations on columns of type JSONB in Postgres 9.4 MySQL - UPDATE multiple rows with different values in one query How to update multiple columns in single update statement in DB2 Update Multiple Rows in Entity Framework from a list of ids Update MySQL using HTML Form and PHP CodeIgniter query: How to move a column value to another column in the same row and save the current time in the original column? mysql after insert trigger which updates another table's column Update values from one column in same table to another in SQL Server I want to use CASE statement to update some records in sql server 2005