Hi, I hope someone on this board can help me, I'm completely stuck on a problem I have with the mysql_insert_id(); command....
Basically I'm using dreamweaver to code my site (sorry - I know!) and I'm trying to set up registration pages for new users. My database currently only has 2 tables: 'members' (mem_ID(auto) email, pwords, address..) and 'preferences' (register their interests). I have a primary key field which auto-increments in the members table (mem_ID) and use this as a foreign key in the preferences table. I have two pages for the registration. The first page is register.php and this has a form which takes the members login name(which is email ad), name, address etc and enters it into the members table (this works fine). You are then taken to register_pref.php to complete a form and this data is inserted into the preferences table.
My problems: I'm trying to get the new mem_ID using mysql_insert_id(); and set it as a session variable in register.php, I want to call on this variable in register_pref.php and insert this into the preferences table as the foreign key. All data is going into the tables correctly apart from this mem_Id which is always entered as 0. I've been trying to figure this out myself for days, trying different syntax but am completely stuck. Any help you could give me would be much appreciated as I'm going insane.....here goes....
REGISTER.PHP
<?php require_once('Connections/gpeConn.php'); ?>
<?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;
}
$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 member (mem_ID, email, pword, firstname, surname, address1, address2, city, postcode, phone, mobile, date_joined) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
GetSQLValueString($POST['mem_ID'], "int"),
GetSQLValueString($POST['email'], "text"),
GetSQLValueString($POST['pword'], "text"),
GetSQLValueString($POST['firstname'], "text"),
GetSQLValueString($POST['surname'], "text"),
GetSQLValueString($POST['address1'], "text"),
GetSQLValueString($POST['address2'], "text"),
GetSQLValueString($POST['city'], "text"),
GetSQLValueString($POST['postcode'], "text"),
GetSQLValueString($POST['phone'], "text"),
GetSQLValueString($POST['mobile'], "text"),
GetSQLValueString($POST['date_joined'], "date"));
mysql_select_db($database_gpeConn, $gpeConn);
$Result1 = mysql_query($insertSQL, $gpeConn) or die(mysql_error());
$insertGoTo = "register_pref.php";
if (isset($SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
?>
<?php require_once('Connections/gpeConn.php'); ?>
<?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;
}
$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 member (email, pword, firstname, surname, address1, address2, city, postcode, phone, mobile, date_joined) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
GetSQLValueString($POST['email'], "text"),
GetSQLValueString($POST['pword'], "text"),
GetSQLValueString($POST['firstname'], "text"),
GetSQLValueString($POST['surname'], "text"),
GetSQLValueString($POST['address1'], "text"),
GetSQLValueString($POST['address2'], "text"),
GetSQLValueString($POST['city'], "text"),
GetSQLValueString($POST['postcode'], "text"),
GetSQLValueString($POST['phone'], "text"),
GetSQLValueString($POST['mobile'], "text"),
GetSQLValueString($_POST['date_joined'], "date"));
mysql_select_db($database_gpeConn, $gpeConn);
$Result1 = mysql_query($insertSQL, $gpeConn) or die(mysql_error());
//my try here
$getID = mysql_insert_id();
$GLOBALS['MM_MemberID'] = $getID;
session_register("MM_MemberID");
$insertGoTo = "register_pref.php";
if (isset($SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
?>
REGISTER_PREF.php
<?php require_once('Connections/gpeConn.php');
session_start();
?>
<?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;
}
$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 preference (mem_ID, ind_plot, mult_plot, selfbuild, cap_invest, barn_conv, rundown, smallhold, min_land, min_width, locations, bedrooms, garage, f_garden, r_garden, sell_profit, build_live, built_before, builder, tradesman, build_abroad, languages, experience) 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['mem_ID'], "int"),
GetSQLValueString($POST['ind_plot'], "text"),
GetSQLValueString($POST['mult_plot'], "text"),
GetSQLValueString($POST['selfbuild'], "text"),
GetSQLValueString($POST['cap_invest'], "text"),
GetSQLValueString($POST['barn_conv'], "text"),
GetSQLValueString($POST['rundown'], "text"),
GetSQLValueString($POST['smallhold'], "text"),
GetSQLValueString($POST['min_land'], "int"),
GetSQLValueString($POST['min_width'], "int"),
GetSQLValueString($POST['locations'], "text"),
GetSQLValueString($POST['bedrooms'], "int"),
GetSQLValueString($POST['garage'], "text"),
GetSQLValueString($POST['f_garden'], "text"),
GetSQLValueString($POST['r_garden'], "text"),
GetSQLValueString($POST['sell_profit'], "text"),
GetSQLValueString($POST['build_live'], "text"),
GetSQLValueString($POST['built_before'], "text"),
GetSQLValueString($POST['builder'], "text"),
GetSQLValueString($POST['tradesman'], "text"),
GetSQLValueString($POST['build_abroad'], "text"),
GetSQLValueString($POST['languages'], "text"),
GetSQLValueString($_POST['experience'], "text"));
mysql_select_db($database_gpeConn, $gpeConn);
$Result1 = mysql_query($insertSQL, $gpeConn) or die(mysql_error());
$insertGoTo = "register_thanks.php";
if (isset($SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
?>