So far I've tried a couple of methods for pulling the data from the following array that is received from JAVA(After print_r($_POST)):
{"attending":0,"eventid":1,"userid":1}
But I am having no luck, as other ways I've found on SO and around the net are all slightly different (Surrounded by ' ' and [])
My code:
print_r($_POST);
$data = json_decode($_POST);
$userid = $data['userid'];
$eventid = $data['eventid'];
$attending = $data['attending'];
My question is: how do I correctly pull the values from the Post and assign them to values?
I'm new to PHP so please no rude comments.
I have also tried:
$data = json_decode($_POST, true);
As mentioned in the comments but now I get:
Warning: json_decode() expects parameter 1 to be string, array given in C:\xampp\htdocs\attendanceradio.php on line 9
var_dump($_POST);:
Array
(
[{"attending":0,"eventid":2,"userid":1}] =>
)
array(1) {
["{"attending":0,"eventid":2,"userid":1}"]=>
string(0) ""
}
Full PHP code:
<?php
$user = 'root';
$pass = '';
$db = 'testuser';
$con=mysqli_connect('localhost', $user, $pass, $db) or die('Unable to connect');
print_r($_POST['json']);
var_dump($_POST);
$json = json_decode(trim(key($_POST), '[]'), true);
var_dump($json);
$userid = $json['userid'];
$eventid = $json['eventid'];
$attending = $json['attending'];
$statement = mysqli_prepare($con,
'INSERT INTO user_has_event(user_user_id, event_event_id, attendance)
VALUES (?, ?, ?)
ON DUPLICATE KEY UPDATE attendance = ?');
mysqli_stmt_bind_param($statement, 'iii', $userid, $eventid, $attending);
mysqli_stmt_execute($statement);
mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $userid, $eventid, $attending);
mysqli_stmt_close($statement);
mysqli_close($con);
?>