Page 1 of 2

Trying to run a procedure from a button

Posted: Mon Mar 20, 2023 12:38 pm
by tsignadze
Hello everyone!
Forgive me if my question is somewhat stupid, I am very new to this.

I have a form where I have 3 fields : TerminalID, old_address, new_address.
form.png
When user inputs TerminalID old_address is populated automatically from database. Then user enters new_address and I want to run a following query:

Code: Select all

UPDATE db SET address = #new_address# WHERE TerminalID = #TerminalID#;
How do I attach my button to a procedure which will run up mentioned query?

Re: Trying to run a procedure from a button

Posted: Mon Mar 20, 2023 12:49 pm
by kev1n
Hi,

Create a Procedure in which you run the query with the nuRunQuery() function.
Add a run button to your form and choose the procedure to run.

I've created a small demo:

https://streamable.com/s8nexa

Re: Trying to run a procedure from a button

Posted: Mon Mar 20, 2023 1:00 pm
by tsignadze
Kev1n, you are the man!

I am too stupid, I have created mysql procedures and was trying to pass variables from php procedures. This was dead easy :D

Now please help me to add a notification that query was successful and close this popup form.

Once I learn how to get my head around, I will not ask this questions I promise :thumb:

Re: Trying to run a procedure from a button

Posted: Mon Mar 20, 2023 1:08 pm
by kev1n
Add this PHP in your procedure. This will display the message for 2000 ms (2 seconds) and then closes the popup:

Code: Select all

$js = " 

nuMessage('Update was successful!', 2000, closePopup); 

function closePopup() {
    nuClosePopup();
}

";
nuJavaScriptCallback($js);
PS: Please feel free to ask as many questions as you like. I am here to help you and provide information to the best of my ability.

Re: Trying to run a procedure from a button

Posted: Mon Mar 20, 2023 1:14 pm
by tsignadze
Thanks for a quick reply.
Message was displayed but popup did not close, do I need to throw forms id somewhere?

Also, forgot to mention, I want to check if query was successful. I have found this, but can not implement:

Code: Select all

if ($DB->Execute($sql) === false) {

print $DB->ErrorMsg();

}

// If query executed

if ($DB->affected_rows() !== 0) { //true }

Re: Trying to run a procedure from a button

Posted: Mon Mar 20, 2023 1:25 pm
by kev1n
My mistake, it is:

Code: Select all

nuClosePopup();

Re: Trying to run a procedure from a button

Posted: Mon Mar 20, 2023 1:33 pm
by tsignadze
Got it working, please help me implement two new validation points and I will clone this for other buttons for now.

1) I need to check if entered terminal id is existing in my database, if not, display "Check terminal id".
2) Do not allow empty field for new_address, if it is left blank, display "New Address can not be left blank"
3) If query was not successful for whatever reason, display error.
4) (Done) if query was successful display "Update was successful";

Once I will be able to implement this I can create 70% of my access db in nubuilder :mrgreen:

Re: Trying to run a procedure from a button

Posted: Mon Mar 20, 2023 1:35 pm
by kev1n
To get the affected rows with PHP PDO (this is what nuBuilder uses), call db_num_rows() to return the affected rows:

Code: Select all

$stmt = nuRunQuery("UPDATE db SET address = #new_address# WHERE TerminalID = #TerminalID#;");

if (db_num_rows($stmt) == 1) { // 1 row updated

	$js = " 
	nuMessage('Update was successful!', 2000, closePopup); 

	function closePopup() {
		nuClosePopup();
	}
	";

} else {
	$js = " nuMessage('No rows updated'); ";
}

nuJavaScriptCallback($js);

Re: Trying to run a procedure from a button

Posted: Mon Mar 20, 2023 1:41 pm
by kev1n
Updated code:

Code: Select all

if ('#new_address#' == '') {
    nuDisplayError('New Address can not be left blank');
    return;
}

$result = nuRunQuery("UPDATE db SET address = #new_address# WHERE TerminalID = #TerminalID#;");

if (db_num_rows($result) == 1) { // 1 row updated
    $js = " 
	nuMessage('Update was successful!', 2000, closePopup); 

	function closePopup() {
		nuClosePopup();
	}
	";

}
elseif ($result == - 1) {
    nuDisplayError('Update error');
    return;
}
else {
    $js = " nuMessage('No rows updated'); ";
}

nuJavaScriptCallback($js);


Re: Trying to run a procedure from a button

Posted: Mon Mar 20, 2023 2:35 pm
by tsignadze
You made what seemed to be a very demanding task very easy for me!!

Thank you!