user3464091 user3464091 - 8 months ago 78
MySQL Question

How to create a ticket reference number in PHP and mySQL

I'm working on a ticketing system in PHP and mySQL.
I have a table

as my primary key and AI.

I want to add
, so that when a ticket is submitted each ticket is assigned a unique number. I can't use mySQL to AI a second field, right?

If I give the first ticket a number, then write a query to lookup the last
in the DB, then I was thinking of doing something like:

$ticket = 1;
$next = $ticket+1;
echo "ticket number: #".$next;

would this work or is there a better way?


I decided to adapt @Himanshu kumar's answer as follows (as it resolves my orgional question) to use the user's id and the timestamp (both of which I'm using already) as the ticket number:

$user_id = 7; //example from session variable
$cur_date = date('dmyHis'); //timestamp ticket submitted
$ticket = '#'.$user_id.'-'. $cur_date;

This will create create a unique variable that has the user's id and the date and time. I've used seconds to make it more unique. I will then use a query to find all messages/tickets with this ticket.