Jump to content

Problem When Assigning a Foreign Key


chaseman

Recommended Posts

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
Share on other sites

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
Share on other sites

Good job thinking it through :)

 

Thanks a lot :) I'm finally learning to become a problem solver just like a real programmer haha.. :P

 

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.