0

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);
?>
kmil
  • 243
  • 3
  • 13
  • 2
    If you want to use the decoded value as an array you'll need to do : `json_decode($_POST, true)` – DarkBee Mar 27 '16 at 21:15
  • @PaulCrovella I dont think that it is a duplicate as the answers to that question is what I already have in my code. I'm clearly having a different problem. – kmil Mar 27 '16 at 21:18
  • @PaulCrovella I've already read that page before posting my question, else I wouldn't have asked. I mentioned that I was new to PHP, Could you please be more constructive? – kmil Mar 27 '16 at 21:22
  • @DarkBee could you please check my edit? – kmil Mar 27 '16 at 21:24
  • 1
    could you make a var_dump($_POST); and tell me the result? I will help you – Kangoo13 Mar 27 '16 at 21:26
  • @Kangoo13 Check the edit – kmil Mar 27 '16 at 21:30
  • `$_POST` is an array. `json_decode` expects a string, I guess. Access the array via `$_POST["data"]` or whatever the json-part of the post data is called. Try `$data = json_decode($_POST["data"]);` – jDo Mar 27 '16 at 21:32
  • As @jDo said it's an array that's why I wanted you to see with var_dump you can see from which index of the array the value is in. – Kangoo13 Mar 27 '16 at 21:33
  • Possible duplicate of [Reading JSON POST using PHP](http://stackoverflow.com/questions/19004783/reading-json-post-using-php) – Maks3w Mar 27 '16 at 21:33
  • It looks like the JSON string is the *key* of the `$_POST` array element, instead of the value?? – Mark Reed Mar 27 '16 at 21:45

2 Answers2

1

Based on your data something like the following should work?

<?php

$json = json_decode(trim(key($_POST), '[]'), true);

var_dump($json);

?>
  • That could have done it, Just having problems with bind variables now. Once that is sorted I can be sure: `Number of bind variables doesn't match number of fields in prepared statement`. I posted my full code in case you wanted to take a look at it. – kmil Mar 27 '16 at 21:44
  • 1
    your mysqli_stmt_bind_param has an extra parameter 'iiii' not sure why that is there so you are binding 4 params rather than the 3 in the query –  Mar 27 '16 at 21:47
  • Do you mean the 4th: `ON DUPLICATE KEY UPDATE attendance = ?`? – kmil Mar 27 '16 at 21:51
  • 1
    mysqli_stmt_bind_param($statement, 'iiii', $userid, $eventid, $attending, $attending); –  Mar 27 '16 at 21:59
  • 1
    Yes you have 4 parameters defined so you have to add an extra type 'iiii' (4) and a duplicate $attending parameter, i assume you will have to do the same for your results aswell –  Mar 27 '16 at 21:59
  • Thanks for spending your time helping me out, I really appreciate it! It looks like I'm making slow progress now. Duplicating $attendance worked for the param and now I only get the warning for the result: `Warning: mysqli_stmt_bind_result(): Number of bind variables doesn't match number of fields in prepared statement in C:\xampp\htdocs\attendanceradio.php on line 27`(with 3 or 4 paramaters). Is there any way that I can know what is expected? – kmil Mar 27 '16 at 22:08
  • 1
    I forgot you are doing an insert statement which means that you are not returning any results so you dont need to bind the results if you were doing a select col1, col2, col3 from table where ... then you would need to bind the results into variables but in this case this is not required so you can just remove this line –  Mar 27 '16 at 22:13
  • Perfect, looks like its working properly now. I'll have to edit my JAVA slightly but I really appreciate your help! – kmil Mar 27 '16 at 22:17
0

The error message that Array was given instead of a string and according to the var_dump I can see that POST is returning an array.

If you want to decode the JSON string you have to point to add the array index where the string is located, so your code should look like:

$data = json_decode($_POST[0], true);

$userid = $data['userid'];
$eventid = $data['eventid'];
$attending = $data['attending'];
Juan Lago
  • 960
  • 1
  • 10
  • 20
  • With that I get `Notice: Undefined offset: 0 in C:\xampp\htdocs\attendanceradio.php on line 12`, almost ready to give PHP up all together! – kmil Mar 27 '16 at 21:57