Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 584 Vote(s) - 3.46 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PrestaShop, get ID when insert in DB

#1
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?
Reply

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

#3
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.
Reply

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

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

method.


[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through