473,385 Members | 1,610 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

how to increment a variable inserted in database

123 100+
I want to increment the variable number when ever there is a insertion to the database for the first insertion it should be 001 the next person or insertion should be 002.

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. <?php
  4.  
  5. $user =  ($_POST['person']);
  6. $number =  "001"; //what to increament these
  7.  
  8. mysql_query("INSERT INTO users (person, user_num) VALUES ('$user','$number')") or die(mysql_error()); 
  9. ?>
  10.  
Dec 3 '10 #1
2 5009
very simple :)
Expand|Select|Wrap|Line Numbers
  1. $id = $_POST['person'];
  2. $sql = "Update users set user_num=+1 where person='$id' ";
  3. mysql_query($sql);
Dec 3 '10 #2
Samishii23
246 100+
Or you don't have to at all.
MySQL has a built in auto increment feature.

This snippet was taken from a PHPMyAdmin table creation
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE `test` (
  2.   `id` INT( 3 ) NOT NULL AUTO_INCREMENT ,
  3.   PRIMARY KEY (  `id` ) ,
  4.   UNIQUE (`id`)
  5. )
This, from a table update
Expand|Select|Wrap|Line Numbers
  1. ALTER TABLE  `testing` ADD UNIQUE (`id`)
  2. // Then ...
  3. ALTER TABLE  `testing` CHANGE  `id`  `id` INT( 5 ) NOT NULL AUTO_INCREMENT
Its important the tables column is set to unique, as it is required for the auto feature. Also note, only one auto feature is allowed per table. You can more then one unique index, though you'd have to be careful of that you enter into that column.

Every insert you make into that table with increment the ID value from my example. Just a side note, deleting table entrys doesn't effect the increment value.
Dec 4 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Spam sucks | last post by:
hello, i want to have an image in a variable so i can show it were ever i want the only catch is that i want the image to be called in the same page, i found this script but the problem is that...
2
by: Ramez T. Mina | last post by:
I tried to increment variable when I click on a button, but it didn't work. I use the VB.NET editor and not the HTML editor. any one can help??
3
by: mjteigen | last post by:
I'm very new at Python, but have been trying it in conjunction with CGI. I've encountered a problem that I'm pretty sure is a trivial one, but I don't know enough Python to work it out. Here's an...
3
by: Bon | last post by:
Dear all I have a javascript function which embeds ASP script for inserting data into database. After the data is inserted database, I select the maximum id (the latest inserted record id). But,...
3
by: Frankie | last post by:
Hi, I need to add shirt size info for new T-shirts into a mysql database in this order: S, M, L, XL, creating the following table: itemNumber size inStock 1000 ...
2
by: weird0 | last post by:
What is the best way to count the number of records in a MS SQL DB. The way I do it is executeReader() and loop through all the records in a db and increment variable RowCount++. Is there...
1
by: hemashiki | last post by:
hi all i need a help here,i want to increment an number in database through php code without using auto increment key in database.it must be incremented automatically by inserting the values in...
1
by: sbettadpur | last post by:
hi everybody, l have one doubt, please clarify that if anybody knows. Can i able to take database connection across the page through session variable, without initializing connection on every...
1
by: mrityunjay11 | last post by:
the code is as #!/bin/sh i=1; while do echo "welcome $i times"; i= 'expr $i + 1 '; done
2
by: Meditrina | last post by:
I have code that looks like this (pulling in 3 fields from a sql database), but I want it to only show the fields if <%=rs("UnitPrice3")%> is greater than 0. How do I write the if statement on my...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.