0

Hi All,

I have a page with a PHP insert form that posts relevant info to the SQL database. At the same time the key value "reg_id" is auto incremented by the DB.

How do I get new auto incremented value that has been posted so that I can use it to pass to the next page?

I have posted the code below if this helps

Many thanks

John

PS. I'm using Adobe DW CS4

<?php require_once('../Connections/venture.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;
}
}

$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 registration (First_Name, middle_name1, middle_name2, Last_Name, DOB, Gender, Ethnic_Background, Religion, `Language`, registration_date, prefered_name, Main_address, Main_post_code, Main_tel_no, Main_mobile_no, Main_work_no, Other_address, Other_post_code, Other_tel_no, Other_mobile_no, Other_work_no, surgery_Id) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['First_Name'], "text"),
                       GetSQLValueString($_POST['middle_name1'], "text"),
                       GetSQLValueString($_POST['middle_name2'], "text"),
                       GetSQLValueString($_POST['Last_Name'], "text"),
                       GetSQLValueString($_POST['DOB'], "date"),
                       GetSQLValueString($_POST['Gender'], "text"),
                       GetSQLValueString($_POST['Ethnic_Background'], "text"),
                       GetSQLValueString($_POST['Religion'], "text"),
                       GetSQLValueString($_POST['Language'], "text"),
                       GetSQLValueString($_POST['registration_date'], "date"),
                       GetSQLValueString($_POST['prefered_name'], "text"),
                       GetSQLValueString($_POST['Main_address'], "text"),
                       GetSQLValueString($_POST['Main_post_code'], "text"),
                       GetSQLValueString($_POST['Main_tel_no'], "text"),
                       GetSQLValueString($_POST['Main_mobile_no'], "text"),
                       GetSQLValueString($_POST['Main_work_no'], "text"),
                       GetSQLValueString($_POST['Other_address'], "text"),
                       GetSQLValueString($_POST['Other_post_code'], "text"),
                       GetSQLValueString($_POST['Other_tel_no'], "text"),
                       GetSQLValueString($_POST['Other_mobile_no'], "text"),
                       GetSQLValueString($_POST['Other_work_no'], "text"),
                       GetSQLValueString($_POST['surgery_Id'], "int"));

  mysql_select_db($database_venture, $venture);
  $Result1 = mysql_query($insertSQL, $venture) or die(mysql_error());

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

mysql_select_db($database_venture, $venture);
$query_Recordset1 = "SELECT * FROM registration";
$Recordset1 = mysql_query($query_Recordset1, $venture) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);

mysql_free_result($Recordset1);
?>

<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
  <table align="center">
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">First_Name:</td>
      <td><input type="text" name="First_Name" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Middle_name1:</td>
      <td><input type="text" name="middle_name1" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Middle_name2:</td>
      <td><input type="text" name="middle_name2" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Last_Name:</td>
      <td><input type="text" name="Last_Name" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">DOB:</td>
      <td><input type="text" name="DOB" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Gender:</td>
      <td><input type="text" name="Gender" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Ethnic_Background:</td>
      <td><input type="text" name="Ethnic_Background" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Religion:</td>
      <td><input type="text" name="Religion" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Language:</td>
      <td><input type="text" name="Language" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Registration_date:</td>
      <td><input type="text" name="registration_date" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Prefered_name:</td>
      <td><input type="text" name="prefered_name" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Main_address:</td>
      <td><input type="text" name="Main_address" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Main_post_code:</td>
      <td><input type="text" name="Main_post_code" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Main_tel_no:</td>
      <td><input type="text" name="Main_tel_no" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Main_mobile_no:</td>
      <td><input type="text" name="Main_mobile_no" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Main_work_no:</td>
      <td><input type="text" name="Main_work_no" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Other_address:</td>
      <td><input type="text" name="Other_address" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Other_post_code:</td>
      <td><input type="text" name="Other_post_code" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Other_tel_no:</td>
      <td><input type="text" name="Other_tel_no" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Other_mobile_no:</td>
      <td><input type="text" name="Other_mobile_no" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Other_work_no:</td>
      <td><input type="text" name="Other_work_no" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Surgery_Id:</td>
      <td><input type="text" name="surgery_Id" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">&nbsp;</td>
      <td><input type="submit" value="Insert record" /></td>
    </tr>
  </table>
  <input type="hidden" name="MM_insert" value="form1" />
</form>
<p>&nbsp;</p>
1
Contributor
1
Reply
2
Views
6 Years
Discussion Span
Last Post by johnbo100
0

Hi All,

I have a page with a PHP insert form that posts relevant info to the SQL database. At the same time the key value "reg_id" is auto incremented by the DB.

How do I get new auto incremented value that has been posted so that I can use it to pass to the next page?

I have posted the code below if this helps

Many thanks

John

PS. I'm using Adobe DW CS4

<?php require_once('../Connections/venture.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;
}
}

$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 registration (First_Name, middle_name1, middle_name2, Last_Name, DOB, Gender, Ethnic_Background, Religion, `Language`, registration_date, prefered_name, Main_address, Main_post_code, Main_tel_no, Main_mobile_no, Main_work_no, Other_address, Other_post_code, Other_tel_no, Other_mobile_no, Other_work_no, surgery_Id) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['First_Name'], "text"),
                       GetSQLValueString($_POST['middle_name1'], "text"),
                       GetSQLValueString($_POST['middle_name2'], "text"),
                       GetSQLValueString($_POST['Last_Name'], "text"),
                       GetSQLValueString($_POST['DOB'], "date"),
                       GetSQLValueString($_POST['Gender'], "text"),
                       GetSQLValueString($_POST['Ethnic_Background'], "text"),
                       GetSQLValueString($_POST['Religion'], "text"),
                       GetSQLValueString($_POST['Language'], "text"),
                       GetSQLValueString($_POST['registration_date'], "date"),
                       GetSQLValueString($_POST['prefered_name'], "text"),
                       GetSQLValueString($_POST['Main_address'], "text"),
                       GetSQLValueString($_POST['Main_post_code'], "text"),
                       GetSQLValueString($_POST['Main_tel_no'], "text"),
                       GetSQLValueString($_POST['Main_mobile_no'], "text"),
                       GetSQLValueString($_POST['Main_work_no'], "text"),
                       GetSQLValueString($_POST['Other_address'], "text"),
                       GetSQLValueString($_POST['Other_post_code'], "text"),
                       GetSQLValueString($_POST['Other_tel_no'], "text"),
                       GetSQLValueString($_POST['Other_mobile_no'], "text"),
                       GetSQLValueString($_POST['Other_work_no'], "text"),
                       GetSQLValueString($_POST['surgery_Id'], "int"));

  mysql_select_db($database_venture, $venture);
  $Result1 = mysql_query($insertSQL, $venture) or die(mysql_error());

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

mysql_select_db($database_venture, $venture);
$query_Recordset1 = "SELECT * FROM registration";
$Recordset1 = mysql_query($query_Recordset1, $venture) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);

mysql_free_result($Recordset1);
?>

<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
  <table align="center">
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">First_Name:</td>
      <td><input type="text" name="First_Name" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Middle_name1:</td>
      <td><input type="text" name="middle_name1" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Middle_name2:</td>
      <td><input type="text" name="middle_name2" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Last_Name:</td>
      <td><input type="text" name="Last_Name" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">DOB:</td>
      <td><input type="text" name="DOB" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Gender:</td>
      <td><input type="text" name="Gender" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Ethnic_Background:</td>
      <td><input type="text" name="Ethnic_Background" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Religion:</td>
      <td><input type="text" name="Religion" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Language:</td>
      <td><input type="text" name="Language" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Registration_date:</td>
      <td><input type="text" name="registration_date" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Prefered_name:</td>
      <td><input type="text" name="prefered_name" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Main_address:</td>
      <td><input type="text" name="Main_address" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Main_post_code:</td>
      <td><input type="text" name="Main_post_code" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Main_tel_no:</td>
      <td><input type="text" name="Main_tel_no" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Main_mobile_no:</td>
      <td><input type="text" name="Main_mobile_no" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Main_work_no:</td>
      <td><input type="text" name="Main_work_no" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Other_address:</td>
      <td><input type="text" name="Other_address" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Other_post_code:</td>
      <td><input type="text" name="Other_post_code" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Other_tel_no:</td>
      <td><input type="text" name="Other_tel_no" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Other_mobile_no:</td>
      <td><input type="text" name="Other_mobile_no" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Other_work_no:</td>
      <td><input type="text" name="Other_work_no" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Surgery_Id:</td>
      <td><input type="text" name="surgery_Id" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">&nbsp;</td>
      <td><input type="submit" value="Insert record" /></td>
    </tr>
  </table>
  <input type="hidden" name="MM_insert" value="form1" />
</form>
<p>&nbsp;</p>

It's OK I've solved it:

Use - mysql_insert_id(); then pass it on with a session.
Note : make sure you use start_session in each form and use mysql_insert_id() just after the post and before displaying your next page.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.