Extract username from session and add to database

Started on the old forum in November 2012 · view the original in the archive · old links redirect here

3 replies · 640 views · started 14 years ago · 1 min read

2 participants
cedry2k Junior · 15 posts #1

Hi, I have the following problem:

I have a html form for articles and another file that insert data into database. I want to write in the database the id of the author and in the file that insert information in the database I got this code:

$sname = $_SESSION['username'];
$select = "SELECT * FROM users WHERE name = '$sname'";
$result = mysql_query($select) or die (mysql_error());
while ($curent_user = mysql_fetch_assoc($result)){
$author = $curent_user['id'];
}

And in the query the $author, but don't work, write 0 in the author_id field but the id of my account in the users table is 5. How can I make this to write the good id? Sorry for my english and thanks in advance!

do{ beer++ }while(sober); ===//\==\//=== while($lifes_a_bitch == true) { live(); } die;

arnage Lead · 811 posts #2

Try...

$sname = !empty($_SESSION['username']) ? $_SESSION['username'] : null;
$select = 'SELECT * FROM users WHERE name = "'.$sname.'"';
$result = mysql_query($select) or die (mysql_error());
$curent_user = mysql_fetch_array($result)
$author = $curent_user['id'];
<!DOCTYPE html PUBLIC "-//***WAPFORUM**.**RS***
cedry2k Junior · 15 posts #3

Thank you, this works like a charm.

do{ beer++ }while(sober); ===//\==\//=== while($lifes_a_bitch == true) { live(); } die;

arnage Lead · 811 posts #4

You're welcome.

<!DOCTYPE html PUBLIC "-//***WAPFORUM**.**RS***

Write your reply now; next you create a free account (or sign in) and it is posted right away. Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close