Jump to content

Inserting into tables at once


cougarreddy

Recommended Posts

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.