Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $user = "root";
- $pass = "";
- $db = "expenses";
- $host = "localhost";
- mysql_connect($host, $user, $pass);
- mysql_select_db($db);
- // $carArray will have data in array format
- // Array( "name" => "Suzuki", "make" => "Suzuki")
- $carArray = json_decode(file_get_contents('php://input'), true);
- // To save this in db, make sure column names in table are similar to array keys
- // otherwise it will fail with an error
- $status = DB::Save("table_name", $carArray);
- //print_r($carArray); exit;
- /*$company_data = array(
- "company_name" => dbSafe($_POST['company_name']),
- "company_field" => dbSafe($_POST['company_field']),
- "company_city" => dbSafe($_POST['company_city']),
- "contact_name" => dbSafe($_POST['contact_name']),
- "contact_position" => dbSafe($_POST['contact_position']),
- "contact_phone" => dbSafe($_POST['contact_phone']),
- "contact_mobile" => dbSafe($_POST['contact_mobile']),
- "contact_email" => dbSafe($_POST['contact_email']),
- "company_added" => date("Y-m-d H:i:s"),
- "company_added_by" => dbSafe($_POST['company_added_by'])
- );
- $status = DB::Save("Companies", $company_data);*/
- function dbSafe($str) {
- return mysql_real_escape_string($str);
- }
- class DB {
- public static function Get($qry, $page, $pp = 0) {
- global $per_page;
- if($pp==0) {
- $pp = $per_page;
- }
- if($page != 0) {
- $offset = ($page - 1) * $per_page;
- $qry .= " LIMIT $offset, $pp";
- }
- $res = mysql_query($qry);
- if(DEBUG) {
- echo mysql_error();
- }
- $output = array();
- if(mysql_num_rows($res)>0) {
- while($row = mysql_fetch_assoc($res)) {
- $output[] = $row;
- }
- }
- return $output;
- }
- public static function Save($table, $data_array, $where = "") {
- if($where == "") {
- return DB::insert($table, $data_array);
- } else {
- return DB::update($table, $data_array, $where);
- }
- }
- public static function insert($table, $data_array) {
- $qry = "INSERT INTO $table (";
- $delimit = "";
- $val = " VALUES (";
- foreach( $data_array as $col => $value ) {
- $qry .= "{$delimit} {$col}";
- $val .= "{$delimit} '{$value}'";
- $delimit = ",";
- }
- $qry .= ")";
- $val .= ")";
- $qry .= $val;
- mysql_query($qry);
- // if (DEBUG) {
- // echo mysql_error();
- // }
- return mysql_insert_id();
- }
- public static function update( $table, $data_array, $where ) {
- if ( count($data_array) < 1 )
- return false;
- $qry = "UPDATE $table SET ";
- $delimit = "";
- foreach( $data_array as $key => $val ) {
- $qry .= $delimit . "`" . $key . "` = '$val' ";
- $delimit = ",";
- }
- if ( $where != "" ) {
- $qry = $qry . " WHERE " . $where;
- }
- mysql_query($qry);
- if ( DEBUG ) {
- echo mysql_error();
- }
- return true;
- }
- }
Add Comment
Please, Sign In to add comment