gmc1103 Posted March 17, 2016 Share Posted March 17, 2016 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? Quote Link to comment https://forums.phpfreaks.com/topic/301029-foreach-in-mysql-updates-error/ Share on other sites More sharing options...
Barand Posted March 17, 2016 Share Posted March 17, 2016 You have function to which you pass a comma-delimited list (in $idDocentes) and another value in $mudaGrupo. Without showing any code, tell me what the function is supposed to do with those inputs? Quote Link to comment https://forums.phpfreaks.com/topic/301029-foreach-in-mysql-updates-error/#findComment-1532130 Share on other sites More sharing options...
gmc1103 Posted March 17, 2016 Author Share Posted March 17, 2016 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 Quote Link to comment https://forums.phpfreaks.com/topic/301029-foreach-in-mysql-updates-error/#findComment-1532131 Share on other sites More sharing options...
Barand Posted March 17, 2016 Share Posted March 17, 2016 That is what I thought. You are are assigning the ids to the $mudaGrupo variable and 0, 1, 2... to the ids. Instead of foreach ($myArray as $id=> $mudaGrupo) you need foreach ($myArray as $id) { Quote Link to comment https://forums.phpfreaks.com/topic/301029-foreach-in-mysql-updates-error/#findComment-1532132 Share on other sites More sharing options...
gmc1103 Posted March 17, 2016 Author Share Posted March 17, 2016 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... Quote Link to comment https://forums.phpfreaks.com/topic/301029-foreach-in-mysql-updates-error/#findComment-1532133 Share on other sites More sharing options...
Barand Posted March 17, 2016 Share Posted March 17, 2016 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...'); } } Quote Link to comment https://forums.phpfreaks.com/topic/301029-foreach-in-mysql-updates-error/#findComment-1532135 Share on other sites More sharing options...
gmc1103 Posted March 17, 2016 Author Share Posted March 17, 2016 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 ) Quote Link to comment https://forums.phpfreaks.com/topic/301029-foreach-in-mysql-updates-error/#findComment-1532136 Share on other sites More sharing options...
Barand Posted March 17, 2016 Share Posted March 17, 2016 Why have you defined :idteacher as type PARAM_STR? As soon as you update a record you call return, which immediately exits the function. You should return at the end when all updates have been done Quote Link to comment https://forums.phpfreaks.com/topic/301029-foreach-in-mysql-updates-error/#findComment-1532138 Share on other sites More sharing options...
mac_gyver Posted March 17, 2016 Share Posted March 17, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/301029-foreach-in-mysql-updates-error/#findComment-1532140 Share on other sites More sharing options...
gmc1103 Posted March 17, 2016 Author Share Posted March 17, 2016 (edited) Hi Barand moving the return outside the loop? Loop {} Retunr array Is that? Mac Yes it is true...i don't need the if statement to check since i have try/catch Edited March 17, 2016 by gmc1103 Quote Link to comment https://forums.phpfreaks.com/topic/301029-foreach-in-mysql-updates-error/#findComment-1532142 Share on other sites More sharing options...
Jacques1 Posted March 17, 2016 Share Posted March 17, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/301029-foreach-in-mysql-updates-error/#findComment-1532144 Share on other sites More sharing options...
gmc1103 Posted March 17, 2016 Author Share Posted March 17, 2016 Hi I have fixed this with all of you. Thanks Barand,Mac_gyver and Jacques1 The problem was the return inside the loop. I used $result = $stmt->execute();~ //then if (!$result) Best regards Quote Link to comment https://forums.phpfreaks.com/topic/301029-foreach-in-mysql-updates-error/#findComment-1532148 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.