使用php在表单提交时将用户名从会话存储到数据库

时间:2013-05-02 08:49:12

标签: php html sql

我有一个简单的问题,

我有一个登录和工作区。

用户登录后显示我在工作场所登录用户的用户名。现在我的问题是当用户在他的工作区中填写表单时,表单然后存储在数据库中,我还需要来自会话的用户名也存储到数据库中。

此处是用户在登录后在工作区到达后存储用户名和维护会话的代码:

<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/MainProject/connect/auth.php');
session_start();
 ?>

更新的插入文件的最终版本:

 //This code is included to check session and store username
 <?php
 require_once('..\connect\auth.php');
// session_start();
$usern = $_SESSION['SESS_FIRST_NAME'];
 ?>
<?php
mysql_connect('localhost','root','');
mysql_select_db('main_project') or die (mysql_error());

if(isset($_POST['WID'])){
for ($ix=0; $ix<count($_POST['WID']); $ix++)
{
    $WID = mysql_real_escape_string(@$_POST['WID'][$ix]);
    $website = mysql_real_escape_string(@$_POST['website'][$ix]);

    //var_dump("<pre>", $_POST['cat']); die();   // Debugger for checking cat counter.
    // $cat = implode(",", mysql_real_escape_string($_POST['cat'][$ix]));       

    if(is_array(@$_POST['cat'][$ix]))
    $cat = mysql_real_escape_string(implode(',', @$_POST['cat'][$ix]));
    else
    $cat = mysql_real_escape_string(@$_POST['cat'][$ix]);   

    $email = mysql_real_escape_string(@$_POST['email'][$ix]);
    $cform = mysql_real_escape_string(@$_POST['cform'][$ix]);
    $contactp = mysql_real_escape_string(@$_POST['contactp'][$ix]);
    $contacts = mysql_real_escape_string(@$_POST['contacts'][$ix]);
    $fax = mysql_real_escape_string(@$_POST['fax'][$ix]);
    $Ctype = mysql_real_escape_string(@$_POST['Ctype'][$ix]);
   $usern = mysql_real_escape_string(@$_POST['usern'][$ix]);

    $sql_res = mysql_query("INSERT INTO website_01data (WID,website,cat,email,cform,contactp,contacts,fax,Ctype,TimeStamp,usern) 
    VALUES ('".$WID."', '".$website."', '".$cat."', '".$email."','".$cform."', '".$contactp."', '".$contacts."', '".$fax."', '".$Ctype."', Now(), '".$usern."' )");

$sql_res = mysql_error();   

}//end for..

echo "<p><span style=\"color: red;\">Thank You; your records are sent to database. DO NOT REFRESH THE PAGE or data will be sent again.</span></p>"; 

}
?>

2 个答案:

答案 0 :(得分:1)

在登录过程中,您必须将用户名存储在会话中

$_SESSION['username'] = $username;

在保存表单的过程中,可以调用session_start();并使用。获取会话     $tobeinserted = $_SESSION['username'];

我相信

答案 1 :(得分:0)

在会话开始时删除评论。

使用此功能。

//This code is included to check session and store username
<?php
require_once('..\connect\auth.php');
session_start();
$usern = $_SESSION['SESS_FIRST_NAME'];
?>

<?php
mysql_connect('localhost','root','');
mysql_select_db('main_project') or die (mysql_error());

if(isset($_POST['WID'])){
for ($ix=0; $ix<count($_POST['WID']); $ix++)
{
 $WID = mysql_real_escape_string(@$_POST['WID'][$ix]);
 $website = mysql_real_escape_string(@$_POST['website'][$ix]);

//var_dump("<pre>", $_POST['cat']); die();   // Debugger for checking cat counter.
// $cat = implode(",", mysql_real_escape_string($_POST['cat'][$ix]));       

if(is_array(@$_POST['cat'][$ix]))
$cat = mysql_real_escape_string(implode(',', @$_POST['cat'][$ix]));
else
$cat = mysql_real_escape_string(@$_POST['cat'][$ix]);   

$email = mysql_real_escape_string(@$_POST['email'][$ix]);
$cform = mysql_real_escape_string(@$_POST['cform'][$ix]);
$contactp = mysql_real_escape_string(@$_POST['contactp'][$ix]);
$contacts = mysql_real_escape_string(@$_POST['contacts'][$ix]);
$fax = mysql_real_escape_string(@$_POST['fax'][$ix]);
$Ctype = mysql_real_escape_string(@$_POST['Ctype'][$ix]);
//$usern = mysql_real_escape_string(@$_POST['usern'][$ix]);

$sql_res = mysql_query("INSERT INTO website_01data (WID,website,cat,email,cform,contactp,contacts,fax,Ctype,TimeStamp,usern) 
VALUES ('".$WID."', '".$website."', '".$cat."', '".$email."','".$cform."', '".$contactp."', '".$contacts."', '".$fax."', '".$Ctype."', Now(), '".$usern."' )");

$sql_res = mysql_error();   

}//end for..

echo "<p><span style=\"color: red;\">Thank You; your records are sent to database. DO NOT REFRESH THE PAGE or data will be sent again.</span></p>"; 

}
?>