chaseman Posted February 13, 2011 Share Posted February 13, 2011 I have two tables in my MySQL database: 1. user user_id | firstname | lastname | nickname etc. and 2. con (for contribution) con_id | user_id (foreign key) | name | contribution | category etc. Here's how I wanted to solve the problem of assigning the foreign key to the 2nd table (I'm a beginner so bear with me. ) When logging in, I assigned the user_id to the session variable like this: // do the while loop while ($assoc = mysqli_fetch_assoc($rows)) { // assign database COLUMNS to the variables $dbuser_id = $assoc['user_id']; $dbuser_name = $assoc['nickname']; $dbuser_password = $assoc['password']; etc..................... // set a session after login $_SESSION['user'] = $dbuser_name . $dbuser_id; This prints out the user_id just like I wanted to: echo "Your user_id is: " . $_SESSION['user_id'] = $dbuser_id; Gives: Your user_id is: 35 ... and this is how the assignment of the foreign key looks like WHILE posting the contribution: $user_id = $_SESSION['user'] = $dbuser_id; if (!empty($knuffix_name) && !empty($knuffix_category) && !empty($knuffix_contribution)) { // Write the data into the database $query = sprintf("INSERT INTO con (con_id, user_id, name, contribution, category, contributed_date) VALUES (' ', '$user_id', '%s', '%s', '%s', now())", mysqli_real_escape_string($dbc, $knuffix_name), mysqli_real_escape_string($dbc, $knuffix_contribution), mysqli_real_escape_string($dbc, $knuffix_category)); When I now do a contribution through the input areas, nothing gets inserted into the database and I automatically get logged out. Obviously it's not working as I thought it would and I'd like to know why is that? Notice that if I take the user_id part OUT, it's working again, so the rest of the code must be right then. Another question I have is: Is this common practice to solve this problem of assigning a foreign key, or is there a better way of doing it? Link to comment https://forums.phpfreaks.com/topic/227541-problem-when-assigning-a-foreign-key/ Share on other sites More sharing options...
chaseman Posted February 13, 2011 Author Share Posted February 13, 2011 I think the mistake I'm doing is I'm re-assigning the session variable with this: $_SESSION['user'] = $dbuser_id; Now I have only the user_id inside the session variable, and the nickname gets cleared. Is there any way I can tell the session variable to just give me the user_id, but keep the nickname inside the session variable? I think I'll just try assigning two session variables, one for the nickname and one for the id. That may work. Link to comment https://forums.phpfreaks.com/topic/227541-problem-when-assigning-a-foreign-key/#findComment-1173714 Share on other sites More sharing options...
Jessica Posted February 13, 2011 Share Posted February 13, 2011 I think I'll just try assigning two session variables, one for the nickname and one for the id. That may work. Good job thinking it through Link to comment https://forums.phpfreaks.com/topic/227541-problem-when-assigning-a-foreign-key/#findComment-1173716 Share on other sites More sharing options...
chaseman Posted February 13, 2011 Author Share Posted February 13, 2011 Good job thinking it through Thanks a lot I'm finally learning to become a problem solver just like a real programmer haha.. This is how I solved it: $_SESSION['user_name'] = $dbuser_name; $_SESSION['user_id'] = $dbuser_id; As I said, the mistake I was doing is that I was re-assigning it, and I was thinking I was simply taking out the user_id part of the session variable, but I guess PHP doesn't work that way. Now that it's working I hope this is good practice of doing it and I don't encounter vulnerabilities in the future. Link to comment https://forums.phpfreaks.com/topic/227541-problem-when-assigning-a-foreign-key/#findComment-1173721 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.