-2

With this code, I am getting only local correct value with variable $date in MySQL column, because PHP gives me time so it must be correct. What should I change to save JSON post to PHP variables and correctly send it to SQL columns?

It's my JSON call:

{  
   "buyer":{  
      "id":142694,
      "email":"test@example.com",
      "quantity":1,
      "sent_count":0,
      "created_at":"2018-12-29T20:29:23+01:00"
   },
   "listing":{  
      "id":36,
      "name":"GTA V STEAM KEY"
   },
   "database":{  
      "id":16141,
      "available_codes_count":7
   },
   "payment":{  
      "id":null,
      "code":null,
      "amount":null,
      "currency":null,
      "done":false
   },
   "created_at":"2018-12-29T20:29:23+01:00"
}
<?php
$json = json_decode($_POST);

$email = var_dump($json->buyer->email);
$name = var_dump($json->listing->name);
$count = var_dump($json->buyer->quantity);
$code = var_dump($json->payment->code);

date_default_timezone_set('Europe/Warsaw');
$date = date("Y-m-d H:i:s", time());

$servername = "localhost";
$username = "test1";
$password = "test2";
$dbname = "test3";

$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "INSERT INTO list (email, name, date, count, code)
VALUES ('".$email."', '".$name."', '".$date."', '".$count."', '".$code."')";

if ($conn->query($sql) === true) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • What jumps out is the nonsensical bit where you get current time. Just do `date("Y-m-d H:i:s", time());` - or `date("Y-m-d H:i:s");`No need for all that back and forth. Are you getting a specific error when trying to insert? – ficuscr Jul 03 '19 at 20:53
  • Fast copied code from other subpage on my website, thx for info. Then, I had must used it to * minutes ;) – Paweł Smacki Jul 03 '19 at 20:57
  • Assume issue is with that string mess of an insert statement. Hard to help - I don't know the schema of the table. Assume `date` is a datetime or timestamp? But for all i know its a float... Be sure to check your logs. – ficuscr Jul 03 '19 at 21:01
  • Leave `date` alone. Problem is on json side. It can't be decoded or I doing something wrong to get variables... – Paweł Smacki Jul 03 '19 at 21:02
  • 1
    Not sure I'm following... `var_dump(date('Y-m-d H:i:s', strtotime($json->buyer->created_at)));` – ficuscr Jul 03 '19 at 21:06
  • 1
    `var_dump` does not return any values. – Dharman Jul 03 '19 at 21:06
  • @Dharman so how I can get data from json in this case ? – Paweł Smacki Jul 03 '19 at 21:07
  • @Dharman thanks for pointing that out. I wasn't noticing that and didn't get your meaning at first. – ficuscr Jul 03 '19 at 21:17

2 Answers2

0

Feel like we are failing to communicate. This is a fully functional, minimal, example of what I think you are asking.

<?php
$json = '{  
   "buyer":{  
      "id":142694,
      "email":"test@example.com",
      "quantity":1,
      "sent_count":0,
      "created_at":"2018-12-29T20:29:23+01:00"
   },
   "listing":{  
      "id":36,
      "name":"GTA V STEAM KEY"
   },
   "database":{  
      "id":16141,
      "available_codes_count":7
   },
   "payment":{  
      "id":null,
      "code":null,
      "amount":null,
      "currency":null,
      "done":false
   },
   "created_at":"2018-12-29T20:29:23+01:00"
}';

$j = json_decode($json);

$someDate = date('Y-m-d H:i:s', strtotime($j->buyer->created_at));

UPDATE: OK, think I am better understanding your question now. Specific to the comments and understanding what var_dump does., try this and understand it!:

<?php
$a = ['hello World'];
$b = var_dump($a); //this sends output, not what you want to do for an 'assignment'.
$c = $a;

var_dump($a); //desired results

var_dump($b); //null

Now, to your specifics...

<?php
$json = json_decode($_POST['myJSON']);
// Question #1 - does this return an object? This is where you could use var_dump($json); to check.
//If it does...
$email = $json->buyer->email;
$name = $json->listing->name;
$date = date('Y-m-d H:i:s', strtotime($j->buyer->created_at));
// etc...

Otherwise you need to update the question and actually show what you receive in the $_POST.

ficuscr
  • 6,975
  • 2
  • 32
  • 52
  • As I wrote, I need json data from $_POST. Can You help me ? I think You are intelligent. – Paweł Smacki Jul 03 '19 at 21:14
  • `$_POST` is an array. Can you use the array as input to `json_decode`? – Dharman Jul 03 '19 at 21:24
  • *\*cough\* "Otherwise you need to update the question and actually show what you receive in the $_POST"* – ficuscr Jul 03 '19 at 21:32
  • You are drunk ? Shop's API sending THIS to ME: `{ "buyer":{ "id":142694, "email":"test@example.com", "quantity":1, "sent_count":0, "created_at":"2018-12-29T20:29:23+01:00" }, "listing":{ "id":36, "name":"GTA V STEAM KEY" }, "database":{ "id":16141, "available_codes_count":7 }, "payment":{ "id":null, "code":null, "amount":null, "currency":null, "done":false }, "created_at":"2018-12-29T20:29:23+01:00" }` – Paweł Smacki Jul 03 '19 at 21:33
  • No. I'm at work. ^ "`$_POST` is an array." That is likely a value in the array that *is* your POST. If you chill for two seconds and actually read the answers I am confident a smart person like you can figure this out. – ficuscr Jul 03 '19 at 21:36
0

You should use prepared statements instead of injecting variables into SQL.

About your question, how to get JSON POST data see this post: Receive JSON POST with PHP

Here is more or less how your code should look like.

<?php

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

date_default_timezone_set('Europe/Warsaw');

// $date = date("Y-m-d H:i:s", time()); // <- not needed because MySQL has CURDATE()/NOW()
// if for any reason you would like to pass date from PHP you can uncomment the above line and add anoter parameter to SQL

$servername = "localhost";
$username = "test1";
$password = "test2";
$dbname = "test3";

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn = new mysqli($servername, $username, $password, $dbname);

$stmt = $conn->prepare('INSERT INTO list (email, name, `date`, count, code) VALUES (?,?,CURDATE(),?,?)');
$stmt->bind_param(
    'ssss',
    $json->buyer->email,
    $json->listing->name,
    $json->buyer->quantity,
    $json->payment->code
);
$stmt->execute();

echo "New record created successfully";
Dharman
  • 30,962
  • 25
  • 85
  • 135