Mysqli Insert Operation

Description

Insert new records in a table using associative array. Instead of writing long insert queries, you needs to pass array of keys(columns) and values(insert values). This function will automatically create query for you and inserts data.

You need to set $parameter_types as data type of columns before using this function. For examples if you have five columns then you need to pass data type of each column like "sssis" where s denotes the string data type, i denotes numeric data type.



Function Reference :
            	void dbInsert(string $table_name, assoc. array $insert_array);
            

Parameters

Name Type Required Parameter Info Example
table_name string Yes The name of the table to insert new records any table name like employee_table,user_table
insert_array associative array Yes Associative array with key as column name and values as column value. array("username"=>"abcd","gender"=>"male","age"=>29)

Examples

  
          <?php 
          $sdb= new SimplifiedDB(); 
          $sdb->dbConnect("localhost","username","password","dbuser");
          $sdb->parameter_types="ss";
          $sdb->dbInsert("user_roles",array("role_name"=>"Contributer","role_description"=>"Contribues for the system"));
          ?> 

Result

New row will be inserted in table.

Debuging

Debug Operation Result/Output
$sdb->query INSERT INTO user_roles (`role_name`,`role_description`) values (?,?)
$sdb->last_insert_id 6
$sdb->rows_affected 1
$sdb->message_info Connected to database
$sdb->error_info

More Examples

  
         $insertRoles=array(); 
         $insertRoles["role_name"]="Manager";
         $insertRoles["role_description"]="Manages system";
         $sdb->parameter_types="ss";
         $sdb->dbInsert("user_roles",$insertRoles);