简体   繁体   English

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

[英]storing username from session to database while form submission using php

I have a simple question, 我有一个简单的问题,

I have a login and workspace area. 我有一个登录和工作区。

After the user logs in It shows the username of the logged in user at workplace as what I wanted. 用户登录后,它将显示我想要的工作场所登录用户的用户名。 Now my problem is when user finish filling form available in his workspace the form is then stored in database also i need the username that is coming from session also get stored to the database. 现在我的问题是,当用户填写完工作区中可用的表单后,该表单将存储在数据库中,我还需要将来自会话的用户名也存储到数据库中。

here is code that is storing username and maintaining session after user reach at workspace after login: 这是存储用户名并在用户登录后到达工作区后维护会话的代码:

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

The final version of the updated insert file : 更新后的插入文件的最终版本:

 //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>"; 

}
?>

In the logging in process, you must store your username in a session 在登录过程中,您必须将用户名存储在会话中

$_SESSION['username'] = $username;

in the process of saving the form, you can call session_start(); 在保存表单的过程中,可以调用session_start(); and get the session using $tobeinserted = $_SESSION['username']; 并使用$tobeinserted = $_SESSION['username'];获得会话$tobeinserted = $_SESSION['username'];

I believe 我相信

Remove comment in session start. 在会话开始时删除评论。

Use this. 用这个。

//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>"; 

}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM