PHP exercise to read or write database values
Instead of using a big stack of if/elseif, since you're just checking the value of a single variable you should be using a switch/case instead.
What if action and mykey are both set, but action is neither read nor write - Your user will get a blank response.. yet another reason to use a switch (with a default option).
Don't use real_escape_string, use prepared statements instead.
Don't SELECT * ... if you only need the value column, just select value ...
I would suggest creating a function to handle errors.
I would suggest holding your errors in an array and joining them rather than concatting to a string - this will be more efficient and manageable.
There is no reason to use LOCK_EX in conjunction with FILE_APPEND - If you lock the file and the script is called simultaneously one instance will be blocked or denied while the other is writing. See here for more info.
Is this not supposed to be an API? If it is you might consider outputting JSON instead of print_ring the results. I would suggest putting this in an output function as well.




include("opendb.php");
$logging = true;
$log = array(date('d-m-Y'));
$log[] = serialize($_REQUEST)

$reply = array("id" =&gt; null, "status" =&gt; 'success');

if(!isset($_REQUEST["mykey"])) error("No key provided.");
$action = empty($_REQUEST["action"]) ? "" : strtolower($_REQUEST["action"])

switch($action){

    case "read":
        if(!isset($_REQUEST["id"])) error('No ID provided.');
        $stmt = $mysqli-&gt;prepare("SELECT value FROM data WHERE mykey = ? AND id = ?");
        $stmt-&gt;bind_param('si', $_REQUEST["mykey"], $_REQUEST["id"]);
        $stmt-&gt;execute();
        $stmt-&gt;bind_result($value);
        if(!$stmt-&gt;fetch()) error("No results found.");
        $reply["id"] = $_REQUEST["id"];
        $reply["value"] = $value;
        output();

    case "write":
        if(!isset($_REQUEST["value"])) error('No value provided.');
        $stmt = $mysqli-&gt;prepare(INSERT INTO `data` (`value`, `mykey`) VALUES (?, ?));
        $stmt-&gt;bind_param('ss', $_REQUEST["value"], $_REQUEST["mykey"]);
        if(!$stmt-&gt;execute()) error($mysqli-&gt;error);
        $reply["id"] = $mysqli-&gt;insert_id;
        output();

    default: error('Invalid action.');
}

function error($msg){
    $GLOBALS['log'][] = $msg;
    $GLOBALS['reply']['status'] = 'error';
    $GLOBALS['reply']['error'] = $msg;
    output();
}

function output(){
    if($GLOBALS['logging']){
        file_put_contents('log/read_write_exercise.log',  implode(PHP_EOL, $GLOBALS['log']), FILE_APPEND) ;
    }
    header('Content-Type: application/json');
    echo json_encode($GLOBALS['reply'], JSON_PRETTY_PRINT);
    exit;
}