0Day Forums
PrestaShop, get ID when insert in DB - Printable Version

+- 0Day Forums (https://zeroday.vip)
+-- Forum: Coding (https://zeroday.vip/Forum-Coding)
+--- Forum: CMS (https://zeroday.vip/Forum-CMS)
+---- Forum: PrestaShop (https://zeroday.vip/Forum-PrestaShop)
+---- Thread: PrestaShop, get ID when insert in DB (/Thread-PrestaShop-get-ID-when-insert-in-DB)



PrestaShop, get ID when insert in DB - Mrwicker630 - 07-26-2023

I am creating a script for PrestaShop 1.6 that inserts data into a table.
My table is made in this way:

- id: int AUTO INCREMENT
- desc: string

As I enter a description I would get back the ID value.

On can not use the standard because it blocked by PrestaShop.

I find this a situation:

$sql = "INSERT INTO `"._DB_PREFIX_."table`(`desc`) VALUES ('".$this->desc."')";
$restpo = Db::getInstance()->execute($sql);
var_dump($restpo);

But I have an answer only a boolean.
Can you suggest something?


RE: PrestaShop, get ID when insert in DB - jollify270 - 07-27-2023

Use `$id = Db::getInstance()->Insert_ID();` after executing your SQL.


RE: PrestaShop, get ID when insert in DB - Drnichrome3 - 07-27-2023

You can use the:

$id = (int)Db::getInstance()->Insert_ID();
For example, in the Cart class:

$last_id = (int)Db::getInstance()->Insert_ID();

I also recommend the use of the function insert, for example in the Carrier class:

$values = array();
foreach ($shops as $id_shop) {
$values[] = array(
'id_carrier' => (int)$this->id,
'id_tax_rules_group' => (int)$id_tax_rules_group,
'id_shop' => (int)$id_shop,
);
}
$res = Db::getInstance()->insert('carrier_tax_rules_group_shop', $values);
Then use the Insert_ID to get the last one.


RE: PrestaShop, get ID when insert in DB - closeness99861 - 07-27-2023

PrestaShop's `DB` class provides the [last inserted id][1] via

Db::getInstance()->Insert_ID();

method.


[1]:

[To see links please register here]