Skip to main content
June 12, 2009
Answered

Use PHP Variables between pages: Session, URL, or Get

  • June 12, 2009
  • 1 reply
  • 4217 views

Hi,

For our association, I have developed a registration page.  The registration pages insert a record into a mysql database, using php.  After the user submits the form (using $post), I would like to be redirected to the confirmation page where a query of the mysql database is executed to retrieve the record the member just added.

To pass the variable, I have developed the following pages using a session variable.

Registration page :   http://www.aosweb.org/member_dues.php

Confirmation page:  http://www.aosweb.org/member_view.php

Although I have applied some recent changes to the code, it did work at one time.  The forum was helpful getting the information I needed.  At this point, the redirect and passing of the session variable is not working.  It appears the session variable available on the member_view page is from the previous record inserted.  Thus, the query executed on member_view does not return a result.

I have checked some of forum posts, such as:

- php url parameters

- how to pass form parameters to the confirm page ....

- php registration page help!

My outstanding questions are as follows:

1. Have I placed the command to set the $_session('sv_mem_num') at the correct location.

2. Should I consider another method such as passing a URL parameter or using the Get method.

Thanks.

George

This topic has been closed for replies.
Correct answer David_Powers

Hi,

Thanks for the input.  I changed the code the way you suggested and it returns a '&' at the end of the url parameter

http://aosweb.org/member_confirm.php?memid=99998&

I think this is causing the query on the target page not to return results.  Here is code on the source page.  What do I need to change?

$insertGoTo = "member_confirm.php?memid=" .$_POST['Member_Num']."";

if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}


$insertGoTo = "member_confirm.php?memid=" .$_POST['Member_Num']."";

if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}

What's happening is that the Dreamweaver code is looking for an existing query string, and adding it. The problem is that you're putting a query string in your redirect page, which results in the ampersand being added at the end. This is how I would change it:

// set the redirect to the target page without a query string

// then let Dreamweaver check for an existing query string

$insertGoTo = "member_confirm.php;

if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}

// if a query string has been added, add the memid at the end

// otherwise, create a query string to add memid

if (strpos($insertGoTo, '?')) {

$insertGoTo .= '&memid=' . $_POST['Member_Num'];

} else {

$insertGoTo .= '?memid=' . $_POST['Member_Num'];

}

1 reply

David_Powers
Inspiring
June 12, 2009

Have I placed the command to set the $_session('sv_mem_num') at the correct location.

Impossible to say. PHP is a server-side language, so giving links to the pages you have created tells us nothing. All that can be seen is the HTML output, not the PHP code.

However, part of your problem could be the incorrect use of code. It might just be the way you typed things in here, but $post is simply the name of a variable you have created yourself. I presume you are referring to $_POST. The underscore and capital letters are vital. Similarly, you refer to $_session('sv_mem_num'). This is meaningless. It should be $_SESSION['sv_mem_num']. Note the uppercase spelling of SESSION, and the use of square brackets instead of parentheses.

June 12, 2009

David,

Here are selected section of the code.  I have the SESSION AND $_POSTING coding correct.  From a php / html coding perspective, is the code in red in the corret position?  Should it be below the code in blue?  Or, doesn't it matter?

Thanks.

<?php
SESSION_START();
  if (isset($_POST['Member_Num'])) {
$_SESSION['SV_Mem_Num'] = $_POST['Member_Num'];}
?>

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO Member (Member_Num, Active, Created_BY, RCRD_DTTS, LAST_UPDATED_BY, UPDATE_DTTS, FIRST_NAME, LAST_NAME, ADDRESS_1, ADDRESS_2, CITY, `STATE`, ZIP_CODE, SPORT, FB_Level, BB_Level, email, email_Secondary, Member_Since, HOME_PHONE, CELL_PHONE, WORK_PHONE, Fax) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['Member_Num'], "text"),
                       GetSQLValueString($_POST['Active'], "text"),
                       GetSQLValueString($_POST['Created_BY'], "text"),
                       GetSQLValueString($_POST['RCRD_DTTS'], "date"),
                       GetSQLValueString($_POST['LAST_UPDATED_BY'], "text"),
                       GetSQLValueString($_POST['UPDATE_DTTS'], "date"),
                       GetSQLValueString($_POST['FIRST_NAME'], "text"),
                       GetSQLValueString($_POST['LAST_NAME'], "text"),
                       GetSQLValueString($_POST['ADDRESS_1'], "text"),
                       GetSQLValueString($_POST['ADDRESS_2'], "text"),
                       GetSQLValueString($_POST['CITY'], "text"),
                       GetSQLValueString($_POST['STATE'], "text"),
                       GetSQLValueString($_POST['ZIP_CODE'], "text"),
                       GetSQLValueString($_POST['SPORT'], "text"),
                       GetSQLValueString($_POST['FB_Level'], "text"),
                       GetSQLValueString($_POST['BB_Level'], "text"),
                       GetSQLValueString($_POST['email'], "text"),
                       GetSQLValueString($_POST['email_Secondary'], "text"),
                       GetSQLValueString($_POST['Member_Since'], "date"),
                       GetSQLValueString($_POST['HOME_PHONE'], "text"),
                       GetSQLValueString($_POST['CELL_PHONE'], "text"),
                       GetSQLValueString($_POST['WORK_PHONE'], "text"),
                       GetSQLValueString($_POST['Fax'], "text"));

  mysql_select_db($database_aosuser, $aosuser);
  $Result1 = mysql_query($insertSQL, $aosuser) or die(mysql_error());

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

mysql_select_db($database_aosuser, $aosuser);
$query_rs_members = "SELECT * FROM Member";
$rs_members = mysql_query($query_rs_members, $aosuser) or die(mysql_error());
$row_rs_members = mysql_fetch_assoc($rs_members);
$totalRows_rs_members = mysql_num_rows($rs_members);
?>

David_Powers
Inspiring
June 13, 2009

It's in the correct place.