PHP Anti-SQL injection class (PHP PDO prevents SQL injection classes)

Source: Internet
Author: User
  1. Class model{

  2. protected $tableName = "";//Table name
  3. protected $pOb;//pdo class object
  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. * Function: Increase
  11. * Parameters: Array $arr exp:array (' field name ' = = value, ' field name ' = = value,....)
  12. * Return:int|false
  13. */
  14. function Add ($arr) {
  15. Spelling SQL statements
  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::P aram_str);

  33. }

  34. $re = Execute () $pdoS;
  35. if ($re) {//Added successfully
  36. Returns the primary Key ID value
  37. return $this->pob->lastinsertid ();
  38. }
  39. return value
  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::P aram_int);
  65. }else if (is_float ($vW)) {
  66. $pdoS->bindparam ($kW, $ $kn, PDO::P aram_int);
  67. }else{
  68. $pdoS->bindparam ($kW, $ $kn, PDO::P aram_str);
  69. }
  70. }
  71. $re = $pdoS->execute ();
  72. if ($re) {
  73. return true;
  74. }else {
  75. return false;
  76. }
  77. }
  78. function Update ($arrSet, $arrWhere) {
  79. Spelling SQL statements
  80. $str = "";
  81. $n = 0;
  82. foreach ($arrSet as $kS = = $vS) {

  83. $str. = ",". $kS. " =:p ". $n + +;

  84. }
  85. $str = substr ($str, 1);
  86. foreach ($arrWhere as $kW = = $vW) {
  87. $kn =str_replace (":", "", $kW);
  88. if (count ($arrWhere) ==1) {
  89. $strW. = $kn. " = ". $kW;
  90. }else{
  91. $strW. = $kn. " = ". $kW." and ";
  92. }
  93. }
  94. if (count ($arrWhere) >1) {
  95. $strW. = "1=1";
  96. }

  97. $sql = "Update {$this->tablename} set {$str} where". $strW;

  98. Print_r ($sql);

  99. $pdoS = $this->pob->prepare ($sql);

  100. $x = 0;
  101. foreach ($arrSet as $kS = = $vS) {

  102. $kS = ":p". $x + +;

  103. $ $kS = $vS;

  104. if (Is_int ($vS)) {

  105. $pdoS->bindparam ($kS, $ $kS, PDO::P aram_int);
  106. }else if (is_float ($vS)) {
  107. $pdoS->bindparam ($kS, $ $kS, PDO::P aram_int);
  108. }else{
  109. $pdoS->bindparam ($kS, $ $kS, PDO::P aram_str);
  110. }
  111. }

  112. foreach ($arrWhere as $kW = = $vW) {
  113. $kn =str_replace (":", "", $kW);
  114. $ $kn = $vW;//$p 0 $p 1 $p 2
  115. if (Is_int ($vW)) {
  116. $pdoS->bindparam ($kW, $ $kn, PDO::P aram_int);
  117. }else if (is_float ($vW)) {
  118. $pdoS->bindparam ($kW, $ $kn, PDO::P aram_int);
  119. }else{
  120. $pdoS->bindparam ($kW, $ $kn, PDO::P aram_str);
  121. }
  122. }
  123. $re = $pdoS->execute ();
  124. if ($re) {
  125. return true;

  126. }else{

  127. return false;
  128. }

  129. }

  130. Check
  131. function Select ($field = "*", $ArrayWhere = "", $order = "", $limit = "") {
  132. if (!empty ($ArrayWhere)) {
  133. $strW = "where";
  134. foreach ($ArrayWhere as $kW = = $vW) {
  135. $kn =str_replace (":", "", $kW);
  136. if (count ($ArrayWhere) ==1) {
  137. $strW. = $kn. " = ". $kW;

  138. }else{

  139. $strW. = $kn. " = ". $kW." and ";
  140. }
  141. }
  142. if (count ($ArrayWhere) >1) {
  143. $strW. = "1=1";
  144. }
  145. }
  146. if (!empty ($order)) {
  147. $order = "ORDER by". $order;
  148. }
  149. if (!empty ($limit)) {
  150. $limit = "Limit". $limit;
  151. }
  152. Select field List from table name where Condition order by field DESC|ASC limit start,length;
  153. $sql = "Select {$field} from {$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::P aram_int);
  162. }else if (is_float ($vW)) {
  163. $pdoS->bindparam ($kW, $ $kn, PDO::P aram_int);
  164. }else{
  165. $pdoS->bindparam ($kW, $ $kn, PDO::P aram_str);
  166. }
  167. }
  168. }
  169. $re = $pdoS->execute ();
  170. if ($re) {
  171. $pdoS->setfetchmode (PDO::FETCH_ASSOC);
  172. return $pdoS->fetchall ();
  173. }else {
  174. return false;
  175. }
  176. }
  177. }

Copy Code
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.