lukep11a Posted July 11, 2011 Share Posted July 11, 2011 Hi, I have some code that inserts two text fields into a table called 'privateleague', a privateleagueid field is also automatically generated in that table. I then want to update the user table that is called 'login' with the privateleagueid that has been automatically created. It all worked ok until I added the UPDATE part to the $query, this is my code: <?php $privateleaguename = $_POST['privateleaguename']; $privateleaguepasscode = $_POST['privateleaguepasscode']; $query="INSERT INTO privateleague (privateleaguename, privateleaguepasscode)VALUES ('".$privateleaguename."','".$privateleaguepasscode."'), UPDATE login SET privateleagueid = privateleagueid WHERE userid = '{$_SESSION['userid']}'"; mysql_query($query) or die ('Error updating database'); ?> Is it even possible to update the login table with a field that is being created in the same $query? If so, can anyone tell me if they can see any errors in the code, any help would be much appreciated. Regards Luke Quote Link to comment https://forums.phpfreaks.com/topic/241678-insertupdate-query-help/ Share on other sites More sharing options...
teynon Posted July 11, 2011 Share Posted July 11, 2011 Pretty sure you need to separate the queries. $query="INSERT INTO privateleague (privateleaguename, privateleaguepasscode)VALUES ('".$privateleaguename."','".$privateleaguepasscode."')"; mysql_query($query) or die ('Error updating database'); $query="UPDATE login SET privateleagueid = privateleagueid WHERE userid = '{$_SESSION['userid']}'"; mysql_query($query) or die("Error Updating Database"); Quote Link to comment https://forums.phpfreaks.com/topic/241678-insertupdate-query-help/#findComment-1241257 Share on other sites More sharing options...
lukep11a Posted July 11, 2011 Author Share Posted July 11, 2011 Thanks, will give that a try, so do you think it's possible to update the table with an auto increment field that has been created in the first $query? do I not need to create a variable for it? Quote Link to comment https://forums.phpfreaks.com/topic/241678-insertupdate-query-help/#findComment-1241265 Share on other sites More sharing options...
teynon Posted July 11, 2011 Share Posted July 11, 2011 Sorry, didn't even look at the actual sql.. After you insert, get the insert id using "$id=mysql_insert_id();" then your update sql would look like update login set privateleagueid = {$id} WHERE userid = '{$_SESSION['userid']}' Quote Link to comment https://forums.phpfreaks.com/topic/241678-insertupdate-query-help/#findComment-1241267 Share on other sites More sharing options...
lukep11a Posted July 11, 2011 Author Share Posted July 11, 2011 thanks, thought there was maybe something else, will give it a go and let you know Quote Link to comment https://forums.phpfreaks.com/topic/241678-insertupdate-query-help/#findComment-1241271 Share on other sites More sharing options...
lukep11a Posted July 11, 2011 Author Share Posted July 11, 2011 thanks for your help, that worked perfectly! Quote Link to comment https://forums.phpfreaks.com/topic/241678-insertupdate-query-help/#findComment-1241532 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.