Jump to content

Inserting data into two tables in php


raazaq

Recommended Posts

Hi,

 

I am new to this forum and php. I have been trying to insert data into two separate tables but so far unable to do so. With the code I might be able to explain it bit clearer.

 

I have got two tables user and registeruser with the following columns; user (customerid (pk), name, telephone, email, address, zip) and registeruser (username, password, customerid). I am trying to register user by inserting data into tables user and userid. my code is as follows:

 

// Insert data into mysql

 

$sql="INSERT INTO user (userid, name, telephone, email, address, zip)VALUES(NULL, '$name', '$telephone', '$email', '$address', '$zipcode')";

$result=mysql_query($sql);

 

$sql = "INSERT INTO registeruser (username, password, userid)VALUES('$username', '$password', NULL)";

$result = mysql_query( $sql);

 

But this doesnot work. Any suggestions, how to insert data into two separate tables using PHP?

 

Thanks

Link to comment
Share on other sites

Personally I don't use variables when I use mysql_query()

 

Also, you're using the same variables twice

 

Try this:

$sql1 = "INSERT INTO user (userid, name, telephone, email, address, zip) VALUES (NULL, '$name', '$telephone', '$email', '$address', '$zipcode')";
mysql_query( $sql1 );

$sql2 = "INSERT INTO registeruser (username, password, userid) VALUES ('$username', '$password', NULL)";
mysql_query( $sql2 );

Link to comment
Share on other sites

Thanks for all your replies  :).

 

I have tried to use both options posted above (i.e. die(mysql_error()); and mysql_query( $sql ) function) and they seem to improve the scenario. Apart from userid column in table user and registeruser. It is a primary key in user table and foreign in other and it set to 'auto increment'. But its not incrementing. My new code is as below:

 

$sql1="INSERT INTO user (userid, name, telephone, email, address, zip)VALUES(NULL, '$name', '$telephone', '$email', '$address', '$zipcode')";
mysql_query($sql1) or die(mysql_error());

$sql2 = "INSERT INTO registeruser (username, password, userid)VALUES('$username', '$password', NULL)";
mysql_query( $sql2) or die(mysql_error());

Link to comment
Share on other sites

Looks like you'll want to use PHP's mysql_insert_id() function in the second statement.

 

It will use the ID from the first query and use it in the second query.

 

$sql1 = "INSERT INTO user (userid, name, telephone, email, address, zip) VALUES (NULL, '$name', '$telephone', '$email', '$address', '$zipcode')";
mysql_query( $sql1 );

$sql2 = "INSERT INTO registeruser (username, password, userid) VALUES ('$username', '$password', mysql_insert_id())";
mysql_query( $sql2 );

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.