Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

session search

Status
Not open for further replies.

janet24

Technical User
Jul 22, 2003
161
US
Sorry I'm very new to PHP.I'm trying to use the session variable to update a database. It says I'm logged on but only inserts the data when I have the actual name in the code and not the session variable. It seems like this code should work but it's not updating the database.

<?
if($logged_in){
echo 'Logged in as '.$_SESSION['username'].', <a href="logout.php">logout</a>';

$q = "UPDATE userslogin SET test='help' where username= " . $_SESSION['username'] . " ";
$result = mysql_query($q,$conn);


?>
 
It's on the top of this page. The session is working because it says I'm logged in when I log-in. Should the script work the way it's written?
 
Probably not. But I have in sufficient data for a meaningful answer. I don't know what datatype your table column "username" is nor what you're storing in $_SESSION['username'].

If "username" is a character type and if $_SESSION['username'] holds a username, for example "george", then the query you want to build should read something like:

UPDATE userslogin SET test='help' where username= 'george'

Your code will not do that, as it is missing the quotes around the username.


If "username" is a numeric type and if $_SESSION['username'] holds a numeric userid, then the quotes are not necessary.


Want the best answers? Ask the best questions! TANSTAAFL!
 
One problem you're having debugging this is that you're not asking PHP for error messages.

If you change:

$result = mysql_query($q,$conn);

for

$result = mysql_query($q,$conn) or die(myssql_error());

does the behavior of the script change?


Want the best answers? Ask the best questions! TANSTAAFL!
 
Have you connected to the database, using mysql_connect ?
Have you chosen the correct database using mysql_select_db?
 
It was quotes I was missing. Thanks You, I've been going around in circles all day.
 
Everything I've posted is in my FAQ in this forum, titled, "Debugging PHP". There's other useful advice in there, too.

faq434-2999


Want the best answers? Ask the best questions! TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top