Skip to main content
Topic solved
This topic has been marked as solved and requires no further attention.
Topic: multi insert update (Read 2697 times) previous topic - next topic

multi insert update

Hello All,
First I have made a copy of add page for table 'docs',
then by editor  I make my custom Form, that is working and all parameters are passed true POST method,
// action="<?php print_link("docs/add?csrf_token=$csrf_token") ?>"
but it insert only in one table 'docs',
so I need to INSERT some VALUES in two more tables 'partner' , 'sales' and to UPDATE "qty" in  'product'
what/where is the right way to do it?
then I need to redirect to custom view page LAST record?

So I'm confused from that in docsControler.php ,
function add(){
if($formdata){
         $db = $this->GetModel();
         $tablename = $this->tablename;
....
I could make my own functions and do it my way, but I would like to understand the logic here and do it right way!
So advice how to make that multi-insert ?

Re: multi insert update

Reply #1
@dobsun‍ this video https://www.youtube.com/watch?v=UsLlG4EvkbQ describes the functions you are asking of and also look at this thread https://phprad.com/forum/index.php?topic=824.msg2705#msg2705, which shows a better way to do such update.

Note: The best way for you to achieve what you want is to use page events. You can search the forum for more info on what you are trying to do.

Re: multi insert update

Reply #2
@willvin
thank you, page event it works for me!

All works except SELECT array ?
I have try many $params looking as
$params = implode(',',array_keys($products_in_cart);
print_r ($params)  - return  list of product id separated by comma as : 1024,1025,1026

$params = (array_keys($products_in_cart);
print_r ($params) - return : Array ( [0] => 1024 [1] => 1025 [2] => 1026 )

$arr = $db->rawQuery("SELECT * FROM product WHERE id = ?", $params);

all that returns: error 500

Re: multi insert update

Reply #3
solved
<?php
for($i=0; $i<count($product_in_cart); $i++){
$pid = $product_in_cart[$i];
$params = array($pid);
$arr = $db->rawQuery("SELECT * FROM product WHERE id = ?", $params);
$product = $arr['0'];
echo $product['name'];
}
?>
I'm not sure it is right way, as we call server db multiple times in a loop , if you have better suggestions let me know

Re: multi insert update

Reply #4
@dobsun‍ you use this ? in your query when you know or can predict the num of array param you are passing to the query. So for each value in the array, you need to add the same number of this ? which is wrong in this situation.
Use this instead:
Code: [Select]
$params = array($products_in_cart);
$arr = $db->rawQuery("SELECT * FROM product WHERE id IN (?)", $params );


Re: multi insert update

Reply #5
Thank you very much!
I have try that option many times, obviously with wrong syntax!