markifornia Posted April 6, 2012 Share Posted April 6, 2012 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 More sharing options...
Larry Posted April 7, 2012 Share Posted April 7, 2012 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 More sharing options...
markifornia Posted April 7, 2012 Author Share Posted April 7, 2012 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 More sharing options...
bahaa Posted April 8, 2012 Share Posted April 8, 2012 I use the user session id to get the user id and add it to the table. Link to comment Share on other sites More sharing options...
markifornia Posted April 9, 2012 Author Share Posted April 9, 2012 Thank you Bahaa I will use this method. Is this the most common way? Do you have a snippet of code sample, it doesn't have to be elegant. I just wanted to get an idea of how it is done. Thanks, Mark Link to comment Share on other sites More sharing options...
bahaa Posted April 9, 2012 Share Posted April 9, 2012 I check for the ID like this if(isset($_SESSION['UserID'])){ $UserID = (int)$_SESSION['UserID']} 1 Link to comment Share on other sites More sharing options...
markifornia Posted April 9, 2012 Author Share Posted April 9, 2012 Thanks Bahaa, I will use this method in my web applications. Link to comment Share on other sites More sharing options...
masterlayouts Posted April 9, 2012 Share Posted April 9, 2012 I had the same questions a while ago, I guess the correct answer is to use transactions. There should be those posts about transactions, Larry provided clear answers. Link to comment Share on other sites More sharing options...
markifornia Posted April 9, 2012 Author Share Posted April 9, 2012 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 More sharing options...
bahaa Posted April 9, 2012 Share Posted April 9, 2012 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 More sharing options...
markifornia Posted April 9, 2012 Author Share Posted April 9, 2012 Thanks bahaa, I've crossed this somewhere in the book, better explained in the last chapter I think. I will study this further when I get to it. Link to comment Share on other sites More sharing options...
suba Posted August 7, 2012 Share Posted August 7, 2012 I have seen It in:-http://adf.ly/FgFOE Link to comment Share on other sites More sharing options...
Recommended Posts