violinrocker Posted April 25, 2011 Share Posted April 25, 2011 i have to tables... forums & subscription i would like to insert a random character string into 'forums' to be used as a subscription id. and then have the same random string to be inserted into 'subscription' Link to comment https://forums.phpfreaks.com/topic/234648-random-character-string/ Share on other sites More sharing options...
gristoi Posted April 25, 2011 Share Posted April 25, 2011 Instead of using a random string why not use an auto incrimenting primary key for subscriptions. Then each one would have to be unique Link to comment https://forums.phpfreaks.com/topic/234648-random-character-string/#findComment-1205833 Share on other sites More sharing options...
violinrocker Posted April 25, 2011 Author Share Posted April 25, 2011 initially i wanted to use auto increment... but then... how would i link the auto-incremented number from the forums to the auto-incremented number in the subscription? Link to comment https://forums.phpfreaks.com/topic/234648-random-character-string/#findComment-1205834 Share on other sites More sharing options...
gristoi Posted April 25, 2011 Share Posted April 25, 2011 you would use the subscription Id (primary key) as a foreign key in the forums table. or vice versa. That is how you reference the relationship between the tables Link to comment https://forums.phpfreaks.com/topic/234648-random-character-string/#findComment-1205835 Share on other sites More sharing options...
violinrocker Posted April 25, 2011 Author Share Posted April 25, 2011 I think Im getting what you are trying to say... but I am going to insert the forum post and the subscription at the same time and i have no idea how i could get the same incremented ID to be used on the other one Link to comment https://forums.phpfreaks.com/topic/234648-random-character-string/#findComment-1205839 Share on other sites More sharing options...
gristoi Posted April 25, 2011 Share Posted April 25, 2011 Ok, I am missing something here. What is the subscription part for. If you want to use the forum table with a primary key i would do something along the line of: $connection = mysql_connect("localhost", "username", "pass"); $sql = "INSERT INTO forums (value1,value2, value3) VALUES ('v1','v2','v3')"; $result = mysql_query($sql,$connection); $lastId = mysql_insert_id(); $sql2 = "INSERT INTO subscriptions (forumId,value2, value3) VALUES ('$lastId','v2','v3')"; $result2 = mysql_query($sql2,$connection); Dont forget that if you do use an auto incimenting primary key then you do not need to put it in the insert query. Link to comment https://forums.phpfreaks.com/topic/234648-random-character-string/#findComment-1205847 Share on other sites More sharing options...
violinrocker Posted April 25, 2011 Author Share Posted April 25, 2011 nice, i think this would work... $lastId = mysql_insert_id(); ... its the first time i've seen it Link to comment https://forums.phpfreaks.com/topic/234648-random-character-string/#findComment-1205849 Share on other sites More sharing options...
gristoi Posted April 25, 2011 Share Posted April 25, 2011 Basically, if the last thing inserted into a table by sql had an auto incremented primary key then that function would retrieve the designated key. comes in very handy Link to comment https://forums.phpfreaks.com/topic/234648-random-character-string/#findComment-1205850 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.