Hi,

I have want to insert session variable in my database. I have developed a blog and I want to insert session variable in one of the fields defined in my database. I have the code which can insert other fields in the database but I do not know how to insert the session variable.

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form")) {
  $insertSQL = sprintf("INSERT INTO blog (title, specialization, message, time, email) VALUES (%s, %s, %s, %s , %s, %s)",
                       GetSQLValueString($_POST['title'], "text"),
                       GetSQLValueString($_POST['specialization'], "text"),
                       GetSQLValueString($_POST['words'], "text"));

In the table I have the following fields:

blog_id, title, message, time, email.

Email in the last will be taken from the session variable. Can any one please help me and tell me the code which can take an email from session and put it in the database.

Regards,

Bilal A. Khan

Try :

session_start();
if (isset($_SESSION['session_email']))
{
echo $_SESSION['session_email'];
// You can use $_SESSION['session_email'] to insert the email wherever you want just like a simple variable
}

Try :

session_start();
if (isset($_SESSION['session_email']))
{
echo $_SESSION['session_email'];
// You can use $_SESSION['session_email'] to insert the email wherever you want just like a simple variable
}

Thank you very much for the reply. I have changed my syntax something like this:

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form")) {
  $insertSQL = sprintf("INSERT INTO blog (title, specialization, message, email) VALUES (%s, %s, %s ,".$_SESSION['MM_Username'].")",
                       GetSQLValueString($_POST['title'], "text"),
                       GetSQLValueString($_POST['specialization'], "text"),
                       GetSQLValueString($_POST['words'], "text"));

With this code it is giving me the following error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@gmail.com)' at line 1

Can anyone please tell me what does this error mean? I believe it has to do something with the way I am inserting the session variable in my database because my session variable contains an email address as login.

Any help will be highly appreciated.

Regards,

Bilal A. Khan

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.