php防sql注入類別(php pdo防止sql注入的類別)

WBOY
發布: 2016-07-25 08:52:02
原創
1122 人瀏覽過
  1. class Model{

  2. protected $tableName="";//表名
  3. protected $pOb ;//pdo類物件
  4. function __construct(){
  5. $pdo=new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME,DB_USERNAME,DB_PASSWORD);
  6. $pdo ->exec("set names ".DB_CHARSET);
  7. $this->pOb=$pdo;
  8. }
  9. /*
  10. * 作用:增
  11. * 參數:array $arr exp :array('欄位名稱'=>值,'欄位名稱'=>值,....)
  12. * return:int|false
  13. */
  14. function add($arr){
  15. //拼sql語句
  16. $kArr=array_keys($arr);
  17. $kStr=join(",",$kArr);
  18. $vArr=array_values($arr);
  19. $pStr = '';

  20. foreach ($vArr as $s=>$y){
  21. $vname = "p".$s;
  22. $pStr.=': '.$vname.',';
  23. }
  24. $pStr = substr($pStr,0,-1);
  25. $sql = "insert into {$this- >tableName}($kStr) values($pStr)";

  26. print_r($sql);

  27. $pdoS = $this->pOb ->prepare($sql);
  28. foreach ($vArr as $k=>$y){
  29. $vname = "p".$k;
  30. $$vname = $y;
  31. var_dump($vname,$$vname) ;
  32. $pdoS -> bindParam(":".$vname, $$vname,PDO::PARAM_STR);
  33. }

  34. $re = $pdoS -> execute( );
  35. if($re){//新增成功
  36. //回傳主鍵id值
  37. return $this->pOb->lastInsertId();
  38. }
  39. //回傳值
  40. return $re;
  41. }
  42. public function delete($arrWhere){
  43. if(!empty($arrWhere)){
  44. $strW = " where ";
  45. foreach( $arrWhere as $kW=>$vW){
  46. $kn = str_replace(":", "", $kW);
  47. if(count($arrWhere)==1){
  48. $strW .= $kn."=".$kW;
  49. }else{
  50. $strW .= $kn."=".$kW." and ";
  51. }
  52. }
  53. if(count($arrWhere)>1){
  54. $strW .= " 1=1 ";
  55. }
  56. }
  57. $sql = "delete from {$this->tableName}". $strW;
  58. print_r($sql);
  59. $pdoS = $this->pOb->prepare($sql);
  60. foreach ($arrWhere as $kW=>$vW){
  61. $kn = str_replace(":", "", $kW);
  62. $$kn = $vW;
  63. if(is_int($vW)){
  64. $pdoS->bindParam($kW, $$kn,PDO::PARAM_INT);
  65. }else if(is_float($vW)){
  66. $pdoS->bindParam($kW,$$kn,PDO::PARAM_INT);
  67. } else{
  68. $pdoS->bindParam($kW,$$kn,PDO::PARAM_STR);
  69. }
  70. }
  71. $re=$pdoS->execute();
  72. if
  73. if ($re){
  74. return true;
  75. }else {
  76. return false;
  77. }
  78. }
  79. function update($arrSet,$arrWhere){
  80. //拼sql語句
  81. $str = "";
  82. $n=0;
  83. foreach ($arrSet as $kS=>$vS){
  84. $str .= " ,".$kS."=:p".$n ;

  85. }
  86. $str = substr($str, 1);
  87. foreach($arrWhere as $kW=>$vW){
  88. $kn=str_replace(":","",$kW);
  89. if(count($arrWhere)==1){
  90. $strW .= $kn."=".$kW;
  91. }else{
  92. $strW .= $kn."=".$kW." and ";
  93. }
  94. }
  95. if(count($arrWhere)>1){
  96. $strW .= " 1=1 ";
  97. }
  98. $sql="update {$this->tableName} set {$str} where ".$strW;

  99. //print_r($sql);
  100. $pdoS=$this->pOb->prepare($sql);

  101. $x = 0;
  102. foreach($ arrSet as $kS=>$vS){
  103. $kS = ":p".$x ;

  104. $$kS = $vS;
  105. if(is_int($vS)){

  106. $pdoS->bindParam($kS,$$kS,PDO::PARAM_INT);
  107. }else if(is_float($vS)){
  108. $ pdoS->bindParam($kS,$$kS,PDO::PARAM_INT);
  109. }else{
  110. $pdoS->bindParam($kS,$$kS,PDO::PARAM_STR);
  111. }
  112. }
  113. foreach($arrWhere as $kW=>$vW){
  114. $kn=str_replace(":","",$kW);
  115. $$kn=$vW;//$p0 $p1 $p2
  116. if(is_int($vW)){
  117. $pdoS->bindParam($kW,$$kn,PDO::PARAM_INT) ;
  118. }else if(is_float($vW)){
  119. $pdoS->bindParam($kW,$$kn,PDO::PARAM_INT);
  120. }else{
  121. $pdoS-> bindParam($kW,$$kn,PDO::PARAM_STR);
  122. }
  123. }
  124. $re=$pdoS->execute();
  125. if($re){
  126. return true;
  127. }else{

  128. return false;
  129. }
  130. }

  131. //查詢
  132. function select($field ="*",$ArrayWhere="",$order="",$limit=""){
  133. if(!empty($ArrayWhere)){
  134. $strW = " where ";
  135. foreach($ArrayWhere as $kW=>$vW){
  136. $kn=str_replace(":","",$kW);
  137. if(count($ArrayWhere)==1){
  138. $strW .= $kn."=".$kW;
  139. }else{

  140. $strW .= $kn."=".$kW." and ";
  141. }
  142. }
  143. if(count($ArrayWhere)>1){
  144. $strW .= " 1=1 ";
  145. }
  146. }
  147. if(!empty($ order)){
  148. $order="order by ".$order; }
  149. if(!empty($limit)){
  150. $limit="limit ".$limit;
  151. }
  152. //select 欄位清單 from 表名 where 條件 order by 欄位 desc|asc limit start,length;
  153. $sql="select {$field} 從 {$this->tableName} {$strW} {$order} {$limit}";
  154. //print_r($sql);
  155. $pdoS=$this->pOb->prepare($sql);
  156. if(!empty($ArrayWhere)){
  157. foreach($ArrayWhere as $kW=>$vW){
  158. $kn=str_replace(":","",$kW);
  159. $$kn=$vW;
  160. if(is_int($vW)){
  161. $pdoS->bindParam($kW,$$kn,PDO::PARAM_INT);
  162. }else if(is_float($vW)){
  163. $pdoS->bindParam($kW,$$kn,PDO::PARAM_INT);
  164. }else{
  165. $pdoS->bindParam($kW,$$kn,PDO::PARAM_STR);
  166. }
  167. }
  168. }
  169. $re=$pdoS->execute();
  170. if($re){
  171. $pdoS->setFetchMode(PDO::FETCH_ASSOC);
  172. 回傳$pdoS->fetchAll();
  173. }else {
  174. 回傳 false;
  175. }
  176. }
  177. }
複製程式碼


來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板