View Full Version : Insert session value for userID into 2nd table
Kevin373660
05-28-2009, 08:42 AM
I need help to understand how, procedure and the php code to Insert the session value for the logged in userID (and userEmail) into a second table field. The second table has additional information that the user will create after hitting the submit button.
I have created MySQL db tables with the InnoDB engine and am using PHP.
Example:
Tbl_parents -userSID, userEmail
Tbl_children -kidSID, userSID, userEmail
In tbl_children, userSID is FK to tbl_parents userSID, and is set to cascade from tbl_parents. I believe that the log in page created with the WebAssist Wizard starts a session for the login user. I am assuming that when setting up eCart (not done yet) a similar code would input the userID into the order table.
In my case, the parent has logged in with their account information, redirected to their profile page which pulls up their information (record) and then goes by link to a second profile page that allows them to set up the children’s accounts in the second table with unique ID’s. I am sure that this seems simple but I am new to the process. Thank you for your assistance with this.
Ray Borduin
05-28-2009, 09:36 AM
The user information can be stored as a Session variable and you can use that session variable when doing the child inserts associated with that user.
Kevin373660
05-28-2009, 11:21 AM
Yes, I belive that a Session variable for the parent is created at login and that could be used when doing the child inserts associated with that user. I am just not sure how.
There must be a way of qualify the $session variable=userID. I have already setup the insert record to handle it if I can qualify it.
Ray Borduin
05-28-2009, 11:44 AM
When you log in the session variable is automatically set. You don't need to add anything you just need to use it.
Kevin373660
05-29-2009, 01:37 PM
Ok. In the member_Registratin.php added a record set and have set up the following code to verify that I can see these mysterious values when the page loads after user logs in. The correct values from table1 show on the Registration page. The insert statment should but it does not insert the data into table2? I realy need help on this, please.
</php
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "WAATKRegistrationForm")) {
$insertSQL = sprintf("INSERT INTO tbl_tablename2 (kidName, userSID, userEmail, kidRegDate) VALUES (%s, %s, %s)",
GetSQLValueString($_POST['kidFirstName'], "text"),
GetSQLValueString($_POST['kidName'], "text"),
GetSQLValueString($_POST['userID'], "int"),
GetSQLValueString($_POST['userEmail'], "text"),
'Now()';
?>
<tr>
<th>userSID:</th>
<td><?php echo $row_tablename1['userSD']; ?></td>
</tr>
<tr>
<th>userEmail:</th>
<td><?php echo $row_Parent['userEmail']; ?></td>
</tr>
Ray Borduin
05-29-2009, 03:02 PM
It doesn't look like you execute the SQL ever. Do you get an error? Maybe I'm confused.
Kevin373660
05-29-2009, 04:50 PM
Only two sections of the page code are in the last post. Zip attached. I am not sure what you mean by , "execute the SQL?" There are no errors. Once the registration page is submitted the record is created but the values for the userSID and userEmail are not entered into the database. Sorry to ask Ray but I realy need your help. Please and thank you.
Ray Borduin
06-01-2009, 07:05 AM
Right before or after the line:
$Result1 = mysql_query($insertSQL, $TugBoatBen) or die(mysql_error());
add the code:
die($insertSQL);
That will help you debug why the insert doesn't work properly.
Kevin373660
06-01-2009, 07:52 AM
Thank you Ray, I have added the die($insertSQL); line as you suggested. It seems to confirm that the insert is occuring, which it is. However the parents ID & email address still are not being inserted. Support has offered to assist by phone today. The combined help is very important to the success of my project and is very, very much appreciated. I have uploaded the most resent page and the error messages into the following Technical Support Ticket # 91530.
Ray Borduin
06-01-2009, 08:19 AM
It looks like you are using the standard dreamweaver insert server behavior.... or hand coding an insert statement. You should be using the Insert Server Behavior that comes with DataAssist so that the ID is stored in the session. I also don't see you referring to the session for the user email or for the ID. You could update it to look in the $_SESSION instead of the $_POST but first you need to make sure you know the session variables are stored and what their names are.
Kevin373660
06-01-2009, 03:22 PM
When I added the die code and saved the file, the Server Behavior changed as you noted. I had used the WebAssist Wizard/DataAssist but it only allowed me to work with one table (kids) and the loged in userID and pw are in another table (parents). I have gone back to the previous page version before the added die code.
I am sure that you are correct regarding the session variables. To start/continue the session on the member_Registration, do I need to add the block code to start (continue) the session? Do I need to set parents session varibles somehow on the previous webpage (parents_Profile) so that they can be carried forward and referanced on the Member_Registration page?
How do I confirm that the session variables are stored and what their names? How do I update it to look in the $_SESSION instead of the $_POST? Do I put it in the start session block at the begining of the page? What is the correct syntex? Examples that I have read on the web seem conflicting and what I have tried does not work. I have spent days on this and am totaly lost. Once I have a good example that can work, then I will be able to understand and reuse the same principle as needed.
Ray Borduin
06-02-2009, 06:01 AM
Start with the first insert. Remove the DW server behavior and use the DataAssist server behavior for inserting the record. It will automatically store the inserted id as a session variable and will ask you the name of it in the interface. This will give you a place where you can reference the session variable name and assure you that it is set properly.
Then simply try to display that session variable on the next page before using it as part of the second insert.
To the top of the page add:
<?php
session_start();
die($_SESSION['yourvariablename']);
?>
Once that can be used to display the session variable on the next page, you can use that same syntax to reference the session variable in your insert. (leave the session_start() code at the top of the page and remove the die(); line once it displays the session variable properly)
Kevin373660
06-02-2009, 04:56 PM
I have made some headway today with Sessions and forwarding values. Also found some helpful code sample in one of your replies to another member in the Forum. The user email address used in the login, does not seem to be in the global $_SESSION. I have tryied a few diferant naming conventions. Looks like it is put into a $_POST session in the EmailPW page. But I am not sure how to put it into the $_SESSION. Can you give me a clue?
Ray Borduin
06-03-2009, 06:10 AM
On the login page... there is a server behavior applied to log the user in (possibly applied twice if you have autologin enabled). If you update that server behavior, the last screen gives you an option to save as many session variables as you want from that user's profile.
Use that to create all the session variables you might need later.
Kevin373660
06-03-2009, 09:13 AM
Ray you have saved the day!!! I got it working.
For the benifit of other users, I found it helpful to:
1. Under the Bindings tab, set a global session $_SESSION variable. Automates the process and help you track them later.
2. In the Login page the Session Behavior that Ray mentioned was (identified in my case as) Security Assist Authenticate User, double click, keep the settings that you already have (if already configured earlier) and on the 3rd Step you can add variables from your table fields to the $_SESSION. By earlier setting up the variable array names in the Bindings, the gui will allow you to (should pick up automatically) the session value name that you want to assosciate the value to in the session.
vBulletin® v3.8.1, Copyright ©2000-2012, Jelsoft Enterprises Ltd.