DB::insert (method)

Last updated: Tue, Oct 31, 2023 11:23 am
Return to Knowledgebase

Purpose

Insert records into a specified database table.

Location

users/classes/DB.php

Parameters

# Parameter Data Type Required Description
1 $table string Yes Name of the database table
2 $fields=[] associative array Yes Field names as keys and corresponding values to be inserted
3 $update=false bool No If true, handles duplicate key conflicts

Returns

Data Type Description of Returned Data
bool Returns "true" if insert is successful; "false" if unsuccessful

Example

$db->insert('users', $fields, true)

Further Documentation:


// Example 1: Inserting a record into the 'users' table
$fields = [
'username' => 'john_doe',
'email' => 'john_doe@example.com',
'password' => 'password123'
];
$result = $db->insert('users', $fields);

// Example 2: Inserting with the update on duplicate key
$result = $db->insert('users', $fields, true);