Click here to Skip to main content
15,397,084 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey guys,

I'm trying to add plus 1 to the value of $trackingcode everytime I insert it to the database. So far it's only giving me TS1 every time I submit the form. Apologies for the question, I'm new to coding and still learning.

So basically every time I submit the form, ideally it should give me:

TS1, TS2, TS3, TS4, TS5

and so on...

What I have tried:

<pre lang="PHP">$trackingcode = 'TS'. 1;

$query = "INSERT INTO `requests`(`trackingcode`) VALUES ('$trackingcode')";
$query_run = mysqli_query($connection, $query);
Updated 23-Jun-21 6:03am
Richard MacCutchan 23-Jun-21 11:27am
You only run the query once. And unless you save the number somewhere it will get lost next time you submit the form. You would be better using an autoincrement field in the database.
renzoandredg 23-Jun-21 11:33am
Is it better if I just drop 'trackingcode' and just use the PK 'id'?
Richard MacCutchan 23-Jun-21 11:36am
I cannot answer that, since it is not clear what you are trying to do. Inserting a sequence of numbers into a database table does not seem like something that would be particularly useful.

1 solution

Basically, don't.
It's a bad idea to do this kind of thing at all, but to do it in a multiuser database system (And MySQL and SQL Server as both multiuser) is just asking for trouble because as soon as you get two or more users doing this, you get duplications, and they can cause some horrible problems for a human to sort out.

Instead, use an IDENTITY column[^] and let the database system sort out sequential and unique numbers for you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900