DB Read

littleboylb 16 Jul, 2014
Goodafternoon,
I am trying to create a form that can be called back later by the user.

I used DB Read with the condition:

<?php
return array("id" => $form->data("id"));
?>

When id = an existing record, the other fields are loaded, everything is ok. When there is no existing records or when i omit "&id=1" for example there is a warning:
Warning:array_merge():Argument #2 is not an array in ....

Can anyone help me how to avoid this warning?

Another question: Is there any way to avoid using directly the "ID" field to recall a form? I am unable to find out how to add values to uniq_id? I am thinking of adding a hidden field with random string and then join it with the ID to make it really uinque and then save it as a normal field.

Thank you in advance
GreyHead 17 Jul, 2014
1 Likes
Hi littleboylb,

You need to add a check that the value of id is set:
<?php
if ( isset($form->data['id']) && (int) $form->data['id'] > 0 ) {
  return array("id" => $form->data("id"));
} else {
  return array( 1 => 2 ); // this will find no results
}
?>

I prefer to use a short random string to identify records, I had a UniqueID [GH] action for CFv4 that would create them; I haven't yet updated it for CFv5.

Bob
littleboylb 18 Jul, 2014
Hi Bob,
Thank you for your reply.
I tried you code.
* When i try it with Multi read = No and modal ID= YES , and give the "&id=2" a blank form is displayed. No data is recalled.
* With Multiread = NO and Modal ID = NO, without any arguments, the form displays first record always. With argument it displays the correct data, with an inexistent ID the array error is displayed.

Regarding the short random string: I tried the custom field that is loaded with a random string:
<?php echo $unique_id= substr(md5(uniqid(rand(), true)), 0, 32); ?>
named it unique_id, created manualy a field with the same name during the table creation. But this value is not saved during DBSAVE event. Any Idea?
Thank you for your kind help!
K
littleboylb 18 Jul, 2014
Hi Bob,
I was successful in aving a uinque id:

i added a custom action before DB Save with the code:
<?php
$form->data['unique_id'] =  substr(md5(uniqid(rand(), true)), 0, 32);
?>

it updates the field "unique_id" in the database with this random string.

The first problem still remains: I see the first record if i call the form without parameters.

Thank you.

K
This topic is locked and no more replies can be posted.