assistance required with the code <mysql_insert_id()>

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
raazaq
Forum Newbie
Posts: 1
Joined: Wed Mar 19, 2008 1:37 pm

assistance required with the code <mysql_insert_id()>

Post by raazaq »

Hi,

I wonder if anyone can help me plz... I am new to this forum and am trying to figure out what's wrong with the code. I have got two tables customer and regcustomer and trying to insert data into them. Customer id is the (Pk) of customer table. I am assuming 'mysql_insert_id()' is not working and when I run this script, following error appears on the screen 'Duplicate entry '0' for key 1'.

Code is as below:

Code: Select all

 
$sql1 = "INSERT INTO customer  (customerid, name, telephone, email, address, zip) VALUES ('','$name', '$telephone', '$email', '$address', '$zipcode')";
mysql_query( $sql1 )or die(mysql_error());
 
$sql2 = "INSERT INTO regcustomer (username, password) VALUES ('$username', '$password', mysql_insert_id())";
mysql_query( $sql2 ) or die(mysql_error());
 
Any suggestions...please?
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Re: assistance required with the code <mysql_insert_id()>

Post by impulse() »

It means you have a primary key set in one of your columns and you're trying to insert a value into that column that already exists.
Post Reply