Trongate Docs
switch to dark modeswitch to dark mode
»
»
Updating Records

Updating Records

There are thousands of perfectly valid ways to update records.  Here we'll just focus on one technique.  Please remember, none of this is carved in stone and you are encouraged to add your own ideas.

For those of you who like video tutorials, here's a tutorial covering this topic:

Video Tutorial

In this video I'm going to demonstrate how to update table records


For everyone else, here's what gets covered in the video:

First Steps

Most of the action is going to happen in our create() method, which is on the books controller.  Let's kick things off by fetching the record ID from the URL.  We can do this by calling upon Trongate's segment() method.  For additional security, we're going to immediately convert our record ID into an integer by passing in 'int' as an optional second argument.

$update_id = segment(3, 'int');

Just To Let You Know
In instances where a non numeric value is fetched from the URL (including empty strings), settype() will convert that kind of value into a 0 (zero).

Did You Know?
A suggestion recently came in from a developer by the name of Andrew Suares.  His idea was to make 'variable type' an optional second argument when calling Trongate's segment() method.  Andrew's suggestion is excellent it has now been made a part of the framework from v1.3.3028 and above.  Thank you, Andrew!

Pre-Populating Our Form

Having fetched the ID from the URL, our next goal is to pre-populate our form.  If the form has not been submitted and if the update ID is greater than zero then would should fetch the record data from the database.  In all other instances, we should fetch the form data by using the _get_data_from_post() method.  In PHP, we can achieve this with:

$submit = post('submit');
if (($submit == '') && ($update_id>0)) {
    $data = $this->_get_data_from_db($update_id);
} else {
    $data = $this->_get_data_from_post();
}

Here's the method for get fetching record data from the database.  As you can see, it accepts a record ID:

function _get_data_from_db($update_id) {
    $record_obj = $this->model->get_where($update_id);
    $data = (array) $record_obj;
    return $data;
}

Making The Headline Dynamic

Since our form now has the ability to be used for being creating and updating records, let's make our headline dynamic.  We can achieve this passing a 'headline' value into our 'create' view file.  With a simple IF / ELSE statement we can change the headline, depending on whether or not the record ID is greater than zero.  For bonus 'smug' points you can use a ternary operator and achieve the same thing as an 'IF / ELSE' statement, but with one line of code:

$data['headline'] = ($update_id>0 ? 'Update Record' : 'Create New Record');

Of course, this means that our view file should now echo out the headline, instead of having a head coded headline.  For example:

<h1><?= $headline ?></h1>

Performing The Database Update

The actual updating of the database can be carried out by calling upon the 'update()' method, which is part of Trongate's model file.

Just To Let You Know
Full details about how the update() method works can be found at: https://trongate.io/docs_m/information/the-update-method

$this->model->update($update_id, $data);

The Complete Create Method

Below is the code for our complete create() method, which is - of course - on our controller file.

function create() {

  //fetch record ID from the URL
  $update_id = segment(3, 'int');

  //check to see if form has been posted
  $submit = post('submit');

  if (($submit == '') && ($update_id>0)) {
    //fetch the record from the database
    $data = $this->_get_data_from_db($update_id);
  } else {
    $data = $this->_get_data_from_post();
  }

  $data['headline'] = ($update_id>0 ? 'Update Record' : 'Create New Record');
  $data['form_location'] = str_replace('/create', '/submit', current_url());
  $data['author_options'] = $this->_get_author_options($data['author_id']);
  $data['view_file'] = 'create';
  $this->template('admin', $data);
}

The Complete Submit Method

Below is the code for our complete submit() method, which is also to be found on our controller file:

function submit() {

  $submit = post('submit');
  if ($submit == 'Submit') {
    $this->validation_helper->set_rules('title', 'book title', 'required|min_length[3]|max_length[75]');
    $this->validation_helper->set_rules('description', 'book description', 'required');
    $this->validation_helper->set_rules('author_id', 'author', 'required');

    //run the validation tests
    $result = $this->validation_helper->run(); //returns true or false

    if ($result == true) {

    //fetch the posted data
    $data = $this->_get_data_from_post();
    settype($data['published'], 'int');

    $update_id = segment(3, 'int');

    if ($update_id>0) {
      //update the record
      $this->model->update($update_id, $data);
      $flash_msg = 'The record was successfully updated';
    } else {
      //insert into the 'books' database table
      $this->model->insert($data);
      $flash_msg = 'The record was successfully created';
    }

    set_flashdata($flash_msg);
    redirect('books/manage');

    } else {
      $this->create();
    }
  }
}

Just To Let You Know
In the code above, we're using Trongate's flashdata helper to give the user helpful messages whenever create or update events have been successfully executed.  Full details about how to use Trongate's flashdata helper can be found at: https://trongate.io/docs_m/information/the-flashdata-helper

Top Tip
Using PHP's settype() method to convert record IDs, that have been fetched from the URL, into integers is really good for site security.  Make this a habit! So, every time you accept a value from a URL or a form, and if you expect the value to be an integer, turn the value into an integer immediately.


HELP & SUPPORT

If you have a question or a comment relating to anything you've see here, please goto the Help Bar.

 
×