php : variable value not saved in db
<p>I have a php code where i am taking the logged in user name. Then the form comes and then i need to save the logged in user name in db.</p>

<pre><code>&lt;?PHP
 function renderForm($id, $task, $comments, $assignee, $priority, $status, $dataum1, $dataum2, $error )
 {
require_once("./include/membersite_config.php");

if(!$fgmembersite-&gt;CheckLogin())
{
    $fgmembersite-&gt;RedirectToURL("login.php");
    exit;
}
$user = $fgmembersite-&gt;UserFullName();
?&gt;
</code></pre>

<p>Html Form comes here...........</p>

<pre><code>&lt;?php
 }



 // connect to the database
 include('configdb1.php');

 // check if the form has been submitted. If it has, process the form and save it to the database
 if (isset($_POST['submit']))
 { 

 mysql_query("INSERT INTO comments (comment_id, comments, user) VALUES ('$id', '$text', '$user')")
 or die(mysql_error()); 


?&gt;
</code></pre>

<p>But the $user which i declared as the login name is not getting inserted into db. Please help.</p>

<p>This is my entire code :</p>

<pre><code>&lt;?PHP
 function renderForm($id, $task, $comments, $assignee, $priority, $status, $dataum1, $dataum2, $error )
 {
require_once("./include/membersite_config.php");

if(!$fgmembersite-&gt;CheckLogin())
{
    $fgmembersite-&gt;RedirectToURL("login.php");
    exit;
}

?&gt;


&lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"&gt;
&lt;head&gt;
&lt;title&gt;&lt;/title&gt;
&lt;link href="style.css" rel="stylesheet" type="text/css" /&gt;
   &lt;script language="javaScript" type="text/javascript" src="calendar.js"&gt;&lt;/script&gt;
&lt;script src="rollover.js" type="text/javascript"&gt;&lt;/script&gt;
&lt;script src="datetimepicker.js" type="text/javascript"&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body id="page1"&gt;
&lt;table class="main"&gt;
    &lt;tr&gt;
        &lt;td id="header"&gt;
            &lt;table class="centre"&gt;
                        &lt;td class="column3"&gt;
                                                &lt;table&gt;

                                                      &lt;tr&gt;
                                                      &lt;td &gt;
                                                      &lt;/br&gt;
                                                      &lt;color = 'white'&gt;
                                                      Welcome back   &lt;?= $fgmembersite-&gt;UserFullName(); ?&gt;! , &lt;a href='logout.php'&gt;Logout&lt;/a&gt;&lt;/p&gt;
                                                      &lt;/br&gt;
                                                      &lt;/td&gt;
                                                    &lt;/tr&gt;

                                                &lt;/table&gt;

                &lt;tr&gt;
                 &lt;td class="row2"&gt;&lt;a href="home.php"&gt;&lt;img src="images/m1.gif" alt="" color = "white" width="112" height="33" id="Image9"&lt;/a&gt;&lt;a href="myactivity.php"&gt;&lt;img src="images/m2_1.gif" alt=" " width="112" height="33" id="Image10" /&gt;&lt;/a&gt;&lt;a href="ba_report.php"&gt;&lt;img src="images/m3.gif" alt=" " width="112" height="33" id="Image11" /&gt;&lt;/a&gt;&lt;a href="automation_report.php"&gt;&lt;img src="images/m4.gif" alt=" " width="155" height="33"  id="Image12" /&gt;&lt;/a&gt;&lt;a href="functional_report.php"&gt;&lt;img src="images/m5.gif" alt=" " width="148" height="33" id="Image13" /&gt;&lt;/a&gt;&lt;a href="setting.php"&gt;&lt;img src="images/m6.gif" alt=" " width="112" height="33" id="Image14" /&gt;&lt;/a&gt;&lt;/td&gt;
                &lt;/tr&gt;
            &lt;/table&gt;

        &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
        &lt;td id="content"&gt;
            &lt;table&gt;
                &lt;tr&gt;

                    &lt;td class="column1"&gt;&lt;img src="images/spacer.gif" alt="" width="23" height="1" /&gt;&lt;/td&gt;
                    &lt;td class="column2"&gt;

                        &lt;table&gt;
 &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp  &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp  &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &lt;a href="myactivity.php"&gt;&lt;img src="images/t1.gif" alt="" color = "white" width="112" height="33" id="Image9"  /&gt;&lt;/a&gt; &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &lt;a href="view.php"&gt;&lt;img src="images/t2.gif" alt=" " width="112" height="33" id="Image10" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;



                            &lt;div class="signup_form"&gt;
 &lt;form action="" method="post"&gt;
  &lt;input type="hidden" name="id" value="&lt;?php echo $id; ?&gt;"/&gt;
                            &lt;p&gt;&lt;strong&gt;ID:&lt;/strong&gt; &lt;?php echo $id; ?&gt;&lt;/p&gt;
                                 &lt;tr&gt;&lt;font size="2" face="tahoma" color="black"&gt;&lt;b&gt;Enter the Task &lt;/b&gt;&lt;/br&gt;&lt;/br&gt; &lt;/tr&gt;&lt;input name="task" readonly value= &lt;?php echo $task; ?&gt; type="text" size = "127" input style="height:30px;font-size:13;" ...../&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
                                &lt;tr&gt;&lt;font size="2" face="tahoma" color="black"&gt;&lt;b&gt;Enter Task Description &lt;/b&gt;&lt;/br&gt;&lt;/br&gt; &lt;/tr&gt;&lt;textarea readonlyname="comments" ROWS="10" Cols = "91"&gt; &lt;?php echo $comments; ?&gt;&lt;/textarea&gt; &lt;br/&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
   &lt;?php
include('configdb.php');
    $query = "SELECT username FROM users";

    $result = mysqli_query($mysqli,$query) or die(mysqli_error($mysqli));
    $assignee = "&lt;select name='user'&gt; width = '200'";
    while($row = mysqli_fetch_assoc($result)) {
    $assignee .= "\r\n&lt;option width = '200'  value='{$row['username']}'&gt;{$row['username']}&lt;/option&gt;";
    }
    $assignee .= "\r\n&lt;/select&gt; ";

    ?&gt;                              
&lt;b&gt;Assignee: &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &lt;/b&gt;&lt;select name = "assignee" value = &lt;?php echo $assignee ?&gt;&lt;/select&gt; &amp;nbsp &amp;nbsp  &amp;nbsp &amp;nbsp  &amp;nbsp &amp;nbsp 

&lt;b&gt;Priority: &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &lt;/b&gt;&lt;select name = "type" id="type"&gt;
    &lt;option &lt;?php if ($type == 'MAC OS') { echo 'selected="selected"'; } ?&gt;&gt;MAC OS&lt;/option&gt;
    &lt;option &lt;?php if ($type == 'WINDOWS OS') { echo 'selected="selected"'; } ?&gt;&gt;WINDOWS OS&lt;/option&gt;
    &lt;option &lt;?php if ($type == 'GENERAL TASK') { echo 'selected="selected"'; } ?&gt;&gt;GENERAL TASK&lt;/option&gt;
&lt;/select&gt; &amp;nbsp &amp;nbsp  &amp;nbsp &amp;nbsp  &amp;nbsp &amp;nbsp 



&lt;b&gt;Priority: &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &lt;/b&gt;&lt;select name = "priority" id="priority"&gt;
    &lt;option &lt;?php if ($priority == 'Low') { echo 'selected="selected"'; } ?&gt;&gt;Low&lt;/option&gt;
    &lt;option &lt;?php if ($priority == 'Medium') { echo 'selected="selected"'; } ?&gt;&gt;Medium &lt;/option&gt;
    &lt;option &lt;?php if ($priority == 'High') { echo 'selected="selected"'; } ?&gt;&gt;High&lt;/option&gt;
&lt;/select&gt; &amp;nbsp &amp;nbsp  &amp;nbsp &amp;nbsp  &amp;nbsp &amp;nbsp 


&lt;b&gt;Status: &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &lt;/b&gt;    &lt;select name = "status" id="status"&gt;
    &lt;option &lt;?php if ($status == 'Assigned') { echo 'selected="selected"'; } ?&gt;&gt;Assigned&lt;/option&gt;
    &lt;option &lt;?php if ($status == 'Yet to Start') { echo 'selected="selected"'; } ?&gt;&gt;Yet to Start &lt;/option&gt;
    &lt;option &lt;?php if ($status == 'In Progress') { echo 'selected="selected"'; } ?&gt;&gt;In Progress&lt;/option&gt;
    &lt;option &lt;?php if ($status == 'Completed') { echo 'selected="selected"'; } ?&gt;&gt;Completed&lt;/option&gt;
     &lt;option &lt;?php if ($status == 'Blocked') { echo 'selected="selected"'; } ?&gt;&gt;Blocked&lt;/option&gt;
&lt;/select&gt;&lt;br/&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;b&gt;Start Date :&amp;nbsp &amp;nbsp &lt;/b&gt;&lt;input type="text" size = "25" input style="height:25px;font-size:13;" name="datum1" id="datum1" value="&lt;?php echo $dataum1; ?&gt;"&gt;&lt;a href="javascript:NewCal('datum1','ddmmmyyyy',true,24)"&gt;&lt;img src="cal.gif"&gt;&lt;/a&gt;&amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp  &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp  &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp  &amp;nbsp &amp;nbsp &amp;nbsp
&lt;b&gt;End Date :&amp;nbsp &amp;nbsp &lt;/b&gt;&lt;input type="text"  size = "25" input style="height:25px;font-size:13;" name="datum2" id="datum2" value="&lt;?php echo $dataum2; ?&gt;"&gt;&lt;a href="javascript:NewCal('datum2','ddmmmyyyy',true,24)"&gt;&lt;img src="cal.gif"&gt;&lt;/a&gt;&lt;br/&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
                            &lt;input name="submit" type="submit" value="submit"/&gt;
                            &lt;/form&gt;

                            &lt;/div&gt;
                        &lt;/table&gt;
                    &lt;/td&gt;
                &lt;/tr&gt;
            &lt;/table&gt;
        &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;    
        &lt;td id="footer"&gt;
        &lt;/td&gt;

&lt;/table&gt;

&lt;/body&gt;
&lt;/html&gt;

 &lt;?php
 }



 // connect to the database
 include('configdb1.php');

 // check if the form has been submitted. If it has, process the form and save it to the database
 if (isset($_POST['submit']))
 { 
 // confirm that the 'id' value is a valid integer before getting the form data
 if (is_numeric($_POST['id']))
 {
 // get form data, making sure it is valid
 $id = $_POST['id'];
 $task = $_POST['task'];
 $comments = $_POST['comments'];
$assignee = $_POST['assignee'];
 $priority = $_POST['priority'];
 $status = $_POST['status'];
 $dataum1 = $_POST['datum1'];
 $dataum2 = $_POST['datum2'];
    // check that firstname/lastname fields are both filled in
if ($task == '' || $comments == '')
 {
 // generate error message
 $error = 'ERROR: Please fill in all required fields!';

 //error, display form
 renderForm($id, $task, $comments, $assignee, $priority, $status, $dataum1, $dataum2, $error );
 }
 else
 {
 // save the data to the database
 mysql_query("UPDATE work SET task='$task', comments='$comments', assignee='$assignee', priority='$priority', status='$status', dataum1='$dataum1', dataum2='$dataum2' WHERE id='$id' ")
 or die(mysql_error()); 

 // once saved, redirect back to the view page
 header("Location: view.php"); 
 }
 }
 else
 {
 // if the 'id' isn't valid, display an error
 echo 'Error!';
 }
 }
 else
 // if the form hasn't been submitted, get the data from the db and display the form
 {

 // get the 'id' value from the URL (if it exists), making sure that it is valid (checing that it is numeric/larger than 0)
 if (isset($_GET['id']) &amp;&amp; is_numeric($_GET['id']) &amp;&amp; $_GET['id'] &gt; 0)
 {
 // query db
 $id = $_GET['id'];
 $result = mysql_query("SELECT * FROM work WHERE id=$id")
 or die(mysql_error()); 
 $row = mysql_fetch_array($result);

 // check that the 'id' matches up with a row in the databse
 if($row)
 {

 // get data from db
 // get data from db
 $task = $row['task'];
 $comments = $row['comments'];
  $assignee = $row['assignee'];
   $priority = $row['priority'];
    $status = $row['status'];
     $dataum1 = $row['dataum1'];
      $dataum2 = $row['dataum2'];

 // show form
 renderForm($id, $task, $comments, $assignee, $priority, $status, $dataum1, $dataum2,'');
 }
 else
 // if no match, display result
 {
 echo "No results!";
 }
 }
 else
 // if the 'id' in the URL isn't valid, or if there is no 'id' value, display an error
 {
 echo 'Error!';
 }
 }
?&gt;
</code></pre>
