-1
<div id='post' style='display: none;'>
    <?php
    class User {
        public $id;
        public $counter;
        public $removed;
    }
    $dB = json_decode(file_get_contents('dataBase.json'), true);
    $dataBase = $dB['noob'][0];
    $userInDB = null;
    $user = new User();
    $user->id = (int)$_POST['id'];
    $user->counter = (int)$_POST['counter'];
    $user->removed = (bool)$_POST['removed'];
    foreach ($dataBase as $usr) {
        if ($usr['id'] == $user->id) {
            $userInDB = $usr
            break;
        }
    }
    if ($userInDB) {
        $userInDB[counter] = $userInDB[counter] + $user->counter
        $userInDB[removed] = $user->removed
    } else {
        array_push($dataBase, $user);
    }
    if(isset($_POST['id'])) { 
        $json = json_encode($user);
        echo $json;
    }
    $updateddB = json_encode($dB);
    file_put_contents('dataBase.json', $updateddB);
    ?>
</div>
<div id='get' style='display: none;'>
    <?php
    $id = (int)$_GET['id']
    $type = htmlentities($_GET['type'], ENT_QUOTES, 'UTF-8');
    $dB = json_decode(file_get_contents('dataBase.json'), TRUE);
    $dataBase = $dB['noob'][0];
    $response = FALSE
    foreach ($dataBase as $usr) {
        if ($type == 'id') {
            if ($usr['id'] == $user->id) {
                $response = TRUE
                break;
            }
        } else if ($type == 'counter') {
            if ($user['counter'] >= 1) {
                $response = TRUE
                break;
        } else if ($type == 'removed') {
            if ($user[id]) {
                $response = TRUE
                break;
            }
        }
    }
    echo $response;
    ?>
</div>

I don't know why I get a 500 error, so far it hasn't errored when I remove everything but the echo. I tried removing just the part with the json and the iterating but that still didn't do anything. I also tried just remove the post section of the php, nothing. I tried removing everything but the echo and the assignment to false statement, and yet, it didn't work! It may as well just be an IIS problem.

I tried using a different browser being Firefox, and it displayed nothing at all.

Help! This is the json in case that helps.

{
    "noob": [{
            "id": 593286196,
            "counter": 0,
            "removed": true
        },
        {
            "id": 256169534,
            "counter": 0,
            "removed": true
        },
        {
            "id": 516105343,
            "counter": 0,
            "removed": true
        },
        {
            "id": 520887461,
            "counter": 0,
            "removed": true
        },
        {
            "id": 162491701,
            "counter": 0,
            "removed": true
        },
        {
            "id": 352289153,
            "counter": 0,
            "removed": true
        },
        {
            "id": 469971590,
            "counter": 0,
            "removed": true
        },
        {
            "id": 83728091,
            "counter": 0,
            "removed": true
        },
        {
            "id": 34172132,
            "counter": 0,
            "removed": true
        },
        {
            "id": 35349812,
            "counter": 0,
            "removed": true
        }
    ],
    "igno": []
}

1 Answers1

0

JSON string literals must use normal double quote characters (""), not smart quotes (“”) or single quote ('').

Try:

{
"noob":[
    {
        "id": 593286196, 
        "counter": 0, 
        "removed": true
    },
    {
        "id": 256169534,
        "counter": 0,
        "removed": true
    },
    {
        "id": 516105343,
        "counter": 0,
        "removed": true
    },
    {
        "id": 520887461,
        "counter": 0,
        "removed": true
    },
    {
        "id": 162491701,
        "counter": 0,
        "removed": true
    },
    {
        "id": 352289153,
        "counter": 0,
        "removed": true
    },
    {
        "id": 469971590,
        "counter": 0,
        "removed": true
    },
    {
        "id": 83728091,
        "counter": 0,
        "removed": true
    },
    {
        "id": 34172132,
        "counter": 0,
        "removed": true
    },
    {
        "id": 35349812,
        "counter": 0,
        "removed": true
    }
],
    "igno":[
    ]
  }
caiovisk
  • 3,667
  • 1
  • 12
  • 18