Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4598810
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T23:27:07+00:00 2026-05-21T23:27:07+00:00

I am using mysql/php. my table looks like id (int autoincrement) voucher(varchar 25) I

  • 0

I am using mysql/php.

my table looks like

id (int autoincrement)  
voucher(varchar 25)

I am generating voucher codes where the code is:

  • 1 random number (eg 64)
  • 1 fixed number (eg 352)
  • value of id field (1,2,3,4,5…)
  • 1 checkdigit (luhn algorithm).

The obvious issue in doing this with php is that I have to do a select, get the next autoincrement value, calculate the code, insert, by which time another row could have been inserted.

What I want to do is instead,

do insert into vouchers (voucher) value('64352') and have it generate the rest of me.

How can I do this? function/trigger?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T23:27:08+00:00Added an answer on May 21, 2026 at 11:27 pm

    If you are using InnoDB, an easy fix would just be to use your existing code and wrap it in a MySQL transaction. i.e. in pseudocode:

    mysql_query("START TRANSACTION");
    
    # get next autoincrement value into: $next
    
    # do your INSERT query
    
    # get the actual last inserted ID into: $actual
    
    if ($next === $actual) {
      mysql_query("COMMIT");
    } else {
      mysql_query("ROLLBACK");
      # and raise an Exception or return an error
    }
    

    EDIT/ADD:

    Since you mentioned triggers, I looked into that and believe it’s doable. A couple of issues.

    1. Accessing the “next autoincrement ID” from the trigger … I don’t know a “good” way to do this. In INSERT queries you have access only to NEW and not OLD (existing rows) values (see Trigger syntax). What I’m doing to do in the example is just maintain a separate counter @vcount on the server. To initialize this value to whatever the ID currently is, you would just do “SET @vcount = 42;”

    2. Luhn algorithm. You will have to implement this as a SQL function. Here’s a Luhn validator in SQL you could crib from. Alternately you could hash/checksum with a native MySQL function like MD5 (and use only the first X chars if you need a short voucher code). In any case you need to make a hash function … I’ll just use “Luhn” below.

    Anyway here’s what the trigger would look like:

    delimiter //
    CREATE TRIGGER make_voucher_code BEFORE INSERT ON vouchers
    FOR EACH ROW
    BEGIN
        SET @vcount = @vcount + 1;
        SET NEW.voucher = CONCAT(
            NEW.voucher, 
            CAST(@vcount AS CHAR),
            Luhn(CONCAT(NEW.voucher, CAST(@vcount AS CHAR))));
    END;
    //
    delimeter ;
    

    Then, in your INSERTs, you would put just ‘64352’ in the query, as you’ve suggested. The trigger would append the rest.

    Personally, unless you or someone else can solve the autoincrement/@vcount problem better, I would still prefer doing the MySQL transaction, which will do a better job than this of keeping everything atomic and keeping all your app code in PHP.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a table in MySql using php. My code looks
I'm using MySQL with PHP. This is like my table: (I'm using 3 values,
Using MySQL and PHP. I'm trying to select 4 random ads from a table.
I am using MYSQL and PHP. I have a table called item. Two of
using php and mysql I have two tables, a users table and a profiles
Using PHP and MySQL, I want to select only 6 rows from table which
Using PHP, I can convert MySQL data or static table data to csv, Excel,
I currently am using mysql and php to display 9 random results from my
I am using MySQL and PHP 5.3 and tried this code. $dbhost = 'localhost';
I have a table in my MySQL database that looks like the following banner

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.