SQL command does not work with select

Asked

Viewed 255 times

1

Follow my complete code:

<?
    include("conectar.php");    

$query = "select * from pagseguro ORDER BY Referencia DESC";
$result = mysqli_query($db, $query) or die();

    if($_REQUEST['alterarStatus']){
        $status = trataaspas($_REQUEST['alterarStatus']);
        $status_explode = explode("|", $status);        
    }

if($status_explode[0]=="aprovado"){

    $SQL = "update pagseguro set StatusTransacao='Aprovado' where Referencia = '".$status_explode[1]."'";  

}elseif($status_explode[0]=="completo"){

    $SQL = "update pagseguro set StatusTransacao='Completo' where Referencia = '".$status_explode[1]."'";  

}elseif($status_explode[0]=="cancelado"){

    $SQL = "update pagseguro set StatusTransacao='Cancelado' where Referencia = '".$status_explode[1]."'";  

}elseif($status_explode[0]=="devolvido"){

    $SQL = "update pagseguro set StatusTransacao='Devolvido' where Referencia = '".$status_explode[1]."'";  

}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" language="javascript">
function alterarStatusFunction(val){
if(val =="aprovado"){
if (confirm('Tem certeza que deseja alterar o status dessa transação para APROVADO?')) {
document.formtransacoes.submit();
} else {
return false;
}
}
else if(val =="completo"){
if (confirm('Tem certeza que deseja alterar o status dessa transação para COMPLETO?')) {
document.formtransacoes.submit();
} else {
return false;
}
}
else if(val =="cancelado"){
if (confirm('Tem certeza que deseja alterar o status dessa transação para CANCELADO?')) {
document.formtransacoes.submit();
} else {
return false;
}
}
else if(val =="devolvido"){
if (confirm('Tem certeza que deseja alterar o status dessa transação para DEVOLVIDO?')) {
document.formtransacoes.submit();
} else {
return false;
}
}
}
</script>
</head>
<body>

<table width="100%"  border="0" cellpadding="0" cellspacing="10">

<?  if(isset($total)){ if($total==0){?>
N/D
<? }else{ ?>

<tr><td>
<table width="100%" border="1" cellspacing="0" cellpadding="10" class="t-a">
<thead>
    <tr class="th-a"> 
    <th nowrap="nowrap" align="center" width="7%">Refer&ecirc;ncia</th>
    <th nowrap="nowrap" align="center">Opções</th>
    </tr>
</thead>

<tbody>
<form name="formtransacoes" id="formtransacoes" action="" method="post">
<?
while($obj = mysqli_fetch_object($result)){
    $statustranssacao = $obj->StatusTransacao;
?>
    <tr>
    <td nowrap="nowrap" align="center"><?=$obj->Referencia;?></td>
    <td nowrap="nowrap" align="center">
    <?  if($obj->TransacaoID!=''){?>
    <select style="width:200px" onchange="alterarStatusFunction(this.options[this.selectedIndex].title);" name="alterarStatus">
        <option selected title="0">- Opções -</option>
        <option disabled="disabled">------------------------------------------</option>
        <? if($statustranssacao!='Aprovado' && $statustranssacao!='Completo'){?><option title="aprovado" value="aprovado|<?=$obj->Referencia;?>">Marcar como APROVADO</option><? } ?>
        <? if($statustranssacao!='Completo' && $statustranssacao=='Aprovado'){?><option title="completo" value="completo|<?=$obj->Referencia;?>">Marcar como COMPLETO</option><? } ?>
        <? if($statustranssacao!='Cancelado'){?><option title="cancelado" value="cancelado|<?=$obj->Referencia;?>">Marcar como CANCELADO</option><? } ?>
        <? if($statustranssacao!='Devolvido'){?><option title="devolvido" value="devolvido|<?=$obj->Referencia;?>">Marcar como DEVOLVIDO</option><? } ?>
    </select>
    <? }else{ ?>
    <span>Transação não iniciada!</span>
    <? } ?>
    </td>
    </tr>
<? } } ?>       
</form>
</tbody>

<? } ?>

</table>
</td></tr>

</table>

</body>
</html>

Can anyone tell me why it doesn’t work? It seems that it is not running the query to update the record of the selected item.

  • It has no connection to the database in its code or execution, it only has a string with the update

  • He’s in the connection file, I didn’t put him there.

  • 1

    But there’s one mysqli_query() or $pdo->query() without that no sql statement is processed.

  • I updated the code, take a look..

  • 1

    hehehe is the case that they think that we are Stackoverflownian beings endowed with superpowers that can guess what the code is

  • I removed my response that was just to show that there is nothing wrong with SQL and that would be missing only the part to run the UPDATE, ie $data = mysqli_query($link, $SQL);

  • @Leocaracciolo managed to make it work, knows some way to create a validation to show a message whether the query was executed or not?

  • boy, I was suspicious of that part, in the response I removed I had commented Talvez na segunda parte, como estão sendo carregados os values. Beauty that you achieved. Validation can be very simple if($SQL){mensagem sucesso}else{erro} You have more here https://stackoverflow.com/questions/2060325/need-to-test-if-sql-query-was-successful

Show 3 more comments

1 answer

2

In the code there is a string would with update if they are not sent to the bank they do nothing. Basically what was missing in your code was a mysqi_query().

You can simplify if with a status array.

Take care of the logic and scope of variables.

if($_REQUEST['alterarStatus']){
   $status = trataaspas($_REQUEST['alterarStatus']);
   $status_explode = explode("|", $status);

   $status = array('aprovado' => 'Aprovado', 'completo' => 'Completo', 'cancelado' => 'Cancelado', 'devolvido' => 'Devolvido');

   $statusTransacao = isset($status[$status_explode[0]]) ? $status[$status_explode[0]] : false;
   if($statusTransacao){
      $sql = sprintf("update pagseguro set StatusTransacao='%s' where Referencia = '%s'", $statusTransacao, $status_explode[1]);
      mysqli_query($db, $sql) or die(mysqli_error($db)); //linha que executa a sql ;)
   }
   //demais códigos...
  • mysqi_query() was inside the connection file, but I updated the question with the correct codes, take a look, it still doesn’t work.

  • 1

    @CEW in question code has no mysqli_query() for the update.

  • Jeez, it’s true.. I understood what you meant. I really forgot to put mysqli_query for updates.. I tested with your code but it didn’t work either

  • @CEW gave some error? put the mysqli_error()?

  • Yes, but no error occurs simply does not perform the update. I couldn’t understand this new way you did it there, it’s taking the value reference ID?

  • the name says it all trataaspas but how is this code?

  • @CEW see if you can get into if($_REQUEST['alterarStatus']){ can pick up some echos ... to check or break point if you use Xdebug.

  • @Leocaracciolo this code does not interfere in anything, I tested without it and it did not work.

  • @rray I will test here, already I speak to you. Valew ;)

  • @CEW we have to search everything, if it’s beautiful then keep looking

  • @rray will not go, I put an echo there and it did not appear the message nor echo.. I think there must be some error in php code.

  • @CEW also suggest you extract the code that updates to another php file, remember to put its name in the form action. So you separate well the code that makes the display from which you make a change in the database.

  • @rray I saw that the problem is in that part: if($_REQUEST['alteraStatus']){&#xA; $status = $_REQUEST['alteraStatus'];&#xA; $status_explode = explode("|", $status); &#xA; } I tested by changing to $status = "aprovado|12345";, ai it worked and showed echo. It means that it is not getting the value of the option is not that?

  • I gave one echo $_REQUEST['alteraStatus']; on the page and I saw that he took the text only from the first input, and not the value. He returned this: - Options -

  • First option*

  • 1

    I managed to solve, it was the first option the problem, I just inserted the disabled attribute in it and it worked :D

Show 11 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.