Rodrigo Ferraz
Rodrigo Ferraz

Reputation: 9

Array to String Error in Mysql using PHP

I have an Android application that sends a Json as a string to the server in PHP. I've tested with var_dump to verify that the data was being passed correctly and everything is ok. The problem is that when I try to access json and assign values from a nested array of the main object to an array in PHP, I got an error when I try to include this array in Mysql. I've tested only PHP and MySQL and everything is working perfectly.

if  (!empty($_POST)){
   $info = file_get_contents('php://input');
   $json = json_decode($info, true);
   $login= "";
   $useExercise = array();
  //var_dump($info);

   foreach($json['Patient'][0] as $name){
      $login = $name;
   }


   foreach ($json['Patient'][1]as $exercise){
       $useExercise[] = array($exercise);
   }

for ($i=0; sizeOf($useExercise) > $i; $i++){

    $exercicies= mysqli_fetch_array($order);
    $sql1 = ("UPDATE patient_exercise
             INNER JOIN patients ON (patient_exercise.idpatient =  patients.ID)
             INNER JOIN exercises ON (exercises.idexercises  = patient_exercise.idexercise)
             SET patient_exercise.use_exercise=$useExercise[$i]     
             WHERE patient_exercise.idexercise= {$exercises['idexercise']} AND
                   patients.ID=(SELECT c.ID FROM (SELECT * FROM patients ) as c 
                                 WHERE c.login_patients  = '$login');");

    mysqli_query($connect, $sql1);


   }
}

the error occurs on SET patient_exercise.use_exercise=$useExercise[$i]

And my Json is:

{"Paciente":[{"Nome":"Rafael"},
           {"Exercicios":[{"0":"1"},
                        {"1":"0"},
                        {"2":"0"}]}]
}

What could be wrong?

Upvotes: 0

Views: 63

Answers (2)

Rodrigo Ferraz
Rodrigo Ferraz

Reputation: 9

So, I've made a mistake by not capturing the data from $json properly. I've also changed my json object. What I did was this:

 $result= array();
    foreach($json as $patients){
        foreach ($patients as $key=>$value){
            foreach ($value as $a=>$b)          
                    $result[] = $b;     
        }       
    }

    for ($i=0; sizeOf($result)>$i;$i++){
        if($i ==0){
            $login = $result[$i];
        } 
        else{
            $useExercise[]=$result[$i];
        }
    }

And my modified Json:

{"patient":[{"name":"rafael"},
             {"0":"1","1":"0","2":"0"}
            ]
}

Thank you, Kingsley Mitchell for taking your time to help me! :)

Upvotes: 1

Kingsley Mitchell
Kingsley Mitchell

Reputation: 2589

Think your main problem is you are making it to complex by jumping steps instead of doing it step by step.

Make it simple because it looks confusing and it is hard to correct because your context and naming conventions are not easy to understand.

Try this format : $SQL = "UPDATE isignup SET password='". $NPass ."' WHERE custID='$num'";

Upvotes: 0

Related Questions