Page 1 of 1
Insert records into another table
Posted: 2017-05-06 15:56
by salemsaleem76
Hello Everyone,
Can you please help me with a simple code how I can insert a record into another table from the hook file of any table. here is the scenario:
- In table A I've a status field, so I run a case function, I need to place a code in one of these status to create a new record in table B when user change to that status.
I am not sure, do I need to create a database connection or its not needed? If its needed, where do I need to add the call for the connection assuming I may call insert into different tables based on the status as mentioned above.
I don't all fields to be filled, I just need to have a record created there (In Table B) when user selected that status.
Thanks in advance,
Salem
Re: Insert records into another table
Posted: 2017-05-06 18:37
by grimblefritz
Sounds like you need to create an ajax request, triggered by some jquery attached to your field in table A, that will insert a record in table B.
I believe there's are examples of ajax read requests in the Invoice app (in the Downloads section). You should be able to modify that to perform an insert instead.
Re: Insert records into another table
Posted: 2017-05-07 11:53
by salemsaleem76
Thanks for you reply, I'll download the application that you have mentioned, can you please let me know where I can find that code? I am sorry but I don't have that experience so I need a little help here.
Regards,
Salem
Re: Insert records into another table
Posted: 2017-05-07 17:30
by grimblefritz
Here is a bit I use in one of my apps. It's a select (lookup), but you should be able to alter it to do inserts.
This is ajax.php (called from the js hook file, further down below)
Code: Select all
<?php
// load AppGini environment
$currDir = dirname(__FILE__).'/..';
include("$currDir/defaultLang.php");
include("$currDir/language.php");
include("$currDir/lib.php");
/* FUNCTIONS
get_material_info
returns: type|description|unit|buy_price|sell_price
*/
function get_material_info($id=0)
{
// grant access to all users with access to material table
$mi_from = get_sql_from('material');
if(!$mi_from){
header('HTTP/1.0 401 Unauthorized');
exit;
}
// get material record
$res = sql("select `type`,`description`,`unit`,`buy_price`,`sell_price` from `material` where `id`='" . makeSafe($id, false) . "' limit 1", $eo);
if($row = db_fetch_assoc($res)){
return implode(array_map('makeSafe', $row),'|');
}else{
return false;
}
}
// process actions based on $ax
$ax = makeSafe($_REQUEST['ax']);
$id = intval(makeSafe($_REQUEST['id']));
switch ($ax) {
case 'mi':
echo get_material_info($id);
break;
default:
header('HTTP/1.0 400 Bad Request');
exit;
}
?>
In the hook file there is this bit
Code: Select all
$j(function(){
// for material changes - fill in type/description/unit
// and buy/sell/suggested prices fields
function get_material_info(){
var id = $j('#material_id').val();
var qty = $j('#quantity').val();
if (id == '{empty_value}'){
$j('#type,#description,#unit,#buy_price,#sell_price,#suggested_value,#amount_paid').val('');
$j('a[href="#vehicle-info"]').parents('li').hide();
}else{
$j.ajax({
url: 'hooks/ajax.php',
data: { ax: 'mi', id: id },
success: function(data){
data=data.split('|');
if (isNaN(data[3]*qty)){
price='';
}else{
price=data[3]*qty;
}
$j('#type').val(data[0]);
$j('#description').val(data[1]);
$j('#unit').val(data[2]);
$j('#buy_price').val(data[3]);
$j('#sell_price').val(data[4]);
}
});
}
}
// update material details on material changes
$j('#material_id-container').on('change', get_material_info);
});
Hope that helps.