Guest User

Untitled

a guest
May 18th, 2017
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. <?php
  2.  
  3. $user = "root";
  4. $pass = "";
  5. $db = "expenses";
  6. $host = "localhost";
  7.  
  8. mysql_connect($host, $user, $pass);
  9. mysql_select_db($db);
  10.  
  11. // $carArray will have data in array format
  12. // Array( "name" => "Suzuki", "make" => "Suzuki")
  13. $carArray = json_decode(file_get_contents('php://input'), true);
  14.  
  15. // To save this in db, make sure column names in table are similar to array keys
  16. // otherwise it will fail with an error
  17. $status = DB::Save("table_name", $carArray);
  18.  
  19. //print_r($carArray); exit;
  20.  
  21. /*$company_data = array(
  22. "company_name" => dbSafe($_POST['company_name']),
  23. "company_field" => dbSafe($_POST['company_field']),
  24. "company_city" => dbSafe($_POST['company_city']),
  25. "contact_name" => dbSafe($_POST['contact_name']),
  26. "contact_position" => dbSafe($_POST['contact_position']),
  27. "contact_phone" => dbSafe($_POST['contact_phone']),
  28. "contact_mobile" => dbSafe($_POST['contact_mobile']),
  29. "contact_email" => dbSafe($_POST['contact_email']),
  30. "company_added" => date("Y-m-d H:i:s"),
  31. "company_added_by" => dbSafe($_POST['company_added_by'])
  32. );
  33. $status = DB::Save("Companies", $company_data);*/
  34.  
  35. function dbSafe($str) {
  36. return mysql_real_escape_string($str);
  37. }
  38.  
  39. class DB {
  40. public static function Get($qry, $page, $pp = 0) {
  41. global $per_page;
  42.  
  43. if($pp==0) {
  44. $pp = $per_page;
  45. }
  46. if($page != 0) {
  47. $offset = ($page - 1) * $per_page;
  48.  
  49. $qry .= " LIMIT $offset, $pp";
  50. }
  51.  
  52. $res = mysql_query($qry);
  53.  
  54. if(DEBUG) {
  55. echo mysql_error();
  56. }
  57.  
  58. $output = array();
  59.  
  60. if(mysql_num_rows($res)>0) {
  61. while($row = mysql_fetch_assoc($res)) {
  62. $output[] = $row;
  63. }
  64. }
  65.  
  66. return $output;
  67. }
  68.  
  69. public static function Save($table, $data_array, $where = "") {
  70. if($where == "") {
  71. return DB::insert($table, $data_array);
  72. } else {
  73. return DB::update($table, $data_array, $where);
  74. }
  75. }
  76.  
  77. public static function insert($table, $data_array) {
  78. $qry = "INSERT INTO $table (";
  79.  
  80. $delimit = "";
  81. $val = " VALUES (";
  82. foreach( $data_array as $col => $value ) {
  83. $qry .= "{$delimit} {$col}";
  84. $val .= "{$delimit} '{$value}'";
  85.  
  86. $delimit = ",";
  87. }
  88.  
  89. $qry .= ")";
  90. $val .= ")";
  91.  
  92. $qry .= $val;
  93.  
  94. mysql_query($qry);
  95.  
  96. // if (DEBUG) {
  97. // echo mysql_error();
  98. // }
  99.  
  100. return mysql_insert_id();
  101. }
  102.  
  103. public static function update( $table, $data_array, $where ) {
  104. if ( count($data_array) < 1 )
  105. return false;
  106.  
  107. $qry = "UPDATE $table SET ";
  108.  
  109. $delimit = "";
  110. foreach( $data_array as $key => $val ) {
  111. $qry .= $delimit . "`" . $key . "` = '$val' ";
  112. $delimit = ",";
  113. }
  114.  
  115. if ( $where != "" ) {
  116. $qry = $qry . " WHERE " . $where;
  117. }
  118.  
  119. mysql_query($qry);
  120.  
  121. if ( DEBUG ) {
  122. echo mysql_error();
  123. }
  124.  
  125. return true;
  126. }
  127. }
Add Comment
Please, Sign In to add comment