473,416 Members | 1,928 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

no value when trying to insert record and retrieve ID

chumlyumly
Hi -
I'm working with
PHP5
MySQL
Mac OSX

I've developed two pages where a user can input his/her info, which goes to a MySQL database. The first page is supposed to pass the newly created 'member_id' (which is an auto-increment field in the 'members' table) to the second page, where I have records going into another table that uses 'member_id' as a foreign key.

I've downloaded and used the Dreamweaver Server Behaviour "insert-retrieve ID," which creates a session variable (I've named it 'MM_id') equal to 'mysql_insert_id()'.

The problem is, it's not passing this variable to the next page. Am I missing something? Do I need to include a 'session_start()' somewhere?

Here's the pertinent code for the first page:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
  3. {
  4.   $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;
  5.  
  6.   switch ($theType) {
  7.     case "text":
  8.       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  9.       break;    
  10.     case "long":
  11.     case "int":
  12.       $theValue = ($theValue != "") ? intval($theValue) : "NULL";
  13.       break;
  14.     case "double":
  15.       $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
  16.       break;
  17.     case "date":
  18.       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  19.       break;
  20.     case "defined":
  21.       $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  22.       break;
  23.   }
  24.   return $theValue;
  25. }
  26.  
  27. $editFormAction = $_SERVER['PHP_SELF'];
  28. if (isset($_SERVER['QUERY_STRING'])) {
  29.   $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
  30. }
  31.  
  32. // DW Team Insert and Retrieve ID
  33. if ((isset($HTTP_POST_VARS["MM_insert"])) && ($HTTP_POST_VARS["MM_insert"] == "memberinfo")) {
  34.   $insertSQL = sprintf("INSERT INTO member (firstname, lastname, member_no, password, membertype, businessname, street, city, `state`, country, zip, bphone, bfax, mobilephone, email, website, directory_publish, initials, comment, firm_est, firm_size, firm_contact, firm_no_arch, firm_certified, firm_projects, council_district, prof_orgs, currently_serving, curr_serving_detail, interest_serving, interest_serving_detail, media_contact, faculty, sponsor_opps, home_tour, event_locale) 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, %s, %s, %s, %s, %s, %s, %s)",
  35.                        GetSQLValueString($_POST['mbr_fname'], "text"),
  36.                        GetSQLValueString($_POST['mbr_lname'], "text"),
  37.                        GetSQLValueString($_POST['memberno'], "int"),
  38.                        GetSQLValueString($_POST['password1'], "text"),
  39.                        GetSQLValueString($_POST['mbr_type'], "int"),
  40.                        GetSQLValueString($_POST['mbr_firmname'], "text"),
  41.                        GetSQLValueString($_POST['mbr_busaddress'], "text"),
  42.                        GetSQLValueString($_POST['mbr_city'], "text"),
  43.                        GetSQLValueString($_POST['mbr_state'], "text"),
  44.                        GetSQLValueString($_POST['country'], "text"),
  45.                        GetSQLValueString($_POST['mbr_zip'], "text"),
  46.                        GetSQLValueString($_POST['mbr_phone'], "text"),
  47.                        GetSQLValueString($_POST['mbr_fax'], "text"),
  48.                        GetSQLValueString($_POST['mbr_mobile'], "text"),
  49.                        GetSQLValueString($_POST['email'], "text"),
  50.                        GetSQLValueString($_POST['mbr_url'], "text"),
  51.                        GetSQLValueString(isset($_POST['directory_publish']) ? "true" : "", "defined","'Y'","'N'"),
  52.                        GetSQLValueString($_POST['initials'], "text"),
  53.                        GetSQLValueString($_POST['comment'], "text"),
  54.                        GetSQLValueString($_POST['firm_est'], "text"),
  55.                        GetSQLValueString($_POST['firm_size'], "int"),
  56.                        GetSQLValueString($_POST['firm_contact'], "text"),
  57.                        GetSQLValueString($_POST['firm_no_arch'], "int"),
  58.                        GetSQLValueString($_POST['firm_certified'], "text"),
  59.                        GetSQLValueString($_POST['firm_projects'], "text"),
  60.                        GetSQLValueString($_POST['council_district'], "text"),
  61.                        GetSQLValueString($_POST['prof_orgs'], "text"),
  62.                        GetSQLValueString($_POST['currently_serving'], "text"),
  63.                        GetSQLValueString($_POST['curr_serving_detail'], "text"),
  64.                        GetSQLValueString($_POST['interest_serving'], "text"),
  65.                        GetSQLValueString($_POST['interest_serving_detail'], "text"),
  66.                        GetSQLValueString($_POST['media_contact'], "text"),
  67.                        GetSQLValueString($_POST['faculty'], "text"),
  68.                        GetSQLValueString($_POST['sponsor_opps'], "text"),
  69.                        GetSQLValueString($_POST['home_tour'], "text"),
  70.                        GetSQLValueString($_POST['event_locale'], "text"));
  71.  
  72.   mysql_select_db($database_connection, $connection);
  73.   $Result1 = mysql_query($insertSQL, $connection) or die(mysql_error());
  74.   $_SESSION["MM_id"] = mysql_insert_id();
  75.   $insertGoTo = "insert_specialty2.php";
  76.   if (isset($HTTP_SERVER_VARS['QUERY_STRING'])) {
  77.     $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
  78.     $insertGoTo .= $HTTP_SERVER_VARS['QUERY_STRING'];
  79.   }
  80.   header(sprintf("Location: %s", $insertGoTo));
  81. }
  82.  
  83. $colname_rs_member = "-1";
  84. if (isset($_GET['recordID'])) {
  85.   $colname_rs_member = (get_magic_quotes_gpc()) ? $_GET['recordID'] : addslashes($_GET['recordID']);
  86. }
  87. mysql_select_db($database_connection, $connection);
  88. $query_rs_member = sprintf("SELECT * FROM member WHERE member_id = %s", $colname_rs_member);
  89. $rs_member = mysql_query($query_rs_member, $connection) or die(mysql_error());
  90. $row_rs_member = mysql_fetch_assoc($rs_member);
  91. $totalRows_rs_member = mysql_num_rows($rs_member);
  92. ?>
  93.  
Any help is GREATLY appreciated...

Charity
Aug 28 '07 #1
4 2286
mwasif
802 Expert 512MB
You must have to use session_start() whenever you need to manipulate session variables. Put this on the top of the before, make sure there should not be any sort of output.
Aug 28 '07 #2
You must have to use session_start() whenever you need to manipulate session variables. Put this on the top of the before, make sure there should not be any sort of output.
Thank you! Could you please tell me exactly where in my code the 'session_start()' would go? I've put it in there before - first, at the top of the code, and I tried another time to put it right before the session variable, and it still didn't work - the page just took a really long time to load.

Thanks again.

Charity
Aug 28 '07 #3
Atli
5,058 Expert 4TB
Hi.

The session_start() function should be put at the top of the page, or at least before any output.

Also, I see you use $HTTP_POST_VARS and $HTTP_SERVER_VARS in some places. Although they are still available in PHP5 they are deprecated and you should avoid using them. Use the $_POST and $_SERVER superglobals instead, as you do most of the time.
Aug 29 '07 #4
Thank you for your reply. I've changed the old code to the new - thanks for noticing!

I've been fiddling with it all night, to no avail. I've added the session_start() to the top, I've added session_name(), session_register() - with variables - and nothing.

What happens is when I submit the first page to go to the second (and hopefully pass the session variable) one of two things happens.

Either

1 - the page takes a VERY long time to load, and when it does, it's blank; or,
2 - the second page loads, but when I submit it, it says that the 'member_id' field is null. Also, just for testing, I've added dynamic text to the page that's supposed to contain the Session Variable - it's blank space.

The code for the first page is the same, except that I now have this at the top:

Expand|Select|Wrap|Line Numbers
  1. <?php require_once('../Connections/connection.php'); ?>
  2. <?php session_start(); 
  3.  
  4. // *** Redirect if username exists
  5. $MM_flag="MM_insert";
  6. if (isset($_POST[$MM_flag])) {
  7.   $MM_dupKeyRedirect="username_in_use.htm";
  8.   $loginUsername = $_POST['memberno'];
  9.   $LoginRS__query = "SELECT member_no FROM member WHERE member_no='" . $loginUsername . "'";
  10.   mysql_select_db($database_connection, $connection);
  11.   $LoginRS=mysql_query($LoginRS__query, $connection) or die(mysql_error());
  12.   $loginFoundUser = mysql_num_rows($LoginRS);
  13.  
  14.   //if there is a row in the database, the username was found - can not add the requested username
  15.   if($loginFoundUser){
  16.     $MM_qsChar = "?";
  17.     //append the username to the redirect page
  18.     if (substr_count($MM_dupKeyRedirect,"?") >=1) $MM_qsChar = "&";
  19.     $MM_dupKeyRedirect = $MM_dupKeyRedirect . $MM_qsChar ."requsername=".$loginUsername;
  20.     header ("Location: $MM_dupKeyRedirect");
  21.     exit;
  22.   }
  23. }
  24. ?>
  25. <?php
  26. function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
  27.  
etc.

The second page's code looks like this:
Expand|Select|Wrap|Line Numbers
  1. <?php require_once('../Connections/connection.php'); ?>
  2. <?php session_start(); ?>
  3. <?php require_once('../Connections/connection.php'); ?>
  4. <?php
  5. function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
  6. {
  7.   $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;
  8.  
  9.   switch ($theType) {
  10.     case "text":
  11.       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  12.       break;    
  13.     case "long":
  14.     case "int":
  15.       $theValue = ($theValue != "") ? intval($theValue) : "NULL";
  16.       break;
  17.     case "double":
  18.       $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
  19.       break;
  20.     case "date":
  21.       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  22.       break;
  23.     case "defined":
  24.       $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  25.       break;
  26.   }
  27.   return $theValue;
  28. }
  29.  
  30.  
  31. $editFormAction = $_SERVER['PHP_SELF'];
  32. if (isset($_SERVER['QUERY_STRING'])) {
  33.   $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
  34.  
  35. }
  36.  
  37. @reset($specialty);
  38. mysql_select_db($database_connection, $connection);
  39. if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "memberinfo"));
  40. while (list($key, $val) = @each ($specialty)) {
  41.   $insertSQL = sprintf("INSERT INTO specialty_group (specialty, member_id) VALUES (%s,%s)",
  42.                        GetSQLValueString($val, "text"),
  43.                        GetSQLValueString($_POST['hiddenID'], "int"));
  44.  
  45.   mysql_select_db($database_connection, $connection);
  46.   $Result1 = mysql_query($insertSQL, $connection) or die(mysql_error());
  47.  
  48. $insertGoTo = "update_complete.php";
  49.   if (isset($_SERVER['QUERY_STRING'])) {
  50.     $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
  51.     $insertGoTo .= $_SERVER['QUERY_STRING'];
  52.   }
  53.   header(sprintf("Location: %s", $insertGoTo));
  54. }
  55.  
  56. $colname_rsspecialty = "-1";
  57. if (isset($_SESSION['MM_id'])) {
  58.   $colname_rsspecialty = (get_magic_quotes_gpc()) ? $_SESSION['MM_id'] : addslashes($_SESSION['MM_id']);
  59. }
  60. mysql_select_db($database_connection, $connection);
  61. $query_rsspecialty = sprintf("SELECT * FROM specialty_group WHERE member_id = %s", $colname_rsspecialty);
  62. $rsspecialty = mysql_query($query_rsspecialty, $connection) or die(mysql_error());
  63. $row_rsspecialty = mysql_fetch_assoc($rsspecialty);
  64. $totalRows_rsspecialty = mysql_num_rows($rsspecialty);
  65. ?>
  66.  
Is this more of a help?

Thanks again - the help is so appreciated!

Charity
Aug 29 '07 #5

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

Similar topics

14
by: WC Justice | last post by:
I'm assuming this can be done, but I can't seem to get it to work... I'd like to easily return a single value from a sql statement, something like: "intNewItems = conn.execute "SELECT COUNT...
2
by: Devesh Aggarwal | last post by:
Hi, I have a backup and restore module in my project. The backup uses a typed dataset object (XSD) to get the data from database and creates a xml file as the backup file (using the WriteXml...
11
by: Randell D. | last post by:
Folks, I have a table of addresses and a seperate table with contact names - All addresses tie to one or more names - I would like to keep track of the number of names 'belonging' to an address...
6
by: Hardy Wang | last post by:
Hi all, I have the following codes, but SCOPE_IDENTITY() just returns NULL to me. If I comment out SCOPE_IDENTITY() line and run @@IDENTITY line, it works fine!! Since I have a trigger on the...
17
by: Rico | last post by:
Hello, I am in the midst of converting an Access back end to SQL Server Express. The front end program (converted to Access 2003) uses DAO throughout. In Access, when I use recordset.AddNew I...
5
by: Phil Latio | last post by:
I have 2 virtually identical tables and wish to move data between them. Basically one table is called "live_table" and the other is named "suspended_table" and the only difference is that the...
9
by: Ecohouse | last post by:
I have a main form with two subforms. The first subform has the child link to the main form identity key. subform1 - Master Field: SK Child Field: TrainingMasterSK The second subform has a...
4
by: Simon Gare | last post by:
Hi all, I am trying to retrieve a count of booking entries made 30 days ago, below is the end of the query I am having problems with. dbo.booking_form.TimeOfBooking = DATEADD(day, -30,...
15
by: gunnar.sigurjonsson | last post by:
I´m having some problem retrieving identity value from my newly inserted row into a view. I have two tables T1 and T2 which I define as following CREATE TABLE T1 ( id BIGINT GENERATED ALWAYS...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.