PHP mall security database transaction processing method

Now an era electricity supplier, electricity supplier must first do a mall, mall so security can not be ignored, a data security is inseparable from transaction processing databases mall, mall in the capital, goods, orders, billing, etc. this is an important step plus transaction control is not necessarily essential, like some open Source mall on the market I have to study.
Independent programmers should pay attention to the development of the mall this thing, and now I am here to introduce the method operating under PHP MySQL transaction, to share to you for your reference. Specific methods are as follows:
In general, the transaction should have ACID characteristics. The so-called ACID is an Atomic (Atomic), Consistent (consistency), Isolated (isolation), Durable (continuing) the first letter written by four words, following a "bank transfer" as an example to explain what they mean, respectively, :
① atomicity: composition transaction statement forming one logical unit does not perform only part of it. In other words, the transaction is indivisible minimum unit. For example: bank transfer process, must be subtracted from an account transfer amount, and added to another account, changing only one account is unreasonable.
② consistency: executed before and after the transaction, the database is consistent. In other words, the transaction should correct conversion system status. For example: bank transfer process, or transfer money from one account into another account, or both accounts are the same, no other cases.
③ Isolation: a transaction has no effect on the other transaction. That can not see any transaction in a transaction in an incomplete state. For example, bank transfer, prior to the transfer transaction did not submit, another transfer transaction can only be in a wait state.
④ Sustainability: the effect of the transaction can be preserved permanently. Conversely, the transaction should be able to withstand all of the failures, including servers, processes, communications and media failure and so on. For example: bank transfer process, the transfer status of the account to be preserved.
In PHP, mysqli has a very good package of related operations mysql transaction. The following example:
SQL1 = $ "Update the User SET ScoreCount = +10 ScoreCount WHERE ID = '123456'";
$ SQL2 = "Update ScoreDetail FScore SET = 300 WHERE ID = '123456'";
$ SQL3 = "INSERT INTO ScoreDetail ID, Score) values ( '123456', 60) ";
$ mysqli = new new mysqli ( 'localhost', 'root', '', 'DB_Lib2Test');
$ mysqli-> autocommit (false); // start things
$ mysqli-> query ( SQL1 $);
$ mysqli-> Query ($ SQL2);
! IF ($ mysqli-> errno) {
  $ mysqli-> the commit ();
  echo 'OK';
} the else {
 echo 'ERR';
  $ mysqli-> ROLLBACK ();
}
here, we'll use the php mysql series function executes transactions.
$ sql1 = "update User set ScoreCount = ScoreCount +10 where ID = '123456'";

$sql3 = "insert into  ScoreDetail ID,Score) values ('123456',60)";
$conn = mysql_connect('localhost','root','');
mysql_select_db('DB_Lib2Test');
mysql_query('start transaction');
//mysql_query('SET autocommit=0');
mysql_query($sql1);
mysql_query($sql2);
if(mysql_errno ()){
    mysql_query('rollback');
    echo 'err';
}else{
    mysql_query('commit');
    echo 'ok';
}
// mysql_query('SET autocommit=1');
// mysql_query($sql3);

Guess you like

Origin blog.51cto.com/13938514/2404617
Recommended