Jump to content
Larry Ullman's Book Forums

Insert Data Across Multiple Tables Using One Form


Recommended Posts

I have just finished Chapter 9, to get a feel for were I'm at in my reading.

 

Chapter 6 (Advanced SQL and MySQL) does an excellent job of explaining database design and relating tables through primary and foreign keys. I understand how to SELECT columns from multiple tables using table JOINS.

 

Ok, my question is:

 

How do you INSERT data across multiple tables? When working with forms, should forms correspond to one table at a time?

 

Say the form has name, email fields that corresponds to the users table. And then the same form also has address fields that correspond to the location table.

 

 

I might of been getting ahead of myself here as Chapter 11 explains how to track users through sessions, I have skimmed the chapter a little as I've become very curious. I don't know to much about sessions at this point. Is sessions the practical most widely used method for accomplishing this?

 

Thanks,

Mark

Link to comment
Share on other sites

You insert data into multiple tables using multiple queries. While one HTML form may correspond to one database table, that's not always the case.

 

Sessions are often the most widely used method for tracking users, depending upon the particulars of the site.

Link to comment
Share on other sites

Thanks Larry.

 

Here is a very simplified example for form.html for the form markup, handler.php as the handler.

 

form.html

 

<form action="edit.php" method="post">

<tr>

<td>Name: <input type="text" name="name" value=""></td>

</tr>

<tr>

<td>Address: <input type="text" name="address" value=""></td>

</tr>

<tr>

<td><input type="submit" name="submit" value="submit"></td>

</tr>

</form>

 

_____________

 

handler.php

 

$name = $_POST['name']';

$address = $_POST['address'];

 

$n = "INSERT INTO users (user_id, name) VALUES ('$name'); // users table

 

$a = "INSERT INTO address (user_id, address) VALUES ('$address')"; // address table

 

 

I just realized my question now is, how to insert data into multiple tables for one unique user id? Put simple, which user (for instance in an admin environment) user added a record. Would I use the session id?

 

I understand there are two methods: (1) using a hidden field (2) passing the values to the URL using $_GET.

 

Thanks,

Mark

Link to comment
Share on other sites

Masterlayouts,

 

I am only inclined to relate the term transactions with something having to do with ecommerce. Does the transaction involve using the session id? It seems that when recording a post data (or many) per unique user, utilizing the session variable is required. Is it not?

Link to comment
Share on other sites

Masterlayouts,

 

I am only inclined to relate the term transactions with something having to do with ecommerce. Does the transaction involve using the session id? It seems that when recording a post data (or many) per unique user, utilizing the session variable is required. Is it not?

 

The transactions are used if you want to inster multi record to the database and you want to make sure all queries succeed or roll back if something went wrong.

 

example of using transation is when transfering money from one account to another, if you send money to another acount and the money withdrown from you account and then something happens before the are deposited in the other account then everything roll back.

So in the bank account example when useing the transation is forcing to the queries to roll back if something goes wrong.

Link to comment
Share on other sites

  • 3 months later...
 Share

×
×
  • Create New...