Duplicate records

I created a button with php code to duplicate a record in a single record form, what i need to know is how to get the last position or record added. Currently the application goes to the first record.

thanks in advance

Best regards.

Re: Duplicate records

is there a primary id field?? if so, you could get the ID when you do the first insert and set the ID as a global if you have to, then you know which record you need to duplicate.

Re: Duplicate records

Try something like this:

$new_record_id = duplicate_record(‘mytable’,‘mykeyfield’,‘currentkeyvalue’);

function duplicate_record($table,$key_field,$key_value) {
$id_new = -1;

$sql = ‘SHOW COLUMNS FROM ‘.$table.’;’;
sc_lookup(field_names,$sql);

if ({field_names} === false) {
echo 'ERROR: Unable to access table: '.$table;
} elseif (empty({field_names})) {
echo ‘ERROR: Unable to get field names!’;
} else {
// get the field list
$field_array = {field_names};
$field_count = count($field_array);
// start insert query
$query = 'INSERT INTO ’ . $table . ’ (SELECT ';
// build field list for query
$i = 0;
while ($i < $field_count) {
if ($field_array[$i][0] == $key_field) {
$query .= 'NULL, ';
} else {
$query .= $field_array[$i][0] . ', ‘;
}
$i++;
}
// remove extra chars,
$query = substr($query, 0, strlen($query) - 2);
$query .= ’ FROM ’ . $table . ’ WHERE ’ . $key_field . ’ = "’ . $key_value . ‘")’;
// insert new record
sc_exec_sql($query);
// return new id
$id_new = mysql_insert_id();
return $id_new;
}
}