473,811 Members | 2,038 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Fetching info from a freshly created MYSQL Insert statement

12 New Member
I've about give up with the whole mysql_insert_id function. Always returns a "0". I do have an auto-incrementing field in my database called "ordernumbe r", which when I query it from another page, always gives the right answer of "45" or "60" or whatever. User fills out simple form on page 1. Form info is POSTed to response Page 2. If all goes well, and user does as instructed (field validation), user gets a nice pretty page to print out which contains all the stuff they just filled out (still on Page 2 now). However, I need user to also receive the 2 fields that were automatically created on their behalf, which were the "ordernumbe r" field and the "date" (yyyy-mm-dd format) field. There must be a way. Any help would be greatly appreciated. Is there a unique ID that creates the lock prior to the INSERT that I can reference immediately after the INSERT to then go back and fetch the remaining 2 new fields? Thanks,
JMO
Sep 27 '07 #1
17 2036
pbmods
5,821 Recognized Expert Expert
Heya, JMO. Welcome to TSDN!

Is there only a specific query that exhibits this behavior, or can you recreate it with any query that INSERTs data into that table?
Sep 28 '07 #2
joberman
12 New Member
Here's the relevant code.
Expand|Select|Wrap|Line Numbers
  1.  
  2. mysql_select_db("db199830720", $con);
  3.  
  4. $sql="INSERT INTO lensorder (firstName, lastName, ccname, cctype, ccnumber, cccode, ccmonth, ccyear, ccaddress1, ccaddress2, cccity, ccstate, cczip, ccphone, email, leftlens, rightlens, nopickup, shipaddress1, shipaddress2, shipcity, shipstate, shipzip, shipphone, date)
  5. VALUES
  6. ('$_POST[firstname]', '$_POST[lastname]', '$_POST[ccname]', '$_POST[cctype]', '$_POST[ccnumber]', '$_POST[cccode]', '$_POST[ccmonth]', '$_POST[ccyear]', '$_POST[ccaddress1]', '$_POST[ccaddress2]', '$_POST[cccity]', '$_POST[ccstate]', '$_POST[cczip]', '$_POST[ccphone]', '$_POST[email]', '$_POST[leftlens]', '$_POST[rightlens]', '$_POST[nopickup]', '$_POST[shipaddress1]', '$_POST[shipaddress2]', '$_POST[shipcity]', '$_POST[shipstate]', '$_POST[shipzip]', '$_POST[shipphone]', '$today')";
  7.  
  8.  
Shortly after this insert into the db, there is a simple table drawn with the results above. However, the "ordernumbe r" field in the database is auto-incremented with the successful addition of every complete record. Everything works like a champ, except I just can't figure out how to display the "ordernumbe r" field on this page.

(Sorry for the long code above - didn't know that it wasn't going to wrap)
Sep 28 '07 #3
pbmods
5,821 Recognized Expert Expert
Heya, JMO.

Code wraps better (and is easier to decipher in general) when you use whitespace ~_^

What is the output of this statement:
Expand|Select|Wrap|Line Numbers
  1. SHOW CREATE TABLE `lensorder`\G
  2.  
Sep 28 '07 #4
joberman
12 New Member
Sorry it took so long (crap satellite connection). Also had to remember how to run direct statements remotely (firewalled). Here's the output.
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE `lensorder` (
  2.  `firstname` varchar(20) NOT NULL default '',
  3.  `lastname` varchar(25) NOT NULL default '',
  4.  `ccname` varchar(30) NOT NULL default '',
  5.  `cctype` varchar(20) NOT NULL default '',
  6.  `ccnumber` varchar(25) NOT NULL default '',
  7.  `cccode` varchar(6) NOT NULL default '',
  8.  `ccmonth` varchar(12) NOT NULL default '',
  9.  `ccyear` varchar(5) NOT NULL default '',
  10.  `ccaddress1` varchar(25) NOT NULL default '',
  11.  `ccaddress2` varchar(50) NOT NULL default '',
  12.  `cccity` varchar(30) NOT NULL default '',
  13.  `ccstate` char(2) NOT NULL default '',
  14.  `cczip` varchar(10) NOT NULL default '',
  15.  `ccphone` varchar(14) NOT NULL default '',
  16.  `email` varchar(50) NOT NULL default '',
  17.  `leftlens` int(1) NOT NULL default '0',
  18.  `rightlens` int(1) NOT NULL default '0',
  19.  `nopickup` varchar(5) NOT NULL default '',
  20.  `shipaddress1` varchar(50) NOT NULL default '',
  21.  `shipaddress2` varchar(50) NOT NULL default '',
  22.  `shipcity` varchar(30) NOT NULL default '',
  23.  `shipstate` char(2) NOT NULL default '',
  24.  `shipzip` varchar(10) NOT NULL default '',
  25.  `shipphone` varchar(14) NOT NULL default '',
  26.  `leftqty` int(3) NOT NULL default '0',
  27.  `rightqty` int(3) NOT NULL default '0',
  28.  `ordernumber` int(8) NOT NULL auto_increment,
  29.  `date` date NOT NULL default '0000-00-00',
  30.  `freeshipping` tinyint(1) NOT NULL default '0',
  31.  `completed` tinyint(1) NOT NULL default '0',
  32.  PRIMARY KEY  (`ordernumber`)
  33. ) TYPE=MyISAM
The "/g" didn't take in the command.
Sep 28 '07 #5
pbmods
5,821 Recognized Expert Expert
Heya, JMO.

The '\G' is used in the MySQL client app to output results in vertical format instead of horizontal. It makes it a lot easier to copy and paste long pieces of result data.

Next time you're insanely curious, type \? at the MySQL prompt.

At any rate, your structure and query look OK to me.

Let me just make sure that I understand your situation. You're saying that if you were to add this line right after you execute the above query, the result is '0', correct?

Expand|Select|Wrap|Line Numbers
  1. mysql_query( $sql, $con );
  2. echo mysql_insert_id($con);
  3.  
Sep 28 '07 #6
joberman
12 New Member
Yup. "0" every time. Weird. By my understandaing, that code should return the most recent "autoincremente d" value. I remember reading something about the field being "null" or "not null" (default) might having something to do with the "0" return. Any further clues would be stellar. It's not that big of a deal if I can't get it to work, but I'm some what of a perfectionist so I'm hell-bent on eventually figuring it out.
Sep 28 '07 #7
pbmods
5,821 Recognized Expert Expert
Heya, JMO.

A couple of ideas. First thing to check is to make sure that your SQL query is valid.

Try adding this:
Expand|Select|Wrap|Line Numbers
  1. if( ! mysql_query( $sql, $con ) )
  2. {
  3.     echo $sql, '<br /><br />', mysql_error($con);
  4. }
  5.  
  6. echo mysql_insert_id($con);
  7.  
I believe you mentioned earlier that the INSERTs are executing successfully, but what the heck.

Perhaps try explicitly setting a NULL value for `ordernumber`.. .?
Expand|Select|Wrap|Line Numbers
  1. $sql = "INSERT INTO `lensorders` ( ... `ordernumber` ... ) VALUES ( ... NULL ... )"; 
  2.  
Sep 28 '07 #8
joberman
12 New Member
Hey! I got it to work! (Ok, actually you got it to work). I was leaving out the actual "mysql_queryxxx x" statement immediately prior to the insert_id statement. However, the last time I put the query statement in (earlier today), SQL threw up and spewed errors all over the place. Must have had my con and sql references mixed up. Thanks for the tip and the verification that I wasn't completely out of whack.
Sep 28 '07 #9
pbmods
5,821 Recognized Expert Expert
Heya, JMO.

Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)
Sep 28 '07 #10

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

Similar topics

3
8803
by: hokieghal99 | last post by:
Hi, I'd like to get user input from an html form into a mysql select statement. Here's where I'm stumped: $result = mysql_query("SELECT * FROM dept WHERE notes LIKE '%search-string%'",$db); I need to get the user's input into the '%search-string%' section, but I do not understand how to do this. I can hard-code a specific search
0
4563
by: Kenneth Illingsworth | last post by:
--=_603E7359.5B3A569C Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline I suspect that this is some kind of issue with privileges. However, the = account I am using has been given 'ALL' privileges to the database I am = trying to insert to. Am I save to assume that ALL includes insert = privileges?
0
2988
by: Kenneth Illingsworth | last post by:
--=_603E7359.5B3A569C Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline I suspect that this is some kind of issue with privileges. However, the = account I am using has been given 'ALL' privileges to the database I am = trying to insert to. Am I save to assume that ALL includes insert = privileges?
24
4167
by: clinttoris | last post by:
Could some please finish this line of code. For the life of me whatever I try it does not work. I am trying to insert the current date time into oracle. Now I know that in oracle if I run select to_char(sysdate, 'dd/mm/yy hh24:mi:ss') from dual result would be 15/06/06 14:49:35
9
1668
by: warezguy05 | last post by:
Hello I'm experiencing a problem; I've written a small script where volunteers can be booked for work-activities at a festival. The festival has 5 different departments so i've created a database-table named "COORDINATOR" where all 5 usernames, password (md5 protected) and short description of the departments are located. When a volunteer is getting booked by a certain festivaldepartment, the earlier mentioned description of the...
22
2752
by: Sandman | last post by:
So, I have this content management system I've developed myself. The system has a solid community part where members can register and then participate in forums, write weblogs and a ton of other things. So, in instances where I list, for example, the latest weblogs. I list the headline of the weblog, the date and the name of the member who wrote it. Now, the name isn't just "Smith", but rather Smith's online status, his nick and his...
1
3611
by: cumupkid | last post by:
II am trying to create a form that will allow me to upload photos to a folder in the site root directory and add the information to the mysql db at the same time. I have created two forms, one that uploads to photo to the folder in the site root diorectory. One that uploads the info to mysql database. but... When I try to upload them both ways at the same time i cant get it. I am Including code for both pages i have working. Code for...
1
9598
ssnaik84
by: ssnaik84 | last post by:
Hi Guys, Last year I got a chance to work with R&D team, which was working on DB scripts conversion.. Though there is migration tool available, it converts only tables and constraints.. Rest of things (stored procedures, functions).. we have to manually edit. That time, we face some interesting challenges.. I failed to document all of them, but whatever I can share with u.. I will try.. :) ...
1
2522
by: Maklar60 | last post by:
I am attempting to execute an INSERT statement on my page but continually get the following error: Microsoft OLE DB Provider for ODBC Drivers error '80040e14' Incorrect syntax near '<'. /int_code04/myNMLC/insertNewTrackRecord.asp, line 97 I've tested the INSERT stmt both within SQL Server and as a string literal within the page's code with hardcoded values to ensure that the statement works, which is does. But when I assign this...
0
10389
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...
1
10402
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9205
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...
0
6890
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
5554
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...
0
5692
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4339
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3867
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3018
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.