0

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?

2 Answers 2

1

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! :)

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

1 Comment

Sorry. I just did it! :D
0

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'";

26 Comments

Sorry, I've edited my question. What steps could I add so you can understand it better?
You are updating multiple rows in a database at once, try splitting it up into one-liners .
Joins are notorious of problems
Take out the joins and make it without the joins first.
But Im using a FOR. How am I adding multiple rows at once? I'm using each element from an array and adding each one of them at a time. At least from what I understand from the FOR clause.
|

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.