CincoPistolero Posted October 29, 2009 Share Posted October 29, 2009 Not sure whether to put this in mysql help or php help. I have a php page that sends paramaters to a confirm page to update the database. I have two sql insert statements. The first updates the DB twice, it should only do it once. The second updates the DB once, this is working as intended. Below is the code. //Insert Into pwUser Table $sql="INSERT INTO pwUsers (userName, password, email, acctType, activationKey, active, createDate, loggedIn, lastLoggedIn, firstLogin) VALUES ('$userName','$md5pwd','$email',23,'$activationKey',0,CURDATE(), 0, CURDATE(),0)"; $result1 = mysql_query($sql) or die ("Error in query: $sql. " . mysql_error()); $aID=mysql_insert_id(); if (!mysql_query($sql)) { die('Error: ' . mysql_error()); } //Insert Into Employer Table $sql2="INSERT INTO employers (pwUserID, companyID, jobID, email, fname, lname, title, ac, ac2, phone, phone2, fax, mainAcct, active) VALUES ('$aID','','','$email','','','','','','','','',1,1)"; if (!mysql_query($sql2)) { die('Error: ' . mysql_error()); } Now, if I take out the below lines, the first INSERT updates only once, but pwUserID does not get set in the second INSERT. I need each INSERT to work once, and for pwUserID to get set in the second. $result1 = mysql_query($sql) or die ("Error in query: $sql. " . mysql_error()); $aID=mysql_insert_id(); Quote Link to comment Share on other sites More sharing options...
mpharo Posted October 29, 2009 Share Posted October 29, 2009 probably going to see more of the code on the page... Quote Link to comment Share on other sites More sharing options...
CincoPistolero Posted October 29, 2009 Author Share Posted October 29, 2009 The sql statements aren't failing. I'm inserting into the database in the first one, then what I want is the ID that gets created in that INSERT. I then want to put it into a field in the second INSERT. I've done this in code on other websites, but on this one, that original InSERT is reacting twice for whatever reason. I take those two lines out I mentioned above and it INSERTS once, wierd. Quote Link to comment Share on other sites More sharing options...
CincoPistolero Posted October 29, 2009 Author Share Posted October 29, 2009 I believe I resolved it. I had it error checking twice. I took out the below code for the first insert and then ran through the process again and it worked fine. if (!mysql_query($sql)) { die('Error: ' . mysql_error()); } Quote Link to comment Share on other sites More sharing options...
DavidAM Posted October 29, 2009 Share Posted October 29, 2009 //Insert Into pwUser Table $sql="INSERT INTO pwUsers (userName, password, email, acctType, activationKey, active, createDate, loggedIn, lastLoggedIn, firstLogin) VALUES ('$userName','$md5pwd','$email',23,'$activationKey',0,CURDATE(), 0, CURDATE(),0)"; $result1 = mysql_query($sql) or die ("Error in query: $sql. " . mysql_error()); $aID=mysql_insert_id(); // **** HERE YOU ARE EXECUTING THE QUERY A SECOND TIME // **** I THINK YOU WANT TO TEST $result1 if (!mysql_query($sql)) { die('Error: ' . mysql_error()); } Quote Link to comment Share on other sites More sharing options...
mpharo Posted October 29, 2009 Share Posted October 29, 2009 Typically you wouldnt use (!mysql_query($sql) you would just do (!$result1).... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.