将$ _SESSION ['MM_Username']插入日志消息中

时间:2012-05-20 11:37:32

标签: php mysql session insert

这里的第一篇文章 - 我已经学习了一个月左右的php和mysql。

我正在尝试将会话“MM_Username”发布到日志备注中。 所以它显示'由用户名存档'

我认为如果我将lognote值设置为'archived by'。$ _SESSION ['MM_Username']。“' 我会得到我追求的结果。显然不是。

我做错了什么? 我的代码如下。

由于

    if ((isset($_GET['divisionid'])) && ($_GET['divisionid'] != "")) {


  $logarchiveSQL = sprintf("INSERT INTO log (logaction, logdivisionid, lognotes) VALUES ('Division Archived', %s, 'archived by " . $_SESSION['MM_Username'] . "')",
                       GetSQLValueString($_GET['divisionid'], "int"));

  mysql_select_db($database_connect, $connect);
  $Result2 = mysql_query($logarchiveSQL, $connect) or die(mysql_error());


  $updateGoTo = "../divisions.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    $updateGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $updateGoTo));}

4 个答案:

答案 0 :(得分:1)

你记得把它放到session_start();在尝试访问会话之前?

答案 1 :(得分:0)

你得到了什么结果?查询是否实际执行? 当语句用双引号“”包装时,请尝试引用{}

中的变量
   $logarchiveSQL = sprintf("INSERT INTO log (logaction, logdivisionid, lognotes) VALUES ('Division Archived', %s, 'archived by {$_SESSION[MM_Username]} ')",
                       GetSQLValueString($_GET['divisionid'], "int"));

答案 2 :(得分:0)

由于您没有提供错误,我检查了代码,并没有找到任何严重的编码方式。但是,一些小建议如下:

<?Php
if ((isset($_GET['divisionid'])) && ($_GET['divisionid'] != "")) {


    $logarchiveSQL = sprintf("INSERT INTO log (logaction, logdivisionid, lognotes) VALUES ('Division Archived', %d, 'archived by %s')",
        GetSQLValueString($_GET['divisionid'], "int"), 
        $_SESSION['MM_Username']);

    mysql_select_db($database_connect, $connect);
    $Result2 = mysql_query($logarchiveSQL, $connect) or die(mysql_error());


    $updateGoTo = "../divisions.php";
    if (isset($_SERVER['QUERY_STRING'])) {
        $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
        $updateGoTo .= $_SERVER['QUERY_STRING'];
    }
    header(sprintf("Location: %s", $updateGoTo));
    exit;
}
?>
  • 您应该分别使用%d或%f表示INT和FLOAT值。
  • 由于您使用的是sprintf,为什么不使用另一个%s来放置SESSION变量?
  • 您应该在每次header()调用后放置一个EXIT以确保无效 传输程序逻辑后,将执行此页面中的更多操作 到另一页。
  • PHP区分大小写,因此建议您检查案例 SESSION变量与此处使用的变量。

最终会欣赏您所面临的错误!

答案 3 :(得分:0)

解决!

感谢大家的帮助!

我没有声明session_start,我忘记了它是在我的安全包含文件中定义的。

我还必须创建一个部门记录集

    session_start();

$colname_division = "-1";
if ((isset($_GET['divisionid'])) && ($_GET['divisionid'] != "")) {
      $colname_division = $_GET['divisionid'];

mysql_select_db($database_connect, $connect);
$query_division = sprintf("SELECT * FROM division WHERE divisionid = %s", GetSQLValueString($colname_division, "int"));
$division = mysql_query($query_division, $connect) or die(mysql_error());
$row_division = mysql_fetch_assoc($division);
$totalRows_division = mysql_num_rows($division);


// ADD LOG DIVISION REINSTATED
    $logarchiveSQL = sprintf("INSERT INTO log (logaction, logdivisionid, lognotes) VALUES ('Reinstated', %d, '" . $row_division['division'] . " reinstated by " . $_SESSION['MM_Username'] . "')",
                    GetSQLValueString($_GET['divisionid'], "int")); 

    mysql_select_db($database_connect, $connect);
    $Result1 = mysql_query($logarchiveSQL, $connect) or die(mysql_error());

// MESSAGE USERS DIVISION REINSTATED
    $messageinsertSQL = sprintf("INSERT INTO messages (message) VALUES ('The division " . $row_division['division'] . " has been reinstated by " . $_SESSION['MM_Username'] . " you may now select this division')",
                    GetSQLValueString($_GET['divisionid'], "int")); 

    mysql_select_db($database_connect, $connect);
    $Result4 = mysql_query($messageinsertSQL, $connect) or die(mysql_error());

// UPDATE DIVISION TO REINSTATED
    $divisionarchiveSQL = sprintf("UPDATE division SET divisionarchive=0 WHERE divisionid=%s",
                    GetSQLValueString($_GET['divisionid'], "int"));

    mysql_select_db($database_connect, $connect);
    $Result3 = mysql_query($divisionarchiveSQL, $connect) or die(mysql_error());

// REDIRECT
    $updateGoTo = "../divisions.php";
        if (isset($_SERVER['QUERY_STRING'])) {
            $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
            $updateGoTo .= $_SERVER['QUERY_STRING'];
        }
    header(sprintf("Location: %s", $updateGoTo));

    mysql_free_result($division);
    exit;