6
I have a form where I display to the user some phases that a particular contract needs to respect, follows image of the said form:
I’ve already done the update
that makes the order change and is working, my doubt is the following, following the example of the image I posted, how can I change the order 3 to 1 and the order 1 to order 3 at the same time.
The code that updates without this implemented routine is this:
if ($_POST["Operacao"] == 'UpFaseObrigatoria') { $sql = "UPDATE `intranet_cocari`.`gerFaseObrigatoria` SET `Ordem` = ? WHERE `gerFaseObrigatoria`.`IdContrato` = ? AND `gerFaseObrigatoria`.`IdTipoFase` = ?"; if ($stmt = $conn->prepare($sql) ){ $stmt->bind_param( "iii", $_POST["Ordem"], $_POST["IdContrato"], $_POST["IdTipoFase"] ); if ($stmt->execute()){ $aretorno["msg"] = "Registro atualizado com sucesso."; $stmt->close(); } else { $aretorno["msg"] = "Ocorreu um erro na atualização dos dados: " . $stmt->error . ". Verifique."; $aretorno["status"] = "ERRO"; } } else { $aretorno["msg"] = "Ocorreu um erro na preparação dos dados: " . $stmt->error . ". Verifique."; $aretorno["status"] = "ERRO"; } }
I’ve achieved a lot with the help of @Felipe Moraes, the code changed according to the suggestion was like this:
UPDATE gerFaseObrigatoria AS FaseObrigatoria JOIN gerFaseObrigatoria AS gerFaseObrigatoriaUp ON ( FaseObrigatoria.Ordem = ? AND gerFaseObrigatoriaUp.Ordem = 1 ) OR ( FaseObrigatoria.Ordem = 1 AND gerFaseObrigatoriaUp.Ordem = ? ) SET FaseObrigatoria.Ordem = gerFaseObrigatoriaUp.Ordem, gerFaseObrigatoriaUp.Ordem = FaseObrigatoria.Ordem WHERE FaseObrigatoria.IdContrato = ? AND gerFaseObrigatoriaUp.IdContrato = ?
Copy and paste this code thingy into my BD
and the change was made successfully, but when applying on my page the script gives me the message that the change was made, but the same is not being executed.
I believe I’m making the mistake at the time of the bind_param
, the code is like this:
if ($_POST["Operacao"] == 'UpFaseObrigatoria') { $sql = "UPDATE gerFaseObrigatoria AS FaseObrigatoria JOIN gerFaseObrigatoria AS gerFaseObrigatoriaUp ON ( FaseObrigatoria.Ordem = ? AND gerFaseObrigatoriaUp.Ordem = 1 ) OR ( FaseObrigatoria.Ordem = 1 AND gerFaseObrigatoriaUp.Ordem = ? ) SET FaseObrigatoria.Ordem = gerFaseObrigatoriaUp.Ordem, gerFaseObrigatoriaUp.Ordem = FaseObrigatoria.Ordem WHERE FaseObrigatoria.IdContrato = ? AND gerFaseObrigatoriaUp.IdContrato = ? "; if($stmt = $conn->prepare($sql) ){ $stmt->bind_param( "iiii", $_POST["Ordem"], $_POST["Ordem"], $_POST["IdContrato"], $_POST["IdContrato"] ); if($stmt->execute()){ $aretorno["msg"] = "Registro atualizado com sucesso."; $stmt->close(); }else{ $aretorno["msg"] = "Ocorreu um erro na atualização dos dados: " . $stmt->error . ". Verifique."; $aretorno["status"] = "ERRO"; } }else{ $aretorno["msg"] = "Ocorreu um erro na preparação dos dados: " . $stmt->error . ". Verifique."; $aretorno["status"] = "ERRO"; } }
Hi @Felipe Moraes your tip was excellent, I mounted my query and apply it in my BD to test and it worked perfectly, but now when I try to play the same script in the example I posted is not working? I believe it’s time to replace bind_param, any suggestions?
– adventistapr
@adventistapr how did your code get with the change? Can you post? But do not delete the code from the question if it is not without context.
– Filipe Moraes
Hello @Filipe Moraes, but how can I post? In the same question post or a new?
– adventistapr
@adventistapr changed the answer by adding how the PHP code should look. See if it helps, if not put here in the comments your difficulty.
– Filipe Moraes