3
\$\begingroup\$

I wrote this code for Android server interaction. The way I built the client was an onSharePreference of strings includes photo encoded to a base64 String object {imei, barcode0, barcode1, photo0, photo1, time_taken0, time_taken1,..} because I had the possibility the user won't be connected to the internet all the time. Then I made a working code to intercept it.

Is it good enough? Is it a good enough code or it has a problem in it? I think it is really straightforward but the server-side guys have had some problems replicating this script in ASP.

<?php

/*
* Following code will create a new product row
* All product details are read from HTTP Post Request
*/

// array for JSON response
$response = array();

// check for required fields


header('Content-Type: image/jpg; charset=utf-8');
//$base=$_REQUEST['image'];


if ((isset($_POST['imei'])))
{

$counter = $_POST['brcd_counter'];
$imei = $_POST['imei'];            //isset($_POST['imei']) ? $_POST['imei'] : 'NULL';

 //mysqli connect - object oriented style (recommended)
    $mysqli = new mysqli('','','','');
    $mysqli->set_charset("utf8");


    //Output any connection error
    if ($mysqli->connect_error) {
            die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
    }

    // start transaction
 $mysqli->autocommit(FALSE);



for ($i = 0; $i<=$counter; $i++){

    $qr_code[$i] = $_POST['qr_code'.$i];           //isset($_POST['qr_code']) ? $_POST['qr_code'] : 'NULL';
    $time_stamp[$i] = $_POST['time_stamp'.$i];     //isset($_POST['timestamp']) ? $_POST['timestamp'] : 'NULL';
    $photo[$i] =  $_POST['photo'.$i];              //isset($_POST['car_type']) ? $_POST['car_type'] : 'NULL';
    //values to be inserted in database table (for clarity)
    $client_imei = $imei;
    $client_qr_code = $qr_code[$i];
    $client_time_stamp = $time_stamp[$i];
    $client_photo = $photo[$i];   //'DRAGON' .. for chekcing
    $query = "INSERT INTO messages (imei, qr_code , time_stamp, base64image ) VALUES(?,?,?,?)";
    $statement = $mysqli->prepare($query);

    //bind parameters for markers, where (s = string, i = integer, d = double,  b = blob)
    $statement->bind_param('ssss', $client_imei, $client_qr_code, $client_time_stamp, $client_photo);
    $statement->execute();
}




  if ($mysqli->commit()) {
    // successfully inserted into database

            $response["success"] = 1;
            $response["message"] = "Product successfully created.";

            // echoing JSON response
            echo json_encode($response);
     } else {
    // failed to insert row
    $response["success"] = 0;
    $response["message"] = "Oops! An error occurred.";

    // echoing JSON response
    echo json_encode($response);
}

$mysqli->close();}else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Name is missing";

// echoing JSON response
echo json_encode($response);



}
\$\endgroup\$

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.