简体   繁体   中英

Increasing client number automatically.

So, I'm trying to create a database where the user is making client pages. I figured out how to upload there first name,last name,telephone,fax,e-mail,client number to the database.My question is how do I automatically increase the client number, when ever the end-user is creating a new page.Ex first time PCO-14-1, next time without them typing in PCO-14-2 it should do it automattically. Any suggestion where do I start from.

By the way the languages i'm using are html,php,javascript and phpmyadmin.

First is that you need to have another column only for ID's(int) in order to get the max data or last ID inserted. You can do it by this way:

 // you connect in your database here

$query = mysqli_query("SELECT MAX(ID) as id FROM tbl_newPage") or die(mysqli_error());
$result = mysqli_fetch_assoc($query);
$ID = $result['id'] + 1;

$newID = "PCO-14-" .$ID; // the new id that is already incremented 

Hopes this helps bro.

You need to define an autoincrement field for field ID.

ALTER TABLE `table` CHANGE COLUMN `id` `id` INT(11) NOT NULL AUTO_INCREMENT ;

then after insert you do

$query = "INSERT ... SET firstname='john', (etc) ";
mysql_query($query) or die($query.':'.mysql_error());
$last_inserted_id = mysql_insert_id();
//append any needed strings and use it

You can also use MAX(ID) + 1 but there will be problems if you have relations based on columnd ID in other tables. for example when you delete the highest id, then all its relations will point to the wrong id.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM