Jump to content

Insert AutoNumber


doubledee

Recommended Posts

How do I INSERT a record - using PHP - that has an AutoNumber as the first field?

 

I tried this but got an id=0...

// Build Query.
$q4 = "INSERT INTO prior_email(member_id, email, created_on)
					VALUES (?, ?, NOW())";

// Prepare statement.
$stmt4 = mysqli_prepare($dbc, $q4);

 

Thanks,

 

 

Debbie

 

Link to comment
Share on other sites

you left out mysqil_bind_param

 

 

mysqli_bind_param($q4,'isi'); 
//bind as interger string integer or coulf try using double or float instead of interger

 

No, I just didn't post it here since I didn't think that was where the problem is.

 

Here is more of my Prepared Statement...

// ******************
// Log Old Email.		*
// ******************

// Build Query.
$q4 = "INSERT INTO prior_email(member_id, email, created_on)
							VALUES (?, ?, NOW())";

// Prepare statement.
$stmt4 = mysqli_prepare($dbc, $q4);

// Bind variables to query.
mysqli_stmt_bind_param($stmt4, 'is', $memberID, $newEmail);

// Execute query.
mysqli_stmt_execute($stmt4);

// Verify Insert.
if (mysqli_stmt_affected_rows($stmt4)==1){
	// Insert Succeeded.

 

 

Debbie

 

Link to comment
Share on other sites

You won't need to define the unique id if you set the field in mysql to auto increment. So basically you'd remove member_id from your query and change member_id in mysql to auto increment.

 

ALTER TABLE `members` CHANGE `member_id` `member_id` INT( 11 ) UNSIGNED NOT NULL AUTO_INCREMENT

Link to comment
Share on other sites

You won't need to define the unique id if you set the field in mysql to auto increment. So basically you'd remove member_id from your query and change member_id in mysql to auto increment.

 

ALTER TABLE `members` CHANGE `member_id` `member_id` INT( 11 ) UNSIGNED NOT NULL AUTO_INCREMENT

 

id is my primary key

 

member_id is my foreign key back to the member table, so I do need that.

 

I thought I was doing things correctly by leaving out the id above...

 

 

Debbie

 

Link to comment
Share on other sites

When I ran my Change Email script the first time I got this in phpMyAdmin...

id 	member_id 	email 	ip 	host_name 	created_on 	updated_on
0 	21 	pam@mail.com 	NULL 	NULL 	2012-03-16 20:12:33 	NULL

 

But when I ran it again, my script errored out to a default, catch-all error, leading em to believe the SQL failed.

 

And since I see no new records, that must be it?!

 

So I assume the issue is with my id autoincrement field and my Prepared Statement syntax...

 

 

Debbie

 

Link to comment
Share on other sites

I think there is something wrong in MySQL, because when I ran a manual query in phpMyAdmin I got this...

Error

SQL query:

INSERT INTO prior_email( member_id, email, created_on )
VALUES ( 21, 'pam2@mail.com', NOW( ) )

MySQL said: Documentation
#1062 - Duplicate entry '0' for key 1 

 

 

So the database isn't auto-incrementing the "id" field?!

 

 

Debbie

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.