$conn and custom pages

Rox

New member
Hi

Simple version: is possible to use the $conn variable in a custom php page? How to use it? A code like the one below could work?

<?php

if(!defined('custom_page_from_inclusion')) { die(); }

global $conn;
$sql = "INSERT INTO Interessati (Interessati) VALUES ('Clienti')";
if(mysqli_query($conn, $sql)){
echo "Records inserted successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($conn);
}

?>

Long version: I have a form for table A with a multiple select field that lockup to a related table B (Many to Many relationship).
In some circumstances, I need to let users to add new values in the related table B while they are in the form of table A, but I understand that this is not possible. So I create a new field in form A that ask "Do you need to insert a new value?". If the answer is "Yes", a JS custom function open a custom PHP page with a simple form to let user to insert just the new value and return to the original form.

Now I'm trying to insert the new value with a SQL query inside the custom PHP page (recalled by the custom JS function) but it seems that the database connection doesn't work...

Thanks in advance




My system info are:
You are using DaDaBIK version 9.0-Monterosso enterprise, installed on 16-08-2018 (installation code: 0), the latest version of DaDaBIK is 9.0-Monterosso released on 05-07-2018

You are runnning the last release of DaDaBIK

PHP Version: 7.2.7

mysql version: 5.7.21

Web server: Apache/2.2.34 (Unix) mod_wsgi/3.5 Python/2.7.13 PHP/7.2.7 mod_ssl/2.2.34 OpenSSL/1.0.2o DAV/2 mod_fastcgi/mod_fastcgi-SNAP-0910052141 mod_perl/2.0.9 Perl/v5.24.0

Client: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.1.2 Safari/605.1.15
 

eugenio

Administrator
Staff member
Hello Rox,
you are right you cannot add "on the fly" records to a select_multiple field type. You could, however, use a master/details view instead and in that case you could add items but it's a quite different way to treat the case.

About $conn, yes you can use it but it's a PDO connection, that's why your mysqi_query command doesn't work, you should use PDO. You can see an example of query execution in custom_functions.php, function dadabik_send_notice_after_accounts_insert.

Best,
 

Rox

New member
Thanks for the hint.
I'm still trying 'cause I cannot execute the query. In the example code I define the variables $interessati and $descrizione in the php code itself... But it still not working. Any help?

<?php

if(!defined('custom_page_from_inclusion')) { die(); }
function dadabik_newinsert()
{
global $conn;
$interessati = 'clienti1';
$descrizione = 'clienti area nord';
$sql = "INSERT INTO Interessati (Interessati, Descrizione) VALUES (':interessati', ':descrizione')";
$res_prepare = prepare_db($conn, $sql);
$res_bind = bind_param_db($res_prepare, ':interessati', $interessati);
$res_bind = bind_param_db($res_prepare, ':descrizione', $descrizione);
$res = execute_prepare_db($res_prepare);
}

?>

The other option, master/view details is still hard form me to implement... I think the problem is with the many to many relationship.. But I'm still studying :D
 

eugenio

Administrator
Staff member
Hello,
execute_prepare_db should be: execute_prepared_db and
VALUES (':interessati', ':descrizione')";
should be:
VALUES :)interessati, :descrizione)";

for simple queries, always take as example dadabik_send_notice_after_accounts_insert and modify starting from that to avoid mistakes.

Best,
 

Rox

New member
Thanks Eugenio,
now the code is:
<?php

if(!defined('custom_page_from_inclusion')) { die(); }
function dadabik_newinsert()
{
global $conn;
$interessati = 'Clienti1';
$descrizione = 'Clienti area nord';
$sql = "INSERT INTO Interessati (Interessati, Descrizione) VALUES :)interessati, :descrizione)";
$res_prepare = prepare_db($conn, $sql);
$res_bind = bind_param_db($res_prepare, ':interessati', $interessati);
$res_bind = bind_param_db($res_prepare, ':descrizione', $descrizione);
$res = execute_prepared_db($res_prepare);
}
?>

I've read it many times but I cannot see the error.. Anyway, it doesn't work. Thanks anyway for your help
 

eugenio

Administrator
Staff member
Hello,
did you get any error? Set $debug_mode to 1 in config.php and paste here the output.

Best,
 

Rox

New member
I've enabled the debug mode, but there is no output. When I call the php page with the code, nothing happens: no error and no new records..
If you prefer, I can use the support direct mail the solve my problem. Thanks
 

eugenio

Administrator
Staff member
Hello,
are you sure it's not a simple syntax error and you have set PHP not to show errors? I would check that, first.

Yes, if you prefer you can contact the support email.


Best,
 

Rox

New member
I reply only for share my error, so that the other users may solve similar problems.
As you suggest, Eugenio, it was an error in the PHP code (but the PHP log and MYSQL log doesn't give any error feedback).

The error was that the code was in a function. The following code now works ...

<?php
if(!defined('custom_page_from_inclusion')) { die(); }
global $conn;
$interessati = 'Clienti1';
$descrizione = 'Clienti area nord';
$sql = "INSERT INTO Interessati (Interessati, Descrizione) VALUES :)interessati, :descrizione)";
$res_prepare = prepare_db($conn, $sql);
$res_bind = bind_param_db($res_prepare, ':interessati', $interessati);
$res_bind = bind_param_db($res_prepare, ':descrizione', $descrizione);
$res = execute_prepared_db($res_prepare);
?>

Bye
 

eugenio

Administrator
Staff member
Oh, I see! You didn't get any error because the function was OK but it was never executed ... you had to call the function:

dadabik_newinsert();

or, alternatively, simply write the code without including it in a function (as you are doing now).

best,
 
Top