Glenn
Glenn

Reputation: 79

Is there a way to add current date to a received JSON in php?

I have an andriod app sending data in JSON to a php script and appending it to a mysql table, is there any way to insert the current date without adding it to the app. I am trying to get php to add the current date to BoughtDate inside the same query.

<?php
error_reporting(E_ALL); ini_set('display_errors',1);

include('../htconfig/dbConfig.php'); 
$dbSuccess = false;
        $dbConnected = mysql_connect($db['hostname'],$db['username'],$db['password']);

        if ($dbConnected) {     
            $dbSelected = mysql_select_db($db['database'],$dbConnected);
            if ($dbSelected) {
                $dbSuccess = true;
            }   
        }


$data_back = json_decode(file_get_contents('php://input'));


$userName = $data_back->{"User"};
$password = $data_back->{"Pword"};
$BoughtFrom = $data_back->{"Bfrom"};
$VIN = $data_back->{"VIN"};
$Color = $data_back->{"Color"};
$MakeIndex = $data_back->{"Make"};
$ModelIndex= $data_back->{"Model"};
$Year = $data_back->{"Year"};
$Bid = $data_back->{"Bid"};
$INV = $data_back->{"PurchasePrice"};
$BidContact = $data_back->{"Contact"};
$BidEmail= $data_back->{"Email"};


mysql_query("Insert Into tblinventory (VIN, MakeIndex, ModelIndex, Year, Color, Bid, INV, 
BidContact, BidEmail, BoughtFrom, BoughtDate)
VALUES ('$VIN','$MakeIndex', '$ModelIndex', '$Year', '$Color', '$Bid', 
'$INV', '$BidContact', '$BidEmail', '$BoughtFrom', '$BoughtDate'") or die (mysql_error());



$responses = array();
 {
    $responses[] = array( $VIN . " " . $MakeIndex . " " . $ModelIndex." ". $Year." " );
}


header("Content-type: application/json");


echo json_encode($responses);
?>

Upvotes: 0

Views: 543

Answers (3)

user924016
user924016

Reputation:

There are a couple of things, that needs your attention.

First the $data_back->{"bar"}, is a unnecessary syntax. Instead use $data_back->bar. (usefull comments below answer on this subject) Also the " is for string parsing, you can replace it with a ' as long as it is only a string without variables.

Secondly the mysql_connect, mysql_select_db and mysql_query functions are deprecated (check the php manual and the big warning box in each of these function).

The query is vulnerable to SQL injection (serious security issue). Instead use mysqli or pdo and prepared statements. You can find tons of resources on this subject on google.

See @gview answer for info on inserting the date in the column, make sure the column type matches.

Upvotes: 1

Fabian S.
Fabian S.

Reputation: 2529

As first the var $BoughtDate is not existent in your current code.

You can fill $BoughtDate by using php function time wich returns the current unix timestamp.

See http://php.net/time for further explanation.

Code Snipped:

$BoughtDate=time();

Upvotes: 3

gview
gview

Reputation: 15371

Let mysql do that for you using CURDATE. See this article for more details.

Upvotes: 1

Related Questions