473,569 Members | 2,747 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

copy auto_increment value to another field

Hi,

i want to create a unique serial number to my Db entries. I thought
the best way would be to add the auto_increment primary key value to a
string, then insert it into a table field 'serial_num'.

$serial_num = "NAME000" . $auto_increment _value;

Is this possible in one function? Or do I have to insert all the other
data first, then get the last inserted id value and then update the
entry with the $serial_num variable?

Cheers
Jul 17 '05 #1
5 3081
Nel
"Paul Lamonby" <pa**@popimages .com> wrote in message
news:86******** *************** ***@posting.goo gle.com...
Hi,

i want to create a unique serial number to my Db entries. I thought
the best way would be to add the auto_increment primary key value to a
string, then insert it into a table field 'serial_num'.

$serial_num = "NAME000" . $auto_increment _value;

Is this possible in one function? Or do I have to insert all the other
data first, then get the last inserted id value and then update the
entry with the $serial_num variable?

Cheers


Hi Paul,

If you set one field type, lets call it "id", to an INT and set
auto_increment, primary, unique, this would automatically create a unique
number within the "id" field for all entries. You can the prefix the id
number with "NAME"+$id within php to achieve the above.

It really depends on what you want the serial number for, human readable
stuff or just passing on id within your site(s). You could use an md5()
hash of your number + some other variable to provide you with a more secure
id.

Nel.
Jul 17 '05 #2
"Nel" <ne***@ne14.co. NOSPAMuk> wrote in message news:<jK******* ************@wa rds.force9.net> ...
"Paul Lamonby" <pa**@popimages .com> wrote in message
news:86******** *************** ***@posting.goo gle.com...
Hi,

i want to create a unique serial number to my Db entries. I thought
the best way would be to add the auto_increment primary key value to a
string, then insert it into a table field 'serial_num'.

$serial_num = "NAME000" . $auto_increment _value;

Is this possible in one function? Or do I have to insert all the other
data first, then get the last inserted id value and then update the
entry with the $serial_num variable?

Cheers


Hi Paul,

If you set one field type, lets call it "id", to an INT and set
auto_increment, primary, unique, this would automatically create a unique
number within the "id" field for all entries. You can the prefix the id
number with "NAME"+$id within php to achieve the above.

It really depends on what you want the serial number for, human readable
stuff or just passing on id within your site(s). You could use an md5()
hash of your number + some other variable to provide you with a more secure
id.

Nel.


Nel,

Cheers for the reply, I have got it sorted, but my post was more
concerned with the 'correct way' to do this.

I have my auto_increment, primary, unique 'id' field and my 'serial'
field.
What I am doing is INSERTing data into the Db, then retrieving the
mysql_insert_id (), then UPDATEing the same entry with the $serial
prefixed variable, $serial = "NAME000" . $id;

Is the the correct way to do it? Or can I retrieve the id in the same
INSERT statement? Or can mysql add a prefix to a auto_increment,
primary, unique field?

Paul
Jul 17 '05 #3
In article <86************ **************@ posting.google. com>, Paul Lamonby wrote:
"Nel" <ne***@ne14.co. NOSPAMuk> wrote in message news:<jK******* ************@wa rds.force9.net> ...
What I am doing is INSERTing data into the Db, then retrieving the
mysql_insert_id (), then UPDATEing the same entry with the $serial
prefixed variable, $serial = "NAME000" . $id;


I presume it will work. But i don't see why you want to add a prefix to
the key? To me, it seems that that prefix is quite redundant.

I haven't had the time to check it out, but i think you might notice a
difference in speed (i know there is no theoretical base for this, but
it might to try it out in the real world) if you use an INT instead of
a CHAR for your primary key.

--
Tim Van Wassenhove <http://home.mysth.be/~timvw/contact.php>
Jul 17 '05 #4
Tim Van Wassenhove <eu**@pi.be> wrote in message news:<2h******* *****@uni-berlin.de>...
In article <86************ **************@ posting.google. com>, Paul Lamonby wrote:
"Nel" <ne***@ne14.co. NOSPAMuk> wrote in message news:<jK******* ************@wa rds.force9.net> ...
What I am doing is INSERTing data into the Db, then retrieving the
mysql_insert_id (), then UPDATEing the same entry with the $serial
prefixed variable, $serial = "NAME000" . $id;


I presume it will work. But i don't see why you want to add a prefix to
the key? To me, it seems that that prefix is quite redundant.

I haven't had the time to check it out, but i think you might notice a
difference in speed (i know there is no theoretical base for this, but
it might to try it out in the real world) if you use an INT instead of
a CHAR for your primary key.


Tim,

I know it seems strange, but I need this unique 'serial number' for
people to enter in via SMS to download an image from the server. I
could easily use the auto_increment primary key value, but people just
entering '1, 2, 3.. 34' as a serial number doesnt really have the same
kudos as a prefixed string, like IMAGE0023.

My post was wondering whether it is possible to set up a field in
mysql that auto_increments onto a string prefix, instead of having to
retrieve the id value and add it into another field, but it seems this
is not possible, so i have opted to retrieve the value and add it
manually.

Cheers
Jul 17 '05 #5
I noticed that Message-ID:
<86************ **************@ posting.google. com> from Paul Lamonby
contained the following:
My post was wondering whether it is possible to set up a field in
mysql that auto_increments onto a string prefix, instead of having to
retrieve the id value and add it into another field, but it seems this
is not possible, so i have opted to retrieve the value and add it
manually.


Wouldn't it be just as easy to get the system to ignore any prefix?
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1955
by: Pjotr Wedersteers | last post by:
I have a table with quiz questions. Each question has a unique ID, based on an auto_increment field. Now I've discovered that with deleting rows from the table the deleted ID's are not reissued. Makes sense, just thought of that a bit late. How can I simply re-index or update or how one would call it, the columns holding the ID numbers ? Is...
4
4662
by: jjliu | last post by:
i have created the following table: create table test (field1 varchar(100), id integer unsigned not null auto_increment, unique (field1), primary key (id)); When i try to insert some values by using the following statement insert into test values ("this is a test"); I was told "column count does not match value count at row 1".
9
3316
by: Bart Van der Donck | last post by:
Hello, The first column of my table is AUTO_INCREMENT. I fill my table with 5 records with a blanco value in their first field. The first column of my table will then hold the values 1,2,3,4,5 (top>down). Then I erase those 5. Then I add a new record with a blanco first value. It will get number 6.
0
1764
by: Shailesh | last post by:
I made a MyISAM table on mysql 4.0.18 NT with auto_increment column started at 2147483646. The third row I insert fails as expected because the integer range is maxed out. However, if I delete the topmost row, and then re-insert a row, the topmost value is reused. This contradicts the documentation. ...
2
492
by: hjyn | last post by:
Hi, how can I add char to auto_increment? For example, the auto_increment is for integer, I want to add a character at the front of that number. Ex: >create table abs (UserID INT(3) NOT NULL AUTO_INCREMENT, name varchar(15), primary key(UserID)); >insert into abs(name) VALUES ('Elton'), ('Andy'), ('Peter'); >select * from abs;
1
1723
by: Phil Latio | last post by:
Newbie questions here... Reading the MySQL manual regarding InnoDB databases, can someone confirm that: 1. if the server is rebooted, AUTO_INCREMENT reverts back to 1? 2. what happens if the field is specified as or PRIMARY KEY or UNIQUE? Cheers Phil
2
3453
by: Swinky | last post by:
I hope someone can help...I feel like I'm walking in the dark without a flashlight (I'm NOT a programmer but have been called to task to do some work in Access that is above my head). I have code that will successfully copy a record and append the information to a new record in the same table (parent table) within a form. However, there...
5
3235
by: akapsycho | last post by:
I've got an index field which stores id numbers which are created by auto_incrementing and assigning that value as the id. ive already emptied out the values a few times since I'm testing, but I can't seem to figure out how to reset a field set to auto_increment, so that the next time someone registers an account, their ID will be set to 1 and it...
3
9230
by: Richnep | last post by:
Hi all, I have tabbed subforms where I need to copy one field value from one subform over to another subform. Although I can run an update query to accomplish this I would like to do it through VBA. sub tables relationships are 1:N with the main table. So the recods display like this in the subform:
0
7694
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7921
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8118
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6278
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5504
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1208
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.