ShaolinF Posted January 25, 2008 Share Posted January 25, 2008 Hi Guys, I need to populate 2 tables with data. One is the users table and the other is a purchase table. Now this is the code I am using for the users table: $insert = mysql_query("INSERT INTO users VALUES ('', '".$_SESSION['NAME']."', '".$_SESSION['CONTACTNO']."', '".$_SESSION['EMAILADD']."', '".$_SESSION['GENDER']."')"); Now for the purchase field, is there anyway I could grab the userid for the user above without going through a lengthy SELECT statement ? Please note, there is no user registration allowed on the website. Quote Link to comment https://forums.phpfreaks.com/topic/87671-solved-getting-userid/ Share on other sites More sharing options...
p2grace Posted January 25, 2008 Share Posted January 25, 2008 Sure can $userid = mysql_insert_id(); Edit: Make sure you put that after the query. Quote Link to comment https://forums.phpfreaks.com/topic/87671-solved-getting-userid/#findComment-448432 Share on other sites More sharing options...
ShaolinF Posted January 25, 2008 Author Share Posted January 25, 2008 Thanks. I have now added the INSERT for purchase and am getting the following error: Parse error: syntax error, unexpected T_VARIABLE code (ERROR is in the $insert variable): //Grab userid and eventid to be used for purchase table $userid = mysql_insert_id(); $eventid = mysql_query("SELECT eventid FROM event WHERE eventset = 1") //Update time to time of input into DB // Add purchase details $insert = mysql_query("INSERT INTO purchase VALUES ('', '$userid', '$eventid', '".$_SESSION['TICKET']."', '".$_SESSION['TOTALPRICE']."', 'N', '')") or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/87671-solved-getting-userid/#findComment-448454 Share on other sites More sharing options...
revraz Posted January 25, 2008 Share Posted January 25, 2008 Missing a semi colon $eventid = mysql_query("SELECT eventid FROM event WHERE eventset = 1"); <------ Quote Link to comment https://forums.phpfreaks.com/topic/87671-solved-getting-userid/#findComment-448459 Share on other sites More sharing options...
p2grace Posted January 25, 2008 Share Posted January 25, 2008 Yeup, Nice catch revraz Quote Link to comment https://forums.phpfreaks.com/topic/87671-solved-getting-userid/#findComment-448461 Share on other sites More sharing options...
ShaolinF Posted January 25, 2008 Author Share Posted January 25, 2008 Thanks, didnt see that. Yet again another 2 problems. First of all, I am trying to insert the date into the table but its not going. This is how I defined the column: timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP, How I am adding date into the the column: $date = date("D dS M,Y h:i a"); //Update time to time of input into DB // Add purchase details $insert2 = mysql_query("INSERT INTO purchase VALUES ('', '$userid', '$eventid', '".$_SESSION['TICKET']."', '".$_SESSION['TOTALPRICE']."', 'N', '$date')") or die(mysql_error()); sECOND PROBLEM: the eventid when inserted into the db does not work. See code: $eventid = mysql_query("SELECT eventid FROM event WHERE eventset = 1"); Quote Link to comment https://forums.phpfreaks.com/topic/87671-solved-getting-userid/#findComment-448469 Share on other sites More sharing options...
revraz Posted January 25, 2008 Share Posted January 25, 2008 Since you set the MySQL Column to TIMESTAMP, you have to follow their format. '0000-00-00 00:00:00' Quote Link to comment https://forums.phpfreaks.com/topic/87671-solved-getting-userid/#findComment-448488 Share on other sites More sharing options...
p2grace Posted January 25, 2008 Share Posted January 25, 2008 sECOND PROBLEM: the eventid when inserted into the db does not work. See code: $eventid = mysql_query("SELECT eventid FROM event WHERE eventset = 1"); Is the name of the field "eventid"? You could also try using complete syntax. $eventid = mysql_query("SELECT `eventid` FROM `event` WHERE `eventset` = '1'"); Quote Link to comment https://forums.phpfreaks.com/topic/87671-solved-getting-userid/#findComment-448500 Share on other sites More sharing options...
ShaolinF Posted January 25, 2008 Author Share Posted January 25, 2008 Well I checked its output and it gave me a resource # something. I will have to use mysql_fetch_array. Is there any other way as I only want 1 record ? Quote Link to comment https://forums.phpfreaks.com/topic/87671-solved-getting-userid/#findComment-448514 Share on other sites More sharing options...
revraz Posted January 25, 2008 Share Posted January 25, 2008 You have to fetch the resource. Quote Link to comment https://forums.phpfreaks.com/topic/87671-solved-getting-userid/#findComment-448517 Share on other sites More sharing options...
p2grace Posted January 25, 2008 Share Posted January 25, 2008 That's not returning the actual id. You need to do something like this: $run = mysql_query("SELECT eventid FROM event WHERE eventset = 1"); $eventid = mysql_result($run,"0","eventid"); Quote Link to comment https://forums.phpfreaks.com/topic/87671-solved-getting-userid/#findComment-448520 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.