By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,635 Members | 2,188 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,635 IT Pros & Developers. It's quick & easy.

Auto-increment field not letting me insert multiple records at once

chumlyumly
P: 9
Hello scripters -

OS: Mac OSX
Language: PHP w/ MySQL database

I've created an insert page where a user inputs his info, which then goes to four different tables in a MySQL database. The tables are all linked with the field 'member_id', which is an auto-increment field in the parent table ('members').

I've been able to input multiple records into the other three tables 'specialty_groups', 'committee_interest' and 'committee_member' until recently.

After creating this insert page, I had to add an auto-increment key field to the 'specialty_groups' table so that users could go in and delete or make updates to their specialty choices (in the update pages).

The trouble is, now when I go to insert specialty records into the 'specialty_group' table, I can only insert one record. The 'committee_interest' and 'committee_member' tables are still inputting multiple records just fine.

So, I figured it was because I added the auto-increment key field to the 'specialty_group' table. But the confusing thing is that when a user is updating his info, he/she can add as many 'specialty_group' fields as he/she wants - on the update page. (However, on that page, the 'member_id' field is taken from a session variable 'username' from the 'member' table).

My code for the insert page is below:
Expand|Select|Wrap|Line Numbers
  1. <?php require_once('../Connections/connection.php'); ?>
  2. <?php
  3. function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
  4. {
  5.   $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;
  6.  
  7.   switch ($theType) {
  8.     case "text":
  9.       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  10.       break;    
  11.     case "long":
  12.     case "int":
  13.       $theValue = ($theValue != "") ? intval($theValue) : "NULL";
  14.       break;
  15.     case "double":
  16.       $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
  17.       break;
  18.     case "date":
  19.       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  20.       break;
  21.     case "defined":
  22.       $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  23.       break;
  24.   }
  25.   return $theValue;
  26. }
  27.  
  28. //code here for inserting fields into the 'member' table.
  29.  
  30. $editFormAction = $_SERVER['PHP_SELF'];
  31. if (isset($_SERVER['QUERY_STRING'])) {
  32.   $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
  33.  
  34. }
  35.  
  36. // loop through array of commmbr[] and write each instance to db
  37. @reset($commmbr);
  38. mysql_select_db($database_connection, $connection);
  39. if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "memberinfo"));
  40. while (list($key, $val) = @each ($commmbr)) {
  41.   $insertSQL = sprintf("INSERT INTO committee_member (commmbr, member_id) VALUES (%s,LAST_INSERT_ID())",
  42.                        GetSQLValueString($val, "text"),
  43.                        GetSQLValueString($_REQUEST['member_id'], "int"));
  44.  
  45.   mysql_select_db($database_connection, $connection);
  46.   $Result1 = mysql_query($insertSQL, $connection) or die(mysql_error());
  47. }
  48.  
  49. $editFormAction = $_SERVER['PHP_SELF'];
  50. if (isset($_SERVER['QUERY_STRING'])) {
  51.   $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
  52.  
  53. }
  54.  
  55. @reset($commint);
  56. mysql_select_db($database_connection, $connection);
  57. if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "memberinfo"));
  58. while (list($key, $val) = @each ($commint)) {
  59.   $insertSQL = sprintf("INSERT INTO committee_interest (commint, member_id) VALUES (%s,LAST_INSERT_ID())",
  60.                        GetSQLValueString($val, "text"),
  61.                        GetSQLValueString($_REQUEST['member_id'], "int"));
  62.  
  63.   mysql_select_db($database_connection, $connection);
  64.   $Result1 = mysql_query($insertSQL, $connection) or die(mysql_error());
  65. }
  66. $editFormAction = $_SERVER['PHP_SELF'];
  67. if (isset($_SERVER['QUERY_STRING'])) {
  68.   $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
  69.  
  70. }
  71. // loop through array of specialty[] and write each instance to db
  72. @reset($specialty);
  73. mysql_select_db($database_connection, $connection);
  74. if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "memberinfo"));
  75. while (list($key, $val) = @each ($specialty)) {
  76.   $insertSQL = sprintf("INSERT INTO specialty_group (specialty, member_id) VALUES (%s,LAST_INSERT_ID())",
  77.                        GetSQLValueString($val, "text"),
  78.                        GetSQLValueString($_REQUEST['member_id'], "int"));
  79.  
  80.   mysql_select_db($database_connection, $connection);
  81.   $Result1 = mysql_query($insertSQL, $connection) or die(mysql_error());
  82. }
  83.  
  84. $colname_rs_member = "-1";
  85. if (isset($_GET['recordID'])) {
  86.   $colname_rs_member = (get_magic_quotes_gpc()) ? $_GET['recordID'] : addslashes($_GET['recordID']);
  87. }
  88. mysql_select_db($database_connection, $connection);
  89. $query_rs_member = sprintf("SELECT * FROM member WHERE member_id = %s", $colname_rs_member);
  90. $rs_member = mysql_query($query_rs_member, $connection) or die(mysql_error());
  91. $row_rs_member = mysql_fetch_assoc($rs_member);
  92. $totalRows_rs_member = mysql_num_rows($rs_member);
  93.  
  94. $colname_rscommitteemember = "-1";
  95. if (isset($_GET['recordID'])) {
  96.   $colname_rscommitteemember = (get_magic_quotes_gpc()) ? $_GET['recordID'] : addslashes($_GET['recordID']);
  97. }
  98. mysql_select_db($database_connection, $connection);
  99. $query_rscommitteemember = sprintf("SELECT * FROM committee_member WHERE member_id = %s", $colname_rscommitteemember);
  100. $rscommitteemember = mysql_query($query_rscommitteemember, $connection) or die(mysql_error());
  101. $row_rscommitteemember = mysql_fetch_assoc($rscommitteemember);
  102. $totalRows_rscommitteemember = mysql_num_rows($rscommitteemember);
  103.  
  104. $colname_rscommitteeinterest = "-1";
  105. if (isset($_GET['recordID'])) {
  106.   $colname_rscommitteeinterest = (get_magic_quotes_gpc()) ? $_GET['recordID'] : addslashes($_GET['recordID']);
  107. }
  108. mysql_select_db($database_connection, $connection);
  109. $query_rscommitteeinterest = sprintf("SELECT * FROM committee_interest WHERE member_id = %s", $colname_rscommitteeinterest);
  110. $rscommitteeinterest = mysql_query($query_rscommitteeinterest, $connection) or die(mysql_error());
  111. $row_rscommitteeinterest = mysql_fetch_assoc($rscommitteeinterest);
  112. $totalRows_rscommitteeinterest = mysql_num_rows($rscommitteeinterest);
  113.  
  114. $colname_rsspecialty = "-1";
  115. if (isset($_GET['recordID'])) {
  116.   $colname_rsspecialty = (get_magic_quotes_gpc()) ? $_GET['recordID'] : addslashes($_GET['recordID']);
  117. }
  118. mysql_select_db($database_connection, $connection);
  119. $query_rsspecialty = sprintf("SELECT * FROM specialty_group WHERE member_id = %s", $colname_rsspecialty);
  120. $rsspecialty = mysql_query($query_rsspecialty, $connection) or die(mysql_error());
  121. $row_rsspecialty = mysql_fetch_assoc($rsspecialty);
  122. $totalRows_rsspecialty = mysql_num_rows($rsspecialty);
  123. ?>
  124.  
Many, many thanks to anyone who can help. Please let me know if you have any further questions or if anything is unclear.

Charity
Aug 24 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.