Php anti-SQL injection class (phppdo prevents SQL injection class)

Source: Internet
Author: User
Php anti-SQL injection class (phppdo prevents SQL injection class)

  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: add
  11. * Parameter: array $ arr exp: array ('field name' => value, 'Field name' => value ,....)
  12. * Return: int | false
  13. */
  14. Function add ($ arr ){
  15. // Spell SQL statement
  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) {// Added successfully
  36. // Return 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: 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 ($ re ){
  73. Return true;
  74. } Else {
  75. Return false;
  76. }
  77. }
  78. Function update ($ arrSet, $ arrWhere ){
  79. // Spell SQL statement
  80. $ Str = "";
  81. $ N = 0;
  82. Foreach ($ arrSet as $ kS =>$ ){

  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 =>$ ){

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

  103. $ KS = $;

  104. If (is_int ($ )){

  105. $ PdoS-> bindParam ($ kS, $ kS, PDO: PARAM_INT );
  106. } Else if (is_float ($ )){
  107. $ PdoS-> bindParam ($ kS, $ kS, PDO: PARAM_INT );
  108. } Else {
  109. $ PdoS-> bindParam ($ kS, $ kS, PDO: PARAM_STR );
  110. }
  111. }

  112. Foreach ($ arrWhere as $ kW => $ vW ){
  113. $ Kn = str_replace (":", "", $ kW );
  114. $ Kn = $ vW; // $ p0 $ p1 $ p2
  115. If (is_int ($ vW )){
  116. $ PdoS-> bindParam ($ kW, $ kn, PDO: PARAM_INT );
  117. } Else if (is_float ($ vW )){
  118. $ PdoS-> bindParam ($ kW, $ kn, PDO: PARAM_INT );
  119. } Else {
  120. $ PdoS-> bindParam ($ kW, $ kn, PDO: PARAM_STR );
  121. }
  122. }
  123. $ Re = $ pdoS-> execute ();
  124. If ($ re ){
  125. Return true;

  126. } Else {

  127. Return false;
  128. }

  129. }

  130. // Query
  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: 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. Return $ pdoS-> fetchAll ();
  173. } Else {
  174. Return false;
  175. }
  176. }
  177. }

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.