Documentation

Insert

mixed
CDatabase::Ins ert(
 string table,
 array fields,
 string error_position = "",
 bool debug = false,
 int exist_id = "",
 bool ignore_errors = false
)

The method inserts a record with field values into the table. The following conditions must be met for this function to work correctly:

  • The "ID" field must be available in the table, serving as the Primary Key for this table.
  • The "ID" field for MySQL must have auto increment (if the exist_id parameter is not specified when calling the method).
  • Oracle: the sequence named like "SQ_.table" must exist.
Returns the ID of the created record or false otherwise.

Note. Если необходимо вставить запись с определенным ID и при этом указать его в параметре fields, то метод возвращает 0, при этом запись вставляется. Если необходимо, чтобы метод вернул ID, который вы указывали, то его необходимо указывать в параметре exist_id.

Note: Values of all fields must be prepared for an SQL query (for example, via CDatabase::ForSql.

Non-static method.

Parameters

. .
ParameterDescription Available fr om version
table Table name.
fields Array of field values in the "field"=>"value" format,...
error_position String indicating the CDatabase::Ins ert method call position in the code. If a SQLquery fails and the file /bitrix/php_interface/dbconn.php has the specified $DBDebug=true; variable, both error_position will be shown and SQL query will be displayed.
debug If If se t to "true", text of an SQL query is displayed.
exist_idIf the value specified in the table is a positive number, a field named "ID" with the value of exist_id is created. If this parameter is not specified, a field "ID" with the val ue of SQ_table.nextval() is added to the Oracle table.
ignore_errors If equals true, any SQL errors are ignored and the function execution is not interrupted.

See Also

Examples of use

<?
if (strlen($save)>0)
{
    if (CheckFields())
    {
        $DB->PrepareFields("b_form");
        $arFields = array(
            "TIMESTAMP_X"             => $DB->GetNowFunction(),
            "NAME"                    => "'".trim($str_NAME)."'",
            "VARNAME"                 => "'".trim($str_VARNAME)."'",
            "C_SORT"                  => "'".intval($str_C_SORT)."'",
            "FIRST_SITE_ID"           => "'".$DB->ForSql($FIRST_SITE_ID,2)."'",
            "BUTTON"                  => "'".$str_BUTTON."'",
            "DESCRIPTION"             => "'".$str_DESCRIPTION."'",
            "DESCRIPTION_TYPE"        => "'".$str_DESCRIPTION_TYPE."'",
            "SHOW_TEMPLATE"           => "'".trim($str_SHOW_TEMPLATE)."'",
            "MAIL_EVENT_TYPE"         => "'".$DB->ForSql("FORM_FILLING_".$str_VARNAME,50)."'",
            "SHOW_RESULT_TEMPLATE"    => "'".trim($str_SHOW_RESULT_TEMPLATE)."'",
            "PRINT_RESULT_TEMPLATE"   => "'".trim($str_PRINT_RESULT_TEMPLATE)."'",
            "EDIT_RESULT_TEMPLATE"    => "'".trim($str_EDIT_RESULT_TEMPLATE)."'",
            "FILTER_RESULT_TEMPLATE"  => "'".trim($str_FILTER_RESULT_TEMPLATE)."'",
            "TABLE_RESULT_TEMPLATE"   => "'".trim($str_TABLE_RESULT_TEMPLATE)."'",
            "STAT_EVENT1"             => "'".trim($str_STAT_EVENT1)."'",
            "STAT_EVENT2"             => "'".trim($str_STAT_EVENT2)."'",
            "STAT_EVENT3"             => "'".trim($str_STAT_EVENT3)."'"
            );
		$DB->StartTransaction();
        if ($ID>0) 
        {
            $DB->Update("b_form", $arFields, "WH ERE ID='".$ID."'", $err_mess.__LINE__);
        }
        else 
        {
            $ID = $DB->Insert("b_form", $arFields, $err_mess.__LINE__);
            $new="Y";
        }
        $ID = intval($ID);
        if (strlen($strError)<=0) 
        {
            $DB->Commit();
            if (strlen($save)>0) LocalRedirect("form_list.php?lang=".LANGUAGE_ID);
            elseif ($new=="Y") LocalRedirect("form_edit.php?lang=".LANGUAGE_ID."&ID=".$ID);
        }
        else $DB->Rollback();
    }
}
?>


© «Bitrix24», 2001-2024
Up