cougarreddy Posted June 5, 2009 Share Posted June 5, 2009 Hi..I am developing a website, where, database has three tables. "usertable","usercourse table" and other is "coursetable". user table columns -> userid,name,password,status(dropped or not) usercourse table columns -> userid,courseid,instructor,grade coursetable->courseid,instructor requirement is, when user acct is created by admin, user details or inserted into usertable, and also usercourse table. for example if there are three courses available in "course table", 3 records are created in "usercourse table".. that means, combination of "userid"+"courseid" is unique in "usercourse table" My question is how can I make sure that when a user account is created records are updated into both tables or not into any. for now I am using code similar to this(using PHP) <?PHP $qry="INSERT INTO users values('**','**'.....)"; $usr_result=mysql_query($qry) or die(""); if($usr) { $qry="INSERT INTO usercourse values('**','**'.....)"; $courseusr_result=mysql_query($qry) or die(""); if($courseusr_result) //success; } ?> for now, this makes sure that no records are inserted into usercourse table, if first query is unsuccessful. But what I want is, I want to make sure, no records are inserted into "users" if second query fails. I want to know how can I guarantee this. Sry, I am a newbie not just to mysql, but to entire db stuff.. Any help is appreciated. Link to comment https://forums.phpfreaks.com/topic/161082-inserting-into-tables-at-once/ Share on other sites More sharing options...
gevans Posted June 5, 2009 Share Posted June 5, 2009 You need to have a look at transactions... You may need to specify your table type, but I have it working on MySQL 5 http://dev.mysql.com/doc/refman/5.0/en/ansi-diff-transactions.html Link to comment https://forums.phpfreaks.com/topic/161082-inserting-into-tables-at-once/#findComment-850063 Share on other sites More sharing options...
cougarreddy Posted June 5, 2009 Author Share Posted June 5, 2009 thanks Link to comment https://forums.phpfreaks.com/topic/161082-inserting-into-tables-at-once/#findComment-850109 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.