Forum Moderators: open
The problem is that the user in a table doesn't get recorded after he/she logs in. The field for user just records 0, whereas in the past it would give the user a number.
There are two tables, one registers the users and that table works fine. The other table records the titles of books a user has read. I use php's session feature to keep track of the users:
In the login php:
// Start the session, register the values & redirect.
$_SESSION['first_name'] = $row[1];
$_SESSION['user_id'] = $row[0];
And in the page where user adds a title.
// Start output buffering and initialize a session.
ob_start();
session_start();?>
I can see that the titles get entered in the second table, but no number for the user.
My php for the table that doesn't record the user looks like this:
$query = "INSERT INTO books (book_id, user_id, title)
VALUES ('$book_id', '$user_id', '$title')";
Any ideas as to why my table doesn't work anymore is greatly welcomed.
Autoincrementing id fields from a post form... [webmasterworld.com]
I don't know if it is related but it is something to check into ... your version upgrade is the tip-off.
My first thought was that I needed to set the auto increment for the user_id field but when I do that I get this response:
"Incorrect table definition; there can be only one auto column and it must be defined as a key"
My primary field, book_id, in this table is set to auto increment and in the other table, the users table, the primary field is user_id and it is auto incremented. So I don't know how I get this field in the book table to record a user when he enters a title into his reading log. I thought that was what the Session function did. Like I said, it worked at one time before my hosting service moved to another server.
I use MYSQL 4.1.21 and PHPMyAdmin 2.8.24
This is a mystery that might have to do with this server change since I'm pretty certain I haven't changed anything in my PHP scripts.
Thank you for trying to help me.
Have you checked the $_SESSION superglobal after a successful login to ensure your variables are being populated? Put a line of code in place that will dump it out for you. I often check the userID before dumping it so that I am not interrupting any other developers ...
<?php
// session_start and other stuff has already happened here
if ($userName == 'Ann_G') {
print '<pre>'; print_r($_SESSION); exit('</pre>');
}
?>
After a successful login you said that the username is displaying on the page correctly which you are pulling from the $_SESSION superglobal. I am assuming the userID is in the same $_SESSION array so by dumping it out at certain points in your process you can tell whether or not you are carrying it throughout your session, especially right before you use it to log the entries in the database table where you are seeing your issues.
In have a header in my includes file that has this code you see below and which I read would be the correct way to track a user.
But since I get nothing with the code you gave me, shall I assume the session is working (although the user is greated by name once logged in)?
This is very confusing.
I do appreciate your taking time to help. If you want to give up I understand, I'm about ready to give up.
// Start output buffering and initialize a session.
ob_start();
session_start();
<?php
// session_start and other stuff has already happened here
if ($_SESSION['userName'] == 'Ann_G') {
print '<pre>'; print_r($_SESSION); exit('</pre>');
}
?>
An error occurred in script /mydirectory/myfilename/includes/config.inc
line 12:ob_flush()[ref.outcontrol]: failed to flush buffer: No buffer to flush.
ref.outcontrol is a hotlink in the error message, but of course when I click on it, I get "page cannot be found".
I know the config.inc just sets the error reporting for the site.
Is the error message referring to my include/header file and this:
// Start output buffering and initialize a session.
ob_start();
session_start();
I'm at loss. I haven't edited any of these files since the time it worked and the user_id was recorded.
But my problem, to get the the user_id recorded in my MYSQL table, remains. When I use this statement:
$query = "INSERT INTO books (book_id, user_id, title, author) VALUES('$book_id', '$user_id', '$title', '$author')";
// Run the query.
$result = @mysql_query ($query);
all the info is entered into the table correctly but the user_id column remains at 0.
$query = "INSERT INTO books (book_id, user_id, title, author) VALUES('$book_id', '$user_id', '$title', '$author')";
exit($query);
// Run the query.
$result = @mysql_query ($query); You do know that you should be moving that $_SESSION value into $user_id or using the $_SESSION value, correct? Old PHP programming and scripts used to have register_globas [php.net] turned on but upon realizing the security implications the developers have turned it off by default on PHP installations now. I wonder if that is the issue you are experiencing?
Welcome, Ann_G!
Welcome, 1!
INSERT INTO books (book_id, user_id, title) VALUES('', '0', 'test')
So the session user_id registers since there is a 1 there, but then it doesn't go into the values. Am I overlooking something very obvious? (PHP can really make you feel stupid)
I believe I'm using the superglobal $_SESSION
For example, in my login file I have this.
// Start the session, register the values & redirect.
$_SESSION['first_name'] = $row[1];
$_SESSION['user_id'] = $row[0];
Doesn't that mean my $_SESSION value is moved into my $user_id?
As I said I'm very spotty in my knowledge of php.
I have learnt a lot even if my problem isn't solved. So please don't hesitate to quit, I realize it's very time consuming to help php dabblers like me.
$_SESSION['user_id'] = $row[0];
That line looks like it is moving a value that was retrieved from the database into your $_SESSION superglobal array under the index 'user_id'. A very typical expression. Now, we know that value is '1' because you are seeing it when you print out some of your $_SESSION information ...
Welcome, Ann_G!
Welcome, 1!
At least, that is what I am guessing that you are doing here. Am I correct? The line of PHP code that prints out that second line there probably looks something like this ...
print 'Welcome, ' . $_SESSION['user_id'];
Now, the big question is, does the $user_id variable ever get populated with the $_SESSSION['user_id'] value at any point prior to you using it in your query? Something like this ...
$user_id = $_SESSION['user_id'];
It doesn't happen magically. I say that, not being sarcastic, but very serious. You see, in the recent past PHP used to auto-magically populate variables like the $user_id variable here because of that
register_globalssetting mentioned earlier. Now, because of security issues, PHP installations turn that directive Off and variables don't get magically populated with their related global counterparts, in this case $_SESSION['user_id']. You have to do that manually, basically forcing you to recognize that you are using a variable with possible user-supplied (and possibly malicious) values and you have made sure it is good to use.
See if you can tell where you are populating the $user_id variable, if anywhere, and how it is populated. If you don't see a line anywhere like I have shown here, then you may have found your issue.
I put this user_id value on every page.
$user_id = $_SESSION['user_id'];
But the value in the Insert statement still reads 0.
It's Friday. I need to forget about this to Monday.
Hope you have a good weekend!
I followed your steps and having put that
variable $user_d = $_SESSION['user_id']
and still not getting a value in the query
made no sense at all.
So I took one more look at my query and
discovered that the order didn't match
the MYSQL table rows' order.
I had two inserts andvalues switched around.
I didn't check that the order matched very
carefully before, because I didn't realize
it could screw up the $_Session insert.
Once again thanks!