I believe I have a fix for non-unique records.
In contrib/crown_college/phproof I have added a test which can (if run enough times when the stars align just right) catch this intermittent bug.
Here is the slightly modified insert_record function that ought to do the trick (I have not committed this to
CVS):
function insert_record($table, $dataobject, $returnid=true) {
/// Insert a record into a table and return the "id" field if required
/// If the return ID isn't required, then this just reports success as true/false.
/// $dataobject is an object containing needed data
global $db, $CFG;
// Determine all the fields needed
if (! $columns = $db->MetaColumns("$CFG->prefix$table")) {
return false;
}
$data = (array)$dataobject;
// Pull out data matching these fields
foreach ($columns as $column) {
if ($column->name "id" && isset($data[$column->name]) ) {
$ddd[$column->name] = $data[$column->name];
}
}
// Construct
SQL queries
if (! $numddd = count($ddd)) {
return false;
}
$count = 0;
$inscolumns = "";
$insvalues = "";
$select = "";
foreach ($ddd as $key => $value) {
$count++;
$inscolumns .= "$key";
$insvalues .= "'$value'";
$select .= "$key = '$value'";
if ($count Execute("INSERT INTO $CFG->prefix$table ($inscolumns) VALUES ($insvalues)")) {
return false;
}
if ($returnid) {
if ($db->hasInsertID) {
return $db->Insert_ID();
// Pull it out again to find the id. This is the most cross-platform method.
} elseif ($rs = $db->Execute("SELECT id FROM $CFG->prefix$table WHERE $select")) {
if ($rs->RecordCount() == 1) {
return $rs->fields[0];
} else {
return false;
}
} else {
return false;
}
} else {
return true;
}
}