- v5:userguide:learn_abstraction:using_execute|Using Execute ^ v5:userguide:learn_abstraction:start_lesson|Start Of Lesson ^ ->
~~NOTOC~~
====== Inserting And Updating Records ======
===== AutoExecute =====
ADOdb provides a helper routine, [[v5:reference:connection:autoexecute|autoExecute()]]. It simplifies the task of inserting and updating records by providing a structured, easy to understand methodology.
==== Inserting A Record ====
In order to insert a record, it is sufficient to know:
- The name of the table to insert the data into
- The name of the fields to fill
In the following example, the table to update is called **people**, and the field names in the table are **first_name** and **last_name**.
/*
* We must declare a variable to hold the name of the table
*/
$table = 'people';
$record = array();
$record["first_name"] = "Carol";
$record["last_name"] = "Smith";
$db->autoExecute($table,$record,'INSERT');
/*
* result executes "INSERT INTO people (first_name,last_name)
values ('Carol',Smith')";
*/
AutoExecute helps, because it can automatically determine the type of the field to be created, quoting data appropriately, and can even handle special characters in the fields.
==== Updating A Record ====
To update a record, we must also know how to retrieve the record uniquely, for example there might be a sequence number on the table that uniquely identifies each one. In the following example, the requested record is identified by an id number of 305:
$table = 'people';
$record = array();
$record["first_name"] = "Caroline";
$record["last_name"] = "Jones"; # Update Caroline's lastname from Smith To Jones
$db->autoExecute($table, $record, 'UPDATE', 'id = 305');
/*
* result executes "UPDATE people SET first_name='Caroline',
* last_name='Jones'
* WHERE id=305";
*/
==== Updating Multiple Records ====
If the record match statement produces a non-unique result set, then **ALL** records that match will be updated.
$db->autoExecute($table, $record, 'UPDATE', "last_name like 'Sm%'");
===== Limitations =====
AutoExecute is designed to be used as a UX helper, and contains multiple layers of validation which may cause performance issues. For improved performance, use can be made of [[v5:reference:connection:getinsertsql|getInsertSql()]] and [[v5:reference:connection:getupdatesql|getUpdateSql()]]. In OLTP environments, consider crafting inserts/updates programatically and using [[v5:reference:connection:execute|execute()]]
**End Of Lesson**