Header Small Ahoy!

Data Manipulation

The Trongate Model class provides the following methods for inserting, updating, and deleting database data:

Creating Database Records With The Insert Method

The insert() method inserts a row of data into a database table. With this method, rows of new data are represented by a PHP array. This method accepts the following parameters:

What Gets Returned?

If the method results in a new table row being inserted, the 'id' of the newly inserted record will be returned.

Example

The syntax below shows the simplest insert example possible. In this example, we create an array of data that represents a new table row. Then, we invoke the insert method to add a new row to a database table using the array we created.


$data["first_name"] = "Eric"; 
$data["last_name"] = "Clapton"; 
$data["trongate_user_id"] = 91; 
$this->model->insert($data);

The code above will produce the following SQL query:

INSERT INTO tablename (first_name, last_name, trongate_user_id) VALUES ('Eric', 'Clapton', 91)

In a working example, 'tablename' would be replaced with the first segment from the URL.

Updating Database Records With The Update Method

The update() method updates a record in a database table. This method takes the ID of the record to update, an associative array containing column names as keys and their corresponding values, and an optional parameter specifying the name of the database table to update. It constructs and executes an SQL query to perform the update, returning true if the update was successful and false otherwise. This method accepts the following parameters:

What Gets Returned?

If the method successfully updates the record, it returns true. Otherwise, it returns false.

Example

The code sample below demonstrates how to use the update() method to update a record in the database.


// ID of the record to update
$update_id = 123;

// Data to update
$data["first_name"] = "John"; 
$data["last_name"] = "Doe"; 
$data["email"] = "[email protected]"; 

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

Deleting Database Records With The Delete Method

The delete() method deletes a record from a database table based on its ID. This method takes the ID of the record to delete and an optional parameter specifying the name of the database table. It constructs and executes an SQL query to delete the record, returning true if the delete operation was successful and false otherwise. This method accepts the following parameters:

What Gets Returned?

If the method successfully deletes the record, it returns true. Otherwise, it returns false.

Example

The code sample below demonstrates how to use the delete() method to delete a record from the database.


// ID of the record to delete
$id = 123; 

// Name of the target table
$target_table = 'products'; 

$is_deleted = $this->model->delete($id, $target_table);

if ($is_deleted) {
    echo "Record with ID {$id} was successfully deleted from the {$target_table} table.";
} else {
    echo "Failed to delete record with ID {$id} from the {$target_table} table.";
}

Video Tutorial: Creating, Updating & Deleteing Database Records

The video below demonstrates how how to create, update and delete database records using Trongate's Model class.