/    Sign up×
Community /Pin to ProfileBookmark

insert ino multiple table

hello friends
suppose we have 2 job to do with mySQL database with PHP
an insert:

[code=php]
$insertSQL1 = sprintf(“INSERT INTO table1 (id1, id2) VALUES (%s, %s)”,
GetSQLValueString($id1, “int”),
GetSQLValueString($id2, “int”));
$Result1 = mysql_query($insertSQL1, $db_con) or die();

[/code]

then an update

[code=php]
$updateSQL1 = sprintf(“UPDATE table2 SET grouplist=%s, group_name=%s WHERE userID=%s”,
GetSQLValueString($groupID, “int”),
GetSQLValueString($g_name, “text”),
GetSQLValueString($ID, “int”));
$Result2 = mysql_query($updateSQL1, $db_con) or die();
[/code]

it is very important to do both insert & update together
but if in any reason mysql stop response to second job insert will work but update won’t
how can I force PHP does not do 1 job if other has error?

thanks in advance

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@svidgenJun 06.2012 — And to explain it briefly, a transaction does precisely what you're looking for. You tell MySQL you want to start a transaction (using a START TRANSACTION) query. You execute some other queries. You then execute either a ROLLBACK to cancel all queries sent since the START TRANSACTION, or a COMMIT to seal the deal.

If you start a transaction and disconnect, I believe a rollback is assumed. (Read up on it though.) And, last I checked (which was admittedly quite some time ago), transactions were not supported by the MyISAM engine.

But, I'm sure you're using InnoDB anyway ?
×

Success!

Help @ma3743 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.1,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...