Can't get MM_Username after Check New Username

Agent909
Agent909 used Ask the Experts™
on
I have a page to create a new user with two fields, user_name and password.  I have two server behaviors on the page, Insert Record and Check New Username.  It checks the username correctly and adds a new record in my Customers table, saving the user_name and password.

On the next page, the user fills in name and address etc.  The problem is that I can't get access to MM_Username on this page, where I have a Recordset to populate the user_id, user_name and password fields.  

The code I have on the first page, where the new user enters their information is:

<?php
if (!isset($_SESSION)) {
    session_start();
}    
?>

The code I have on the second page is:

<?php
//initialize the session
if (!isset($_SESSION)) {
  session_start();
  $username = $_SESSION['MM_Username'];
}
?>

I get an error on the second page:  Undefined index: MM_Username

What am I doing wrong?

Thanks.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Jason C. LevineDon't talk to me.

Commented:
Hi again, Agent909

Show the PHP code from the first page so I can see how the username gets set on registration.  

Author

Commented:
Here's the PHP code from the first page.
<?php
if (!isset($_SESSION)) {
	session_start();
}    
?>

<?php require_once('Connections/connTT.php'); ?>

<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

// *** Redirect if username exists
$MM_flag="MM_insert";
if (isset($_POST[$MM_flag])) {
  $MM_dupKeyRedirect="step-1b.php";
  $loginUsername = $_POST['user_name'];
  $LoginRS__query = sprintf("SELECT user_name FROM customer WHERE user_name=%s", GetSQLValueString($loginUsername, "text"));
  mysql_select_db($database_connTT, $connTT);
  $LoginRS=mysql_query($LoginRS__query, $connTT) or die(mysql_error());
  $loginFoundUser = mysql_num_rows($LoginRS);

  //if there is a row in the database, the username was found - can not add the requested username
  if($loginFoundUser){
    $MM_qsChar = "?";
    //append the username to the redirect page
    if (substr_count($MM_dupKeyRedirect,"?") >=1) $MM_qsChar = "&";
    $MM_dupKeyRedirect = $MM_dupKeyRedirect . $MM_qsChar ."requsername=".$loginUsername;
    header ("Location: $MM_dupKeyRedirect");
    exit;
  }
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO customer (user_name, user_password) VALUES (%s, %s)",
                       GetSQLValueString($_POST['user_name'], "text"),
                       GetSQLValueString($_POST['user_password'], "text"));

  mysql_select_db($database_connTT, $connTT);
  $Result1 = mysql_query($insertSQL, $connTT) or die(mysql_error());
}



  $insertGoTo = "step-1.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));

?>

Open in new window

Don't talk to me.
Commented:
So reason you are getting the error message is that $_SESSION['MM_Username'] is never set in the registration code above.  It checks for a dupe and if no dupe is found it does an Insert and goes to the next page.

Just add the following at line 44:

$_SESSION['MM_Username'] = $_POST['user_name'];

Just declaring that sessions exist doesn't do anything to get the data into the sessions.  You have to assign variables. Don't trust that Dreamweaver will do the right thing.

Author

Commented:
That seems to have fixed it!  Thank you, once again!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial