autoInsert.class.php - Auto Insert


/ Published in: PHP
Save to your folder(s)

Add or change MySQL records based on form values


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

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.