我几乎没有用于日常审计事务的php代码。我做了简单的事来说明问题。
$result = mssql_query("BEGIN TRAN");
$result = mssql_query("insert into items_history (select * from items)"); //move transaction to history
$result = mssql_query("delete * from items)"); //clear transaction table for new month transaction
$result = mssql_query( //get the data for used in another script
"select items_history.item_id,
items_history.item_name,
group_items.group_name
from
items_history,group_items
where group_items.id=items_history.id and
day(items_history.date_trans)=day(items_history.date_trans)-1 " // whit where include
);
$result = mssql_query("update trans_control set current_day=current_day+1" }; //update the system date to next day
if (!$result) {
mssql_query("ROLLBACK TRAN");
} else {
mssql_query("COMMIT TRAN");
}
mssql_close();
由于某些原因,此数据库需要与mysql数据库联机存储。在离线时,我不太担心使用此代码的安全性。但在网上,它让我对安全有了更多的思考。现在我想把这个脚本转换成PDO MySql。目标很简单,更安全:
$result = q("BEGIN");
$result = q("qry1");
$result = q("qry2");
$result = q("qry3");// select with many join table and some parameter data in where like 'string','int', 'date', and maybe with "Union All" in select
$result = q("qry..."};
if (!$result) {
q("ROLLBACK");
} else {
q("COMMIT");
}
如果另一个问题有同样的问题。我很高兴从这个特别简单的包装开始,这样我就可以学习它是如何工作的。谢谢你以前。
只要使用绑定参数,安全性应该没有问题,请访问www.php.net/manual/en/pdostement.bindparam.php和http://www.php.net/manual/en/pdostatement.bindvalue.php
对于您的交易,您可以使用以下方法来模拟相同的事情:
http://www.php.net/manual/en/pdo.begintransaction.php代替BEGIN TRAN查询,http://www.php.net/manual/en/pdo.commit.php代替COMMIT,http://www.php.net/manual/en/pdo.rollback.php而不是ROLLBACK
但是,如果查询与第一个代码示例中的查询完全相同,我看不到任何可能导致安全问题的外部参数