Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP Databases
| Search | Today's Posts | Mark Forums Read
PHP Databases Using PHP in conjunction with databases. PHP questions not specific to databases should be directed to one of the other PHP forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old October 12th, 2006, 01:01 PM
Authorized User
 
Join Date: Aug 2005
Location: , , Finland.
Posts: 68
Thanks: 0
Thanked 0 Times in 0 Posts
Default Insert Data into two tables

I have two tables one for usernames and second for userdetails,

Users
_________________________
|ID (PK)| Usrname | PSW |
---------------------
|1 | User | P@sw|
-------------------------
Details
__________________________
|ID (FK)| firsn | Last_n |
--------------------------
| 1 | Test | Users |
--------------------------
Tables are related.

How can I with php insert data to both tables. So when new user registers and gives his username, password, first name and last name the data would insert into right tables.
  #2 (permalink)  
Old October 19th, 2006, 12:01 AM
Friend of Wrox
 
Join Date: Oct 2003
Location: colombo, WP, Sri Lanka.
Posts: 479
Thanks: 0
Thanked 3 Times in 3 Posts
Send a message via MSN to surendran Send a message via Yahoo to surendran
Default

hello,
write both query,
$sql="insert into Users(ID,Usrname,PSW) values(1,'User','P@sw')";
$sql1="insert into Details(ID,firsn,Last_n) values(1,'Test','Users')";


surendran
(Anything is Possible)
http://ssuren.spaces.msn.com
  #3 (permalink)  
Old November 30th, 2006, 07:40 AM
Authorized User
 
Join Date: Nov 2006
Location: Bhopal, MP, India.
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to talk2leo
Default

Hi You can also do it like the query below..this will
remove the problem of id if it is AUTO_INCREMENT
$id = mysql_insert_id();
$sql="insert into Users(ID,Usrname,PSW) values('$id','User','P@sw')";
$sql1="insert into Details(ID,firsn,Last_n) values('$id','Test','Users')";

by doing so it will remobve the overhead of how to keep same id in both the tables..because if user is submitting information then you will not assign him/her id each time.

  #4 (permalink)  
Old November 30th, 2006, 07:42 AM
Authorized User
 
Join Date: Nov 2006
Location: Bhopal, MP, India.
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to talk2leo
Default

you can also remove quotation marks in the query as iid will be numeric
like this

$id = mysql_insert_id();
$sql="insert into Users(ID,Usrname,PSW) values($id,'User','P@sw')";
$sql1="insert into Details(ID,firsn,Last_n) values($id,'Test','Users')";

  #5 (permalink)  
Old July 7th, 2007, 10:36 PM
Registered User
 
Join Date: Jul 2007
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi, its posible to use:

$id = mysql_insert_id();
$sql="insert into Users(ID,Usrname,PSW) values($id,'User','P@sw')";
$sql1="insert into Details(ID,firsn,Last_n) values($id,'Test','Users')";

in one single

$sql

Thanks...

RRS


Similar Threads
Thread Thread Starter Forum Replies Last Post
Command to insert data in multiple tables? dstein4d Access VBA 3 November 18th, 2008 09:16 AM
how Insert record into two tables mfarooqw ASP.NET 1.0 and 1.1 Basics 2 July 10th, 2007 05:38 PM
insert data in two tables from form mfarooqw ASP.NET 1.0 and 1.1 Professional 1 July 10th, 2007 08:34 AM
Insert into Multiple Tables bmalex1 Beginning PHP 0 February 6th, 2006 12:45 PM
Insert into different Tables cs9896 SQL Language 2 September 16th, 2003 03:52 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.