I would expect something like this:
<?php
header('Content-type: text/plain; charset=utf-8');
$message = "failed";
if (isset($_GET['name'], $_GET['cdsId'], $_GET['code']) && is_array($_GET['code'])) {
$db_conn = new PDO('mysql:host=localhost;dbname=xxx;charset=utf8','xxx','xxx');
$db_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$name = htmlspecialchars($_GET['name']);
$cdsid = htmlspecialchars($_GET['cdsId']);
$statement = $db_conn->prepare('INSERT INTO Car(`Name`,`CDSID`,`Code`,`DateTime`,`ProcessedFlag`) VALUES (:name,:cdsid,:code,now(),False)');
$statement->bindParam(':name', $name);
$statement->bindParam(':cdsid', $cdsid);
$statement->bindParam(':code', $code);
foreach ($_GET['code'] as $obj) {
if (!empty($obj['direction']) && !empty($obj['duration'])) {
$code = $obj['direction'] . $obj['duration'];
$statement->execute();
}
}
$message = "success";
}
echo utf8_encode($message);
Notice that I did lots of checking: you cannot be absolutely sure that the app is sending data in the expected format, so you should check that it does. If it does not, then your "failed" error message comes in handy.
Since we know $_GET['code'] is an array in the expected format, we can just loop over its elements, which I did with a foreach loop.
EDIT:
It looks like you have repeating fields as you insert the same Name and CDSID again and again into the same table. Maybe you should do some database normalisation.
Also, consider not using htmlspecialchars on the name and CDSID. You should use them when printing them in a HTML document, but not when storing in the database, unless you have special reasons for doing so. The prepared statement would already handle the problem of SQL injection.