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
https://forums.phpfreaks.com/topic/124220-auto-username-into-database/
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']}')";

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());

}

 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.