Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to store the userid in the session variable for newly created user after registration
    primarykey
    data
    text
    <p>I have a problem in fetching the value from the <code>userid</code> column of MySql table and storing it in session.</p> <p>Here is the problem in detail:</p> <p>I have a database with many tables but lets consider 2 for now.</p> <p>Table name <b>login</b>:<br></p> <pre><code>| userid | username | password </code></pre> <p>Table name <b>person_details</b>:</p> <pre><code>| firstname | lastname | dob </code></pre> <p>Other than this i have (consider) 2 pages with 2 forms (1 at each page);</p> <p>One for login and other for details after login.</p> <p>My <code>person_details</code> table's <code>userid</code> is foreign key to <code>login</code> table. </p> <p>what i want is when an user register, the provided <code>username</code> and <code>password</code> should be saved in the <code>login</code> table with an unique(autogenerated) <code>userid</code> which is currently working fine.*</p> <p>Now the problem is in this part; when the user is registered then he/she is directed to <code>person_details</code> form and on clicking submit after filling out the form, the data should save to <code>person_details</code> table <strong>with the same 'userid' as in 'login' table</strong>.</p> <pre><code>In order to do that I think i have to store the 'userid' in the session variable so that it can be used as a parameter while inserting the value to 'person_details' table but i am not able to pass that value. </code></pre> <p>here is my code:</p> <pre><code>if ((isset($_POST["MM_insert"])) &amp;&amp; ($_POST["MM_insert"] == "login")) { $insertSQL = sprintf("INSERT INTO secure_login (username, password) VALUES (%s, %s)", GetSQLValueString($_POST['username'], "text"), GetSQLValueString($_POST['password'], "text")); mysql_select_db($database_mydatabase, $mydatabase); $Result1 = mysql_query($insertSQL, $mydatabase) or die(mysql_error()); $obj = @mysql_fetch_object($Result1); $_SESSION['userID']=$obj-&gt;userid; </code></pre> <p>i tried this too but no luck</p> <pre><code> mysql_select_db($database_mydatabase, $mydatabase); $Result1 = mysql_query($insertSQL, $mydatabase) or die(mysql_error()); $_SESSION['userID']=$Result1-&gt;userid; </code></pre> <p>This is the error i am getting when trying:</p> <pre><code>Cannot add or update a child row: a foreign key constraint fails (`mydatabase`.`personaldetails`, CONSTRAINT `personaldetails_ibfk_2` FOREIGN KEY (`userid`) REFERENCES `secure_login` (`userid`) ON DELETE CASCADE ON UPDATE CASCADE) </code></pre> <p>and i tried it without session and it was ok, data was saved to the table <code>person_details</code> with autogenerated <code>userid</code>.</p> <p>Please let me know if u need anymore of the code or any other information.</p> <p><strong>EDIT</strong> I got the userID in session variable, so half of my problem is solved. Now all i need to do is to set the same userid for person_details.</p> <p>P.S. When i posted this question here, i expected an answer but all i got was some visits and a fellow doing some editing for me (which i think he enjoyed so much that he edited some of my comments as code) anyways, thank you friend.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload