Description
PDOModel update function provides a simplest way to update the data into database by passing the data in form of associative array.
You need to pass table name and array of data to update function and where condition data. Please note that you must set where
condition to update specific data else all data will be updated.
Function Defination
void function update($dbTableName, $updateData);
Parameters Details
| Details | Type | Example |
|---|---|---|
| $dbTableName - Table name to update records | string | Any table name like "orderTable", "wp_posts" etc |
| $updateData - Associative array of records to be updateed, with columnName as key and columnValue as value | assoc. array | array("orderNumber"=>1001, "customerName"=>"John Cena", "address"=>"140 B South Jercy") |
Examples
$pdomodel = new PDOModel(); //create object of the PDOModel class
$pdomodel->connect("localhost", "root", "", "pdocrud");//connect to database
/* Update function */
$pdomodel->where("orderId", 7);//setting where condition
$pdomodel->update("order", array("orderNumber"=>"44", "customerName"=>"BKG", "address"=>"140 shakti nagar"));
More Examples
//Example 1
$updateData = array("orderNumber"=>1001, "customerName"=>"John Cena", "address"=>"140 B South Jercy");
$pdomodel->where("orderId", 7);
$pdomodel->update("order", $updateData);
//Example 2
$updateEmpData["firstName"] = "Simon";
$updateEmpData["lastName"] = "jason";
$pdomodel->where("empId", 40);
$pdomodel->update("emp", $updateEmpData);
Result/Output
Record will be updated in database
Debug
| Option | Details | Example |
|---|---|---|
| $pdomodel->getLastQuery(); | Return last query executed | UPDATE `emp` SET `firstName`=?,`lastName`=?,`age`=?,`gender`=?,`status`=? WHERE `empId`= ? |
| print_r($pdomodel->error) | Print errors (if any) | |
| $pdomodel->rowsChanged; | Return Rows created/changed | 1 |