473,624 Members | 2,238 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

no value when trying to insert record and retrieve ID

chumlyumly
9 New Member
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_i d()'.

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 2302
mwasif
802 Recognized Expert Contributor
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
chumlyumly
9 New Member
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 Recognized Expert Expert
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_VA RS 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
chumlyumly
9 New Member
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_registe r() - 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
8982
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 (ItemID) WHERE ItemDate = Date()", where conn is the connection object, etc. What am I getting wrong?
2
1761
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 method of dataset). When doing the restore i have to overwrite the data from xml back to database. these are the steps that i follow. 1. get the data from database.
11
4844
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 and have thus included a column in my address table called num_of_contacts. Everytime I add a new contact, I would like to increment the num_of_contacts column in the address table. Is this possible?
6
6508
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 table, I have to use SCOPE_IDENTITY(). Any ideas? SqlConnection conn = new SqlConnection(connectionString); conn.Open(); //Create the dataadapter
17
2690
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 can retrieve the autonum value for the new record. This doesn't occur with SQL Server, which of course causes an error (or at least in this code it does since there's an unhandled NULL value). Is there any way to retrieve this value when I add a...
5
2480
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 primary key in the "suspended_table" is an auto incremented integer where as "live_table" primary key is just a standard integer. Here's the life-cycle: 1. Record gets entered into "suspended_table" 2. Record checked and then inserted into...
9
9680
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 master-child link to the first subform. subform2 - Master Field: subTrainingModule.Form!TrainingModuleTopicSK Child Field: TrainingModuleTopicSK
4
3163
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, GetDate()) GROUP BY dbo.booking_form.TimeOfBooking") When I use the = sign the error reads
15
3521
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 AS IDENTITY ( START WITH 1
0
8170
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8675
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8619
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8474
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7158
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6108
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5561
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4078
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1784
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.