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

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

Postby koyan » Tue Nov 08, 2016 7:45 pm

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 10:14 pm, edited 1 time in total.
koyan
 
Posts: 3
Joined: Tue Nov 08, 2016 7:14 pm

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

Postby koyan » Tue Nov 08, 2016 9:24 pm

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 7:14 pm

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

Postby koyan » Tue Nov 08, 2016 10:14 pm

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);
}
koyan
 
Posts: 3
Joined: Tue Nov 08, 2016 7:14 pm

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

Postby admin » Mon Nov 14, 2016 7:00 am

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
admin
Site Admin
 
Posts: 3135
Joined: Mon Jun 15, 2009 9:53 am


Return to Custom Code



cron