Posted By

hasantayyar on 08/14/09


Tagged

mysql php


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

hasantayyar
umang_nine


autoInsert.class.php - Auto Insert


 / Published in: PHP
 

URL: http://www.phpclasses.org/browse/file/23883.html

Add or change MySQL records based on form values

  1. <?
  2. class autoInsert{
  3. var $db_conn;
  4.  
  5. function iConnect($user, $pass, $host = 'localhost'){
  6. if ($this->db_conn = mysql_connect($host, $user, $pass)){
  7. return true;
  8. }else{
  9. return false;
  10. }
  11. }
  12.  
  13. function iSelect($db){
  14. if (mysql_select_db($db)){
  15. return true;
  16. }else{
  17. return false;
  18. }
  19. }
  20.  
  21. function iInsert($table, $postData = array()){
  22. $q = "DESC $table";
  23. $q = mysql_query($q);
  24.  
  25. $getFields = array();
  26.  
  27. while ($field = mysql_fetch_array($q)){
  28. $getFields[sizeof($getFields)] = $field['Field'];
  29. }
  30.  
  31. $fields = "";
  32. $values = "";
  33.  
  34. if (sizeof($getFields) > 0){
  35. foreach($getFields as $k){
  36. if (isset($postData[$k])){
  37. $postData[$k] = htmlspecialchars($postData[$k]);
  38.  
  39. $fields .= "`$k`, ";
  40. $values .= "'$postData[$k]', ";
  41. }
  42. }
  43.  
  44. $fields = substr($fields, 0, strlen($fields) - 2);
  45. $values = substr($values, 0, strlen($values) - 2);
  46.  
  47. $insert = "INSERT INTO $table ($fields) VALUES ($values)";
  48.  
  49. if (mysql_query($insert)){
  50. return true;
  51. }else{
  52. return false;
  53. }
  54. }else{
  55. return false;
  56. }
  57. }
  58.  
  59. function iUpdate($table, $postData = array(), $conditions = array()){
  60. $q = "DESC $table";
  61. $q = mysql_query($q);
  62.  
  63. $getFields = array();
  64.  
  65. while ($field = mysql_fetch_array($q)){
  66. $getFields[sizeof($getFields)] = $field['Field'];
  67. }
  68.  
  69. $values = "";
  70. $conds = "";
  71.  
  72. if (sizeof($getFields) > 0){
  73. foreach($getFields as $k){
  74. if (isset($postData[$k])){
  75. $postData[$k] = htmlspecialchars($postData[$k]);
  76.  
  77. $values .= "`$k` = '$postData[$k]', ";
  78. }
  79. }
  80.  
  81. $values = substr($values, 0, strlen($values) - 2);
  82.  
  83. foreach($conditions as $k => $v){
  84. $v = htmlspecialchars($v);
  85.  
  86. $conds .= "`$k` = '$v'";
  87. }
  88.  
  89. $update = "UPDATE $table SET $values WHERE $conds";
  90.  
  91. if (mysql_query($update)){
  92. return true;
  93. }else{
  94. return false;
  95. }
  96. }else{
  97. return false;
  98. }
  99. }
  100.  
  101. function iDisconnect(){
  102. if (mysql_close($this->db_conn)){
  103. return true;
  104. }else{
  105. return false;
  106. }
  107. }
  108.  
  109. }
  110. ?>
  111.  
  112.  
  113. usage
  114.  
  115. <?
  116. include('autoInsert.php');
  117. $a = new autoInsert();
  118.  
  119. $a->iConnect('root', 'password', 'localhost');
  120. $a->iSelect('database');
  121.  
  122. if (isset($_POST['insert'])){
  123. $a->iInsert('testTable', $_POST);
  124. }
  125.  
  126. if (isset($_POST['update'])){
  127. $conditions = array('field_id' => 1);
  128. $a->iUpdate('testTable', $_POST, $conditions);
  129. }
  130.  
  131. $a->iDisconnect();
  132. ?>

Report this snippet  

You need to login to post a comment.