From d4469f2a849cdf33a1f9d1075f5fc8092176be64 Mon Sep 17 00:00:00 2001 From: moodler Date: Tue, 3 Feb 2004 15:18:41 +0000 Subject: [PATCH] Clean up of insert_record with some better error-checking --- lib/datalib.php | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/lib/datalib.php b/lib/datalib.php index a2c1d4ab3b..4d4afa8d3d 100644 --- a/lib/datalib.php +++ b/lib/datalib.php @@ -749,28 +749,29 @@ function insert_record($table, $dataobject, $returnid=true, $primarykey='id') { global $db, $CFG; - // Get empty record from table - $infosql = "SELECT * FROM $CFG->prefix$table WHERE $primarykey ='-1'"; - - // Execute the query and get the empty recordset - $rs = $db->Execute($infosql); - - // Convert data to array to hold the record data to insert - $record = (array)$dataobject; +/// Execute a dummy query to get an empty recordset + if (!$rs = $db->Execute("SELECT * FROM $CFG->prefix$table WHERE $primarykey ='-1'")) { + return false; + } - //Get insertsql from adodb - $insertSQL = $db->GetInsertSQL($rs, $record, true); +/// Get the correct SQL from adoDB + if (!$insertSQL = $db->GetInsertSQL($rs, (array)$dataobject, true)) { + return false; + } - if (! $rs = $db->Execute($insertSQL)) { +/// Run the SQL statement + if (!$rs = $db->Execute($insertSQL)) { return false; } - if (!$returnid) { // Return ID is not needed so just finish here. +/// If a return ID is not needed then just return true now + if (!$returnid) { return true; } +/// Find the return ID of the newly inserted record switch ($CFG->dbtype) { - case "postgres7": + case "postgres7": // Just loves to be special $oid = $db->Insert_ID(); if ($rs = $db->Execute("SELECT $primarykey FROM $CFG->prefix$table WHERE oid = $oid")) { if ($rs->RecordCount() == 1) { @@ -780,7 +781,7 @@ function insert_record($table, $dataobject, $returnid=true, $primarykey='id') { return false; default: - return $db->Insert_ID(); // Should work on most databases, but not all! + return $db->Insert_ID(); // Should work on most databases, but not all! } } @@ -1111,7 +1112,7 @@ function get_course_students($courseid, $sort="s.timeaccess", $dir="", $page=0, } return get_records_sql("SELECT u.id, u.username, u.firstname, u.lastname, u.maildisplay, u.mailformat, - u.email, u.city, u.country, u.lastlogin, u.picture, u.lang, u.timezone, s.timeaccess as lastaccess + u.email, u.city, u.country, u.lastlogin, u.picture, u.department, uuuuuu.lang, u.timezone, s.timeaccess as lastaccess FROM {$CFG->prefix}user u, {$CFG->prefix}user_students s $groupmembers WHERE $select -- 2.39.5