• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Primary key question with PHPmysql database

Explorer ,
Apr 23, 2010 Apr 23, 2010

Copy link to clipboard

Copied

I know that updating the primary key is taboo, but I have a a newsletter subscription form set up that works perfectly, but I can't figure out how to create the primary key.  I tried using the subscriber email address (because its unique, but in trying to add an update function I realize that the email address may need to be updated.

My question is how do I get a primary key that will work when I'm relying on a form variable to create the record and therefore have no control over assigning one?

Is there a way to have one automatically assigned when a subscriber hits the submit button?

TOPICS
Server side applications

Views

455

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Apr 23, 2010 Apr 23, 2010

That's why it's always a good idea to use surrogate keys. You can simply have the database generate a unique id. In MySQL, I think this is a column with the auto_increment attribute. If you need to then obtain that id after insert, php has functions for this (mysql_insert_id() I believe).

Votes

Translate

Translate
LEGEND ,
Apr 23, 2010 Apr 23, 2010

Copy link to clipboard

Copied

That's why it's always a good idea to use surrogate keys. You can simply have the database generate a unique id. In MySQL, I think this is a column with the auto_increment attribute. If you need to then obtain that id after insert, php has functions for this (mysql_insert_id() I believe).

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 23, 2010 Apr 23, 2010

Copy link to clipboard

Copied

Bingo.

I wrote:

ALTER TABLE email_list  ADD (      id MEDIUMINT NOT NULL AUTO_INCREMENT,      name CHAR(30) NOT NULL,      PRIMARY KEY (id)  )

I got my primary key column.  thank you so much!!!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 23, 2010 Apr 23, 2010

Copy link to clipboard

Copied

You be the regent.

Got it handled with the following sql:

ALTER TABLE email_list ADD ( id MEDIUMINT NOT NULL AUTO_INCREMENT,

name CHAR(30) NOT NULL, PRIMARY KEY (id) );

thanks for the guidance.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 23, 2010 Apr 23, 2010

Copy link to clipboard

Copied

LATEST

You're welcome.


Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines