Jump to content

Passing an ID from page to page


BarneyJoe

Recommended Posts

Hope someone can help with this....

 

I have a database structure along the lines of :

 

1. A main table of holiday packages.

 

2. A table of destinations that form a many to many link with the Packages table.

 

3. A table of feature types (eg museums, theme parks) that form another many to many link with the Packages table.

 

First I have a page addPackage.php. This goes onto addPackageDestinations.php, which inserts the data entered in the addPackage page, and grabs the package_ID of that new record :

 

<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $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;
}

/********************
*
*insert Package details into db
*
********************/

//sql string

$insertSQL = sprintf("INSERT INTO packages (package, costperpax, duration, baselocation, category, agerange, hotel, educational_tours, field_trips, corporate_outings, plant_visits, budget_package, themepark, museum, rollingtours, seminars, historicsite, teambuilding, zoo, park, church, naturalbeauty, relaxation, beach, city, wildlife, international, mallshopping, description) VALUES (%s, %s, %s, %s, %s, %s, %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['package'], "text"),
GetSQLValueString($_POST['costperpax'], "text"),
GetSQLValueString($_POST['duration'], "date"),
GetSQLValueString($_POST['baselocation'], "text"),
GetSQLValueString($_POST['category'], "text"),
GetSQLValueString($_POST['agerange'], "text"),
GetSQLValueString($_POST['hotel'], "text"),
GetSQLValueString($_POST['educational_tours'], "text"),
GetSQLValueString($_POST['field_trips'], "text"),
GetSQLValueString($_POST['corporate_outings'], "text"),
GetSQLValueString($_POST['plant_visits'], "text"),
GetSQLValueString($_POST['budget_package'], "text"),
GetSQLValueString($_POST['themepark'], "text"),
GetSQLValueString($_POST['museum'], "date"),
GetSQLValueString($_POST['rollingtours'], "text"),
GetSQLValueString($_POST['seminars'], "text"),
GetSQLValueString($_POST['historicsite'], "text"),
GetSQLValueString($_POST['teambuilding'], "text"),
GetSQLValueString($_POST['zoo'], "text"),
GetSQLValueString($_POST['park'], "text"),
GetSQLValueString($_POST['church'], "text"),
GetSQLValueString($_POST['naturalbeauty'], "text"),
GetSQLValueString($_POST['relaxation'], "text"),
GetSQLValueString($_POST['beach'], "text"),
GetSQLValueString($_POST['city'], "text"),
GetSQLValueString($_POST['wildlife'], "text"),
GetSQLValueString($_POST['international'], "text"),
GetSQLValueString($_POST['mallshopping'], "text"),
GetSQLValueString($_POST['description'], "text"));

//insert

$Result1 = mysql_select_db($database_connPackages, $connPackages); mysql_query($insertSQL); echo mysql_error();

/********************
*
*Retrieve package_ID
*
********************/

$package_ID = mysql_insert_id ();

?>

 

So far, so good.

 

I then go onto an addPackageFeatures.php page, which inserts the package destinations like so :

 

<?php

/********************
*
*insert Package Destinations into PackageDestinations table table
*
********************/

//sql insert string

$sql = "INSERT INTO packagedestinations (packageID, destinationID) VALUES ";

$ck = $_GET['ckbox'];

//loops though the profiles adding the the insert string each profile row

foreach ($ck As $GetIndex => $GetValue){
if ($GetValue=='on'){
$sql .= "('{$_GET['package_ID']}', '$GetIndex'), ";
}
}

//trims the end , 

$sql = rtrim($sql," ,") ;

//inserts data

mysql_select_db($database_connPackages, $connPackages); mysql_query($sql); echo mysql_error();
?>

 

So all I need on this page is the same package_ID for the new package being created, to be passed from the addPackageDestinations page onto the addPackageFeatures page.

 

Not sure how well I've explained it but hopefully it makes sense...?

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/209595-passing-an-id-from-page-to-page/
Share on other sites

OK, so I've added that to the addDestinations page, so its :

 

<code>

/********************

*

*Retrieve package_ID

*

********************/

 

$package_ID = mysql_insert_id ();

 

/********************

*

*Store package_ID in session

*

********************/

 

$_SESSION['package_ID'] = $package_ID;

</code>

 

What's the syntax to pick it up in the next page?

 

Something like :

 

$_GET['package_ID'];

 

or 

 

$_GET[session=package_ID];

 

?

 

 

What's the syntax to pick it up in the next page?

 

Something like :

 

$_GET['package_ID'];

 

or 

 

$_GET[session=package_ID];

 

?

You use the same session variable, which is $_SESSION['package_ID']

 

In order for this to work you need to have session_start at the top of your scripts.

Thank you - that's all pretty much working now, the session variable is echoing on each page so I know is all OK.

 

The only thing that isn't working, is getting the package ID inserted into my packagefeatures interlinking table.

 

At the moment I have this code that works as it should to display the features along with checkboxes as an array :

 

<code>

<p>Package ID : <?php echo $_SESSION['package_ID'];?></p>

   

 

   

    <table width="100%" border="0" cellpadding="0" cellspacing="0" align="center">

<form name="form1" id="form1" method="GET" action="package_added3.php">

<input type='hidden' name='package_ID' value='<?php echo $package_ID; ?>'>

 

<?php

require_once('../Connections/connPackages.php');

error_reporting(E_ALL & E_STRICT);

$package_ID = 1;

 

if (isset($_GET['package_ID'])) {

    $package_ID = intval($_GET['package_ID']);

}

 

//Get destinations & put into array

mysql_select_db($database_connPackages, $connPackages);

$query_Keyword_Match = sprintf("SELECT * FROM packagefeatures WHERE packageID = %s", $package_ID);

$Keyword_Match = mysql_query($query_Keyword_Match, $connPackages) or die(mysql_error());

$packagefeatures = array();

while ($row_Keyword_Match = mysql_fetch_assoc($Keyword_Match)) {

    $packagefeatures[] = $row_Keyword_Match['feature_ID'];

}

 

//Get all the keywords

$sql = "SELECT * FROM features ORDER BY feature";

$query = mysql_query($sql);

 

$current_category = "";

$row_type = "";

$column = 1;

 

while ($keyword=mysql_fetch_assoc($query)) {

 

    //If new category close previous row & display new category

    if ($keyword['Category']!=$current_category) {

        if ($current_category && $column !=1 ) { echo "</tr>\n"; }

        $current_category = $keyword['Category'];

        echo "<tr class=\"categorycell\"><td colspan=\"10\">$current_category</td></tr>\n";

        $column = 1; $row_type="";

    }

 

//Create new row if 1st keyword

    if ($column == 1) {

        $row_type = ($row_type=="odd")?"even":"odd";

        echo "<tr class=\"".$row_type."\">";

    }

 

    //Display the checkbox

    echo "<td width=\"2%\">";

    echo "<input type=\"checkbox\" class=\"tickbox_".$row_type."\"";

    if (in_array($keyword['feature_ID'],$photokeywords)) { echo " checked"; }

    echo " name=\"ckbox[".$keyword['feature_ID']."]\" id=\"ckbox[".$keyword['feature_ID']."]\">";

    echo "</td>\n";

 

    //Display the Keyword

    echo "<td width=\"18%\" align=\"left\">".$keyword['feature']."</td>\n";

 

    //Close the row if 5th keyword OR increase column count

    if ($column == 4) { echo "</tr>"; $column = 1; }

    else { $column++; }

}

 

if ($column != 1) { echo "</tr>"; }

?>

 

<tr>

<td> </td>

</tr>

  <tr>

    <td colspan="2">  

  <tr>

  <td colspan="2">

  <input type="submit" name="Submit" value="FINISH">

  <INPUT type=button value="Cancel">

  </form></td>

 

</tr>

</table>

</code>

 

And on the next page, using this to insert the two IDs :

 

<code>

/********************

*

*insert Package Features into PackageFeatures table table

*

********************/

 

//sql insert string

 

$sql = "INSERT INTO packagefeatures (packageID, featureID) VALUES ";

 

$ck = $_GET['ckbox'];

 

//loops though the profiles adding the the insert string each profile row

 

foreach ($ck As $GetIndex => $GetValue){

if ($GetValue=='on'){

$sql .= "('{$_GET['package_ID']}', '$GetIndex'), ";

}

}

 

//trims the end ,

 

$sql = rtrim($sql," ,") ;

 

//inserts data

 

mysql_select_db($database_connPackages, $connPackages); mysql_query($sql); echo mysql_error();

</code>

 

But its only inserting the featureID, and 0's for each packageID....

TIP: When passing data from page to page or handling data from a form try and sanitise it on reading.

 

For example, when you pick up your package ID on the next form try this:

$packageID=intval($_SESSION['package_id']);

Then you can check it and if it's a 0 (zero) then something has gone wrong somewhere. If you're waiting for data to be filled in and you've got session variables set be aware that session variables expire. If they take too long filling in a form the package ID will be lost so you'll have to check for this - using the above intval() or even isset() can work for this.

 

An even better way is:

$packageID=(isset($_SESSION['package_id']) ? intval($_SESSION['package_id']) : null);

That way if the session variable exists we get the value and make sure it remains an integer. If it isn't set we set $packageID to null. You can then check...

if ($packageID===null) {
  //THE SESSION VARIABLE HAS EXPIRED
} else {
  //HANDLE THE DATA
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.