Jump to content

Recommended Posts

Hi

 

I'm having problems(no updates) in my databases using this code

public function editMultiDocenteGrupo($idDocentes, $mudaGrupo){
         try {
        $stmt = $this->db->prepare("UPDATE `esmaior_ca`.`professor` SET `teacher_grupo` = :teacher_grupo WHERE `idteacher` = :idteacher");
        $myArray = explode(',', $idDocentes);
            foreach($myArray as $id=> $mudaGrupo){       
                $stmt->bindParam(':teacher_grupo', $mudaGrupo, PDO::PARAM_INT);
                $stmt->bindParam(':idteacher', $id, PDO::PARAM_STR);
                if (!$stmt->execute()) {
                    print_r($stmt->errorInfo());
                    return array('status' => 'error', 'message' => 'Opppss...no updates..');
                } else {
                   return array('status' => 'success', 'message' => 'All changes updated...');
                } 
            }
        } catch (PDOException $e) {
            echo $e->getMessage();
        }
        
    }

The array is this one when i user print_r

Array ( [0] => 343 [1] => 256 [2] => 245 [3] => 265 [4] => 287 [5] => 201 [6] => 210 [7] => 275 [8] => 271 [9] => 260 )

i receive the success message but no changes are registered in the table

 

Any help?

Link to comment
https://forums.phpfreaks.com/topic/301029-foreach-in-mysql-updates-error/
Share on other sites

Hi Barand

 

I'm receiving two variables (idteacher, groupteacher) in this function

One comes with delimited commas to put into an array

With this array  represents a teacher_id number.

So if i want to update the groupteacher number in for examples 12 teachers my update function must be in a loop

for every idteacher selected update groupteacher

Hi Barand

 

Only the first one is changed

 

This is the code

 public function editMultiDocenteGrupo($idDocentes, $mudaGrupo){
         try {
        $stmt = $this->db->prepare("UPDATE `esmaior_ca`.`professor` SET `teacher_grupo` = :teacher_grupo WHERE `idteacher` = :idteacher");
        $myArray = explode(',', $idDocentes);
            foreach($myArray as $id){       
                $stmt->bindParam(':teacher_grupo', $mudaGrupo, PDO::PARAM_INT);
                $stmt->bindParam(':idteacher', $id, PDO::PARAM_STR);
                if (!$stmt->execute()) {
                    print_r($stmt->errorInfo());
                    return array('status' => 'error', 'message' => 'Opppss...no updates..');
                } else {
                   return array('status' => 'success', 'message' => 'All changes updated...');
                } 
            }
        } catch (PDOException $e) {
            echo $e->getMessage();
        }
        
    }

I have 12, but only the first one is updated...

Bind the params once before the loop

        $stmt = $this->db->prepare("UPDATE `esmaior_ca`.`professor` SET `teacher_grupo` = :teacher_grupo WHERE `idteacher` = :idteacher");
        $stmt->bindParam(':teacher_grupo', $mudaGrupo, PDO::PARAM_INT);
        $stmt->bindParam(':idteacher', $id, PDO::PARAM_STR);
        $myArray = explode(',', $idDocentes);
            foreach($myArray as $id){       
                if (!$stmt->execute()) {
                    print_r($stmt->errorInfo());
                    return array('status' => 'error', 'message' => 'Opppss...no updates..');
                } else {
                   return array('status' => 'success', 'message' => 'All changes updated...');
                } 
            }

Still updating only the first one...

 

I have checked the input 

343,256,245,265,287,201,210,275,271,260

And when it go to my function i have this when i use 

 

print_r($idDocentes);
echo "-----";
print_r($myArray);
124 343,256,245,265,287,201,210,275,271,260
Array ( [0] => 343 [1] => 256 [2] => 245 [3] => 265 [4] => 287 [5] => 201 [6] => 210 [7] => 275 [8] => 271 [9] => 260 ) 

since you have a try/catch block around your code, that implies you have set the pdo error mode to exceptions. if this is true, why do you even have a conditional statement testing $stmt->execute() ?

 

if the execute() method call fails, your code isn't going to be running any of that logic. it will be running the code in the catch{  } block.

Yes it is true...i don't need the if statement to check since i have try/catch

 

You don't need the try statement either. It's actually harmful, because it swallows important information (the exact location and the stack trace) while exposing the internal error message to your users.

 

Get rid of it and simply let PHP handle the exception.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.