Passing an ID from page to page

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 (&#37;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.

You could simply use a session and unset it once the page is done with it

<?php
session_start();

$_SESSION['package_ID'] = mysql_insert_id();

then on the next script simply do the same thing only call it

<?php
session_start();

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

unset($_SESSION['package_ID']);

Thank you - all working now. :slight_smile: