Welcome to the nuBuilder forums!

Please register and login to view forums and other content only available to registered users.

[SOLVED] Double run of php. How to avoid it?

Post Reply
koyan
Posts: 3
Joined: Tue Nov 08, 2016 10:44 am

[SOLVED] Double run of php. How to avoid it?

Unread post by koyan »

Hi there,
I am trying to add missing records before opening a form. My example code is the following:

Code: Select all

$sql = "SELECT `telephone`
        FROM `employees` WHERE `telephone` not in (SELECT telephone FROM `telephones`)
        GROUP BY `telephone`";
$qry = nuRunQuery($sql);
$arr = db_fetch_array($qry);

foreach ($arr as $telephone) {
    $newID = nuID();
    $addsql = "INSERT INTO telephones (telephones_id, telephone, id) VALUES ('" . $newID . "', '" . $telephone . "' , 0)";
    nuRunQuery($addsql);
}
It does not throw an error, but it adds each record twice.
I run the above code in the "Before Browse" code block of the form.
The first time I run it, it adds each missing record twice.
If I go back (to the home page), and try to open again the browse of that form, the whole application stucks, and the only thing I can do is reload the page (which forces me to relogin)

Several questions
1) Any ideas why it runs twice?
2) How can I debug it? What tools are you using?
3) How can I avoid the need to login from scratch when something goes wrong and no links work?

Thanks
Knostantinos
Last edited by koyan on Tue Nov 08, 2016 1:44 pm, edited 1 time in total.
koyan
Posts: 3
Joined: Tue Nov 08, 2016 10:44 am

Re: Double run of php. How to avoid it?

Unread post by koyan »

Ok, I think I am using the db_fetch_array($qry) wrong.

It seems like it does not return all the rows of my query, but only the first one.

Is there a nuBuilder function to return all the results (all the rows) of my query?
koyan
Posts: 3
Joined: Tue Nov 08, 2016 10:44 am

Re: Double run of php. How to avoid it?

Unread post by koyan »

Ok, I still havent found how to avoid the double run of the browse action, but the correct code to make everything work as I wanted was this one:

Code: Select all

$sql = "SELECT `telephone` FROM `employees` WHERE `telephone` not in (SELECT telephone FROM `telephones`)
        GROUP BY `telephone`
         ";
$qry = nuRunQuery($sql);

while($arr = db_fetch_row($qry)){
    $telephone = $arr[0];
    $newID = nuID();
    $addsql = "INSERT INTO telephones (telephones_id, telephone, id) VALUES ('" . $newID . "', '" . $telephone . "' , 0)";
    nuRunQuery($addsql);
}
admin
Site Admin
Posts: 2778
Joined: Mon Jun 15, 2009 2:23 am
nuBuilder Version: 4.5

Re: [SOLVED] Double run of php. How to avoid it?

Unread post by admin »

koyan,

When you open a new (blank record) it is defined by -1 as the record number.

When you hit save (if its -1) it will save a new record.

So manually doing it means its done twice.

Steven
Post Reply