Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id EB7E0475BF0 for ; Wed, 21 Aug 2002 20:33:42 -0400 (EDT) Received: from sterling.betterhealthfoundation.net (user-112ugh2.biz.mindspring.com [66.47.66.34]) by postgresql.org (Postfix) with ESMTP id 2E49D47580E for ; Wed, 21 Aug 2002 20:33:42 -0400 (EDT) Received: from dinar ([192.168.1.12]) by sterling.betterhealthfoundation.net with Microsoft SMTPSVC(5.0.2195.4905); Wed, 21 Aug 2002 17:33:45 -0700 From: "Tom Haddon" To: Subject: Nextval Problems Date: Wed, 21 Aug 2002 17:33:45 -0700 Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook IMO, Build 9.0.2416 (9.0.2910.0) Importance: Normal X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300 X-OriginalArrivalTime: 22 Aug 2002 00:33:45.0231 (UTC) FILETIME=[92C5F9F0:01C24973] X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200208/67 X-Sequence-Number: 1412 Hi Folks, Quick question regarding inserting records into multiple tables that have key relationships. I have a php page that sets a variable to the nextval value, adds the record into the first table, and then inserts into the other tables with the value that was used for the first (foreign keys). Problem is, I have to add 1 onto the nextval to make this work, and when I look at the ids that are being added into the tables, they are not consecutive, one is being missed each time, so for instance I have 1000, 1002, 1004, etc. Can anyone help? Is this something to do with the way I am connecting to the database? I build it all the insertion pieces into one query separated by ";", of course. Originally I thought that was the problem, and split them into separate queries and then ran them one after the other, but no dice. Am I misunderstanding nextval? Here's how I get the nextval // Need to get value of next input id for agency_contact_info field $strnextval="SELECT nextval('agency_contact_info_id_key');"; // Open Recordset $q_nextval=pg_exec($dbconn, $strnextval) or die("Couldn't open the database --> " . pg_errormessage($q_nextval)); // Need to work out why this has to be '+1' $nextid=pg_result($q_nextval,0)+1; And here's me building the query to add in the record in the other tables // Breast Cancer Resources $strinsert_breast="INSERT INTO breast_cancer_resources (id) VALUES ($nextid);"; Thanks, much appreciated, Tom _______________________________ Tom Haddon IT Director The Better Health Foundation 414 Thirteenth Street, Suite 450 Oakland, CA 94612 (510) 444-5096 www.betterhealthfoundation.org _______________________________