How to insert a value filtered by SELECT within an UPDATE query in the PHP script

Asked

Viewed 51 times

-2

I just need to get the R.A’s of the SELECT and play at UPDATE, but every time I put $usuario['ra], of a mistake:

Parse error: syntax error, Unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting '-' or Identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C: xampp htdocs myschool test.php on line 36

<?php

require_once('db_class.php');

$sql = "SELECT * FROM usuario WHERE ativo_sn = 'n' ";

$objdb = new db();
$link = $objdb->conecta_mysql();
$result = mysqli_query($link,$sql);
?>

     if($result){
      $dados = array();
      while($linha = mysqli_fetch_array($result,MYSQLI_ASSOC)){
         $dados[] = $linha;
      }

       foreach ($dados as $usuario) {
       ?>
         <label><?php echo $usuario['nome_user'];?> </label><br/>
         <label><?php echo $usuario['ra'];?> <br/>
         <a href="" onclick="<?php mysqli_query($link,
            "UPDATE usuario SET ativo_sn = 's' WHERE ra = '**PRECISO COLOCAR R.A AQUI**' ");
            ?>">Aceitar</a><hr/>
            <?php

    }         

2 answers

0

Changes your query to

mysqli_query($link,"UPDATE usuario SET ativo_sn = 's' WHERE ra = '".$usuario['ra']."' ");

You need to take out the variable quotes that you want to pass.

  • it worked!! but every time I enter the page it already executes the query, even when I put in the onclick of the link, I switched for button and still goes automatically : aa

0

Rafael, I think it would be possible to optimize the code also by populating the Abels and hyperlinks within the while itself, dispensing with the creation of the $data and foreach array.

while(....) {
     $usuario = $dados['nome_user'];
     $ra = $dados['ra'];
     ....
}

Browser other questions tagged

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