Answer the question
In order to leave comments, you need to log in
How to get around the problem of creating the same codes?
I am finalizing the store on OpenCart. When a client submits a form, a promotional code like AAA-0001 should be created in the database, etc. in order. Tell me, is it better to store the last code in the options, or is it better to query the table with codes and find out the number of the last code? And most importantly, how to make sure that the same codes cannot appear? Is there such a possibility? Or not?
Answer the question
In order to leave comments, you need to log in
Form this code based on the primary key of the table. That is, the user submitted the form, you made an entry in the database, and received its ID. And you generate a promotional code based on this ID. The ID within the table is unique, so your procode will be unique. You can form this promotional code not in PHP, but in the database, hang the trigger on the insert event. But I still advise you to do it in the code, because this way you can cover it with tests.
promotional code type AAA-0001
CREATE TABLE promocode(
id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT,
user_id INT NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (user_id) REFERENCES user (id)
);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question