1

Earlier, I have put calculation code inside my java for my android application and it is worked like a charm. But since it is time calculation, it exposed user of application to altering the time from device setting. To prevent that, I insert the same calculation from android into my php. It works but the application keep showing "Processing.." and I have to kill the application from task manager. Here is my coding

<?php

include 'create_trans.php';//import timecheckingg
include 'update_trans.php';//mport timecheckoutt


$diff = $timecheckoutt - $timecheckingg;
$diffMinutes = $diff / (60 * 1000) % 60;
$diffHours = $diff / (60 * 60 * 1000) % 24;

// array for JSON response
$response = array();

// check for required fields
if (isset($_POST['points'])) {


$phone_num= $_POST['phone_num'];
$points = $_POST['points'];  


$diffMinfloor = floor($diff/(60*1000));
$charges = floor($diffMinfloor/2)*2;
$total = $points-$charges;


// include db connect class
require_once '../crud_transaction/db_connect.php'; 

// connecting to db
$db = new DB_CONNECT();

// mysql inserting a new row
$result = mysql_query("
UPDATE
transaction
INNER JOIN
User
INNER JOIN
transactionAdmin
ON
transaction.phone_num=User.phone_num AND 
transaction.phone_num=transactionAdmin.phone_num
SET
transaction.points = '$points',User.points='$points',transactionAdmin.points 
= '$points'
WHERE
transaction.phone_num = '$phone_num';");

// check if row inserted or not
if ($result) {
    // successfully inserted into database
    $response["success"] = 1;
    $response["message"] = "successfully updated.";

    // echoing JSON response
    echo json_encode($response);
} else {
    // failed to insert row
    $response["success"] = 0;
    $response["message"] = "Oops! An error occurred.";

    // echoing JSON response
    echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";

// echoing JSON response
echo json_encode($response);
}
?>

I'm stuck for 10 days because of this problem.Very appreciate if u guys can help me. Regards,aj

Qirel, here is my DB_CONNECT.php

db_connect.php

<?php


 class DB_CONNECT {

// constructor
function __construct() {
    // connecting to database
    $this->connect();
}

// destructor
function __destruct() {
    // closing db connection
    $this->close();
}

/**
 * Function to connect with database
 */
function connect() {
    require_once '../crud_transaction/db_config.php';

    // Connecting to mysql database
    $con = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD) or 
  die(mysql_error());

    // Selecing database
    $db = mysql_select_db(DB_DATABASE) or die(mysql_error()) or 
  die(mysql_error());

    // returing connection cursor
    return $con;
}

/**
 * Function to close db connection
 */
function close() {
    // closing db connection
    mysql_close();
}

 }

?>
Azlina T
  • 176
  • 2
  • 17
  • You should check that the query is executable. Run the query in phpMyAdmin or similar GUI. Also, check for errors, [`error_reporting(E_ALL);`](http://php.net/manual/en/function.error-reporting.php) and [`mysql_error`](http://php.net/manual/en/function.mysql-error.php). `mysql_query` also returns an object on true, boolean `false` on failure, so you can wrap it in an `if`-block to check that it actually runs the query, too. – Qirel Jan 19 '16 at 09:47
  • In addition, `mysql_*` functions are deprecated since PHP 5.5 (and removed entirely in PHP 7) and you shoud [stop using them](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) if you can. You should choose another API, like `mysqli_*` or PDO instead - see [choosing an API](http://php.net/manual/en/mysqlinfo.api.choosing.php). Because as the script is now, you might be vulnerable to [SQL-injection](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Qirel Jan 19 '16 at 09:48
  • `$db = mysql_select_db(DB_DATABASE) or die(mysql_error()) or die(mysql_error());` this also seems a bit redundant? :p – Qirel Jan 19 '16 at 09:49
  • Oh, okay..my bad..haha, done correct it.. The query is executable and it output in json since I am using for java. – Azlina T Jan 19 '16 at 09:56

1 Answers1

0

If this file is located in the site root with inner folder. other ways you can change the path of

<?php

require_once dirname(dirname(__FILE__)).'/crud_transaction/db_config.php';


 class Db_Connect {

    /**
     * Function to connect with database
     */  
    function __construct() {
       $this->connect();
    }

    /**
     * Function to connect with database
     */
    function __destruct() {
        $this->close();
    }

    /**
     * Function to connect with database
     */
    function connect() {
        $con = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD) or 
        die(mysql_error());
        $db = mysql_select_db(DB_DATABASE) or die(mysql_error()) or 
        die(mysql_error());
        return $con;
    }

    /**
     * Function to close db connection
     */
    function close() {
        mysql_close();
    }
}
?>
Qirel
  • 25,449
  • 7
  • 45
  • 62
rajwebsoft
  • 16
  • 2
  • I dont think my path of database connection is the problem since if I apply my calculation coding in the java(android), it works.. By the way, I have tried your way and it's still output the same thing, thanks for your help – Azlina T Jan 19 '16 at 09:48
  • You have to add the new function in – rajwebsoft Jan 19 '16 at 10:18