PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Can someone tell me how I can insert records from one form into two different database tables on a submit? These tables are in the same database.
I would like to have one form that a visitor can create an account with a username and password which I would like into one table, and name, address, email, phone, into another database table.
you mentioned your not a "hand coder", so what are you using to create this application? There are a couple of factors here like what database you are using but the most basic answer I can tell you right now is just do mulitple INSERT statments.
<?php
// Create you INSERT Statements for each table from your variables
$sql = "INSERT INTO `access` ( `username` , `password`) VALUES ( 'Test', 'Test') ";
$sql2 = "INSERT INTO `info` ( `firstname` , `lastname`, `phonenumber`) VALUES ( 'John', 'Doe', '555-5555') ";
// Then Insert them
$result = mysql_query($sql) or die;
$result = mysql_query($sql2) or die;
?>
If you did something like this, you would more than likely want to marry the data up somehow. For example, in the sample given I would have grabbed the unique ID from the access table and added to my INSERT statement to the info table so I have a way of telling what username goes with which user.
Answer to your first post, I happily use Dreamweaver for my coding. Unfortunately the bad side is that's what keeps me in the dark as far as learning PHP. And this is the scenario that I wish I did know how to hand code... Oh, I think you asked what kind of database I'm using? It's MySQL
I think I *kind of* understand that example code you wrote, thank you for that. However I do not understand where you wrote VALUES ( 'John' 'Doe' etc....). Is that just an example for what its going to get? I guess I mean obviously this will be blank since those values are unknown?... Sorry, just trying to understand what you wrote and how to implement it. So is that all there is to it?
I use Dreamweaver as well, unfortunately I do most of my coding by hand so I'm not 100% sure if I remember how Dreamweaver does everything but I am 98% sure you can tell Dreamweaver to insert into 2 different tables using the Server Behaviors. Sorry I can't be of more help there but from what I remember, it's pretty intuitive if you already understand how to use the server behaviors. Again, these are only good on simplistic scenarios so I don't really use them at all any more.
Depending on what it is you are actually trying to accomplish, you might want to take the time to learn how to do this by hand. Database manipulation is one of the more basic function of PHP and a great starting point into really learning the language. Once you learn how much flexibility you have hand coding your database stuff you will probably never go back.