Jump to content

Auto Username into database


AWithers04

Recommended Posts

Hello everyone,

 

I am very new to PHP and basically learned alot on my own. ???  I am creating a message board for a website and the user has to log in to post a message. the database has 2 fields...Name and Comment. I have created a form with an insert query to submit the message to the db....what i would like it to do is automatically post the logged in users name to the Name field when they submit the message. How can i go about doing this.

 

Also i am using Dreamweaver CS3

 

Thanks

 

 

 

Link to comment
Share on other sites

Well, where is the username stored when they log in? In many cases the username is stored to a session variable such as $_SESSION['username'].

 

So, when the user submits a comment, just use that variable.

 

Example:

$query = "INSERT INTO table (name, comment) VALUES ('{$_SESSION['username']}', '{$_POST['commnet']}')";

Link to comment
Share on other sites

Here is my insert code for the comment....where should i call the session variable and in what syntax?

 

 

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {

  $insertSQL = sprintf("INSERT INTO comments (Comments) VALUES (%s)",

                GetSQLValueString($_POST['comment'], "text"));

 

  mysql_select_db($database_HiddenRidge, $HiddenRidge);

  $Result1 = mysql_query($insertSQL, $HiddenRidge) or die(mysql_error());

}

 

Link to comment
Share on other sites

Well the query would look something like this:

$insertSQL = sprintf("INSERT INTO comments (Name, Comments) VALUES (%s, %s)",
                       GetSQLValueString($_SESSION['username'], "text"),
                       GetSQLValueString($_POST['comment'], "text") );

 

But, I don not know your database or where you are storing the username - if you even are. Make sure you know that the column names in the query are correct and that you are properly accessing the username.

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.