1

So I have a php script that search data in a mysql database, but for the search I would like to put parameters. Parameters that comes from the android app through POST

<?php 
$parametre=$_POST['parametre'];
$ordre=$_POST['ordre'];
$donneecherchee=$_POST['donneerecherchee'];
$auteur=$_POST['auteur'];
$recenseur=$_POST['recenseur'];

//database constants
define('DB_HOST', 'xxx.xxx.xxx.xxx');
define('DB_USER', 'xxxx');
define('DB_PASS', 'xxxxxxxx');
define('DB_NAME', 'xxxxxxx');
// Connect to the MySQL database

//connecting to database and getting the connection object
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

//Checking if any error occured while connecting
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
die();
}

if ($parametre != null && $ordre != null && $auteur != null && $donneecherchee != null) {
    // echo(" Les paramatres sont:".$parametre.",".$ordre.",".$auteur.",".$donneecherchee.",".$recenseur);
    if ($auteur == "Mes recensements") {
        $queryused = "SELECT id, nom, dateprise, cin, observation, recenseur, usedurl FROM tsena WHERE " . $parametre . " = '" . $donneecherchee . "' AND recenseur = '" . $recenseur . "' ORDER BY id " . $ordre.";";
        $executedquery = $conn->prepare($queryused);
        $executedquery->execute();

        $executedquery->bind_result($id, $nom, $dateprise, $cin, $observation, $recenseur, $usedurl);

        $tsena = array();

        // traversing through all the result
        while ($executedquery->fetch()) {
            $temp = array();
            $temp['id'] = $id;
            $temp['nom'] = $nom;
            $temp['dateprise'] = $dateprise;
            $temp['cin'] = $cin;
            $temp['observation'] = $observation;
            $temp['recenseur'] = $recenseur;
            $temp['usedurl'] = $usedurl;
            array_push($tsena, $temp);
        }
        // echo the result as JSON
        echo json_encode($tsena);
    } else {
        $queryused2 = "SELECT * FROM tsena WHERE " . $parametre . " = '" . $donneecherchee . "' ORDER BY id " . $ordre .";";
        $executedquery2 = $conn->prepare($queryused2);
        $executedquery2->execute();

        $executedquery2->bind_result($id, $nom, $dateprise, $cin, $observation, $recenseur, $usedurl);

        $tsena2 = array();

        // traversing through all the result
        while ($executedquery2->fetch()) {
            $temp = array();
            $temp['id'] = $id;
            $temp['nom'] = $nom;
            $temp['dateprise'] = $dateprise;
            $temp['cin'] = $cin;
            $temp['observation'] = $observation;
            $temp['recenseur'] = $recenseur;
            $temp['usedurl'] = $usedurl;
            array_push($tsena2, $temp);
        }

        // echo the result as JSON
        echo json_encode($tsena2);
    }
}else{
    $data = [
        "id" => 0,
        "nom" => "Aucun",
        "dateprise" => "Aucune date",
        "cin" => "Aucun CIN",
        "observation" => "Aucune observation",
        "recenseur" => "Aucun recenseur",
        "usedurl" => "Aucune image"
    ];
    // Output the JSON data
    echo json_encode([$data]);
}

?>

I would like to get the json returned and display them in my app but it will require me to create another StringRequest with GET, doing so will create a script with empty parameters.

Here is what the Android part look like:

`private void loadProducts(String parametre, String ordre, String donneerecherchee,String auteur,String recenseur) { StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_tsena, new Response.Listener() { @Override public void onResponse(String response) { try { JSONArray array = new JSONArray(response); System.out.println("Reponse du serveur:"+response);

                        //traversing through all the object
                        for (int i = 0; i < array.length(); i++) {

                            JSONObject product = array.getJSONObject(i);

                            //adding the product to product list
                            tsenaList.add(new Tsena(
                                    product.getInt("id"),
                                    product.getString("nom"),
                                    product.getString("cin"),
                                    product.getString("dateprise"),
                                    product.getString("observation"),
                                    product.getString("recenseur")
                            ));
                        }

                        //creating adapter object and setting it to recyclerview
                        TsenaAdapter adapter = new TsenaAdapter(ConsultTsena.this, tsenaList);
                        recyclerView.setAdapter(adapter);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(ConsultTsena.this,"Erreur de load:"+error,Toast.LENGTH_LONG).show();
                    Log.e("Volley Erreur", error.toString());
                }
            }){
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<>();
            params.put("parametre", parametre);
            params.put("ordre", ordre);
            params.put("donneerecherchee",donneerecherchee);
            params.put("auteur",auteur);
            return params;  // Set the parameters for the request
        }
    };

    //adding our stringrequest to queue
    Volley.newRequestQueue(this).add(stringRequest);
}`

What can I do?

I tested:

  • to see if the parameters where passed
  • if the query was incorrect;

What I have in my logcat is an empty json [], which should not be case, as I executed the query in Powershell and it returned something.

1
  • If you have a two consecutive requests they have nothing to do with each other. So act normal: POST your parameters to the outputstream then read the json from the inputStream. Commented Jan 31, 2024 at 14:03

1 Answer 1

0

Sorry for bothering you all, to solve the problem I just put the parameters as GET and I shoot them trough the url string in the android part. It sure works but is not very secure

Sign up to request clarification or add additional context in comments.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.