PHP - I can’t update the database

Asked

Viewed 47 times

1

I can’t update the database, someone can help me pfv?

<?php

$ligaBD = mysqli_connect("localhost", "root", "", "pap");

$tipo_autorizacao = $_POST['tipo_autorizacao'];
$autorizacao_obs = $_POST['autorizacao_obs'];

$linhaProposta = "INSERT INTO propostas () VALUES()";
$resultado = mysqli_query($ligaBD, $linhaProposta);

// Ir buscar o nproposta da proposta
$npropostaLinha = "SELECT nproposta FROM propostas ORDER BY nproposta DESC";
$resultado = mysqli_query($ligaBD, $npropostaLinha);
$nproposta = "";
printf($npropostaLinha);
$row = mysqli_fetch_array($resultado, MYSQLI_NUM);
printf("==> ");
print_r($row[0]);
$nproposta = $row[0];

$insertAutorizacao = "INSERT INTO autorizacao (nproposta,tipo_autorizacao,autorizacao_obs) 
                    VALUES ($nproposta,'$tipo_autorizacao','$autorizacao_obs')";

$resultado = mysqli_query($ligaBD, $insertAutorizacao);
$selectAutorizacao = "SELECT autorizacao FROM propostas WHERE nproposta='$nproposta'";

$updateAutorizacao = mysqli_query($ligaBD, $selectAutorizacao);

if ($row["autorizacao"] == "autorizacao_sim") {
    $updateAutorizacao = "UPDATE propostas SET autorizacao='Autorizado' WHERE nproposta='$nproposta'";
} else if ($row["autorizacao"] == "autorizacao_nao") {
    $updateAutorizacao = "UPDATE propostas SET autorizacao='Nao Autorizado' WHERE nproposta='$nproposta'";
}

$resultado = mysqli_query($ligaBD, $updateAutorizacao);
header('Location: ./conselhoadmin.php');
mysqli_close($ligaBD);
?>

error: inserir a descrição da imagem aqui

  • which error you are accusing?

  • I forgot to put it in, I’ve edited it. Thank you

  • The error is here $row["autorizacao"] where that came from $row?

2 answers

0

I found the possible error, I had to define the variable $row, which contains the array brought by SELECT that will define the operation of UPDATE

    $resultado=mysqli_query($ligaBD, $insertAutorizacao);
    $selectAutorizacao = "SELECT autorizacao FROM propostas WHERE nproposta='$nproposta'";  

    $updateAutorizacao = mysqli_query($ligaBD, $selectAutorizacao);

    //*********************************************************************
    // aqui está o que faltava

    $row = mysqli_fetch_assoc($updateAutorizacao);

    //*********************************************************************

    if($row["autorizacao"] == "autorizacao_sim") {

        $updateAutorizacao = "UPDATE propostas SET autorizacao='Autorizado' WHERE nproposta='$nproposta'";  

    }else if ($row["autorizacao"] == "autorizacao_nao"){

        $updateAutorizacao = "UPDATE propostas SET autorizacao='Nao Autorizado' WHERE nproposta='$nproposta'";  

    }       
    $resultado=mysqli_query($ligaBD, $updateAutorizacao);
    if(!$resultado){
        echo("Error description: " . mysqli_error($ligaBD));
    }
  • Now appears this bug http://prntscr.com/nah727

  • already edited, is mysqli_fetch_assoc

  • 1

    the first two errors are gone, now it’s just the error of Parameter 2. Thank you for helping me out

  • I re-edited the answer, but I believe it’s a problem with $row["autorizacao"] I believe the query is not being defined

  • I’ll try to do some more research, thanks anyway!

  • Take a test, from a var_dump($row) to make sure what you wanted is coming

  • gave this http://prntscr.com/nahray

  • $row["autorizacao"]='Por Autorizar', as in the if() that will define which UPDATE should be done not have that option, it is not going to query to the mysqli_query

Show 3 more comments

0


I made some modifications, it worked here, I took into consideration that your telabe have the model I put in the Insert, whatever thing and just modify.

 

$ligaBD = mysqli_connect("localhost", "root", "", "Pap");

$type authorization = $_POST['type authorization']; $autorizacao_obs = $_POST['autorizacao_obs'];

$opposite line = "INSERT INTO propostas (nproposta, tipo_autorizacao, autorizacao_obs, autorizacao) VALUES ( '1234', '1234', '1234', 'autorizacao_sim')"; $result = mysqli_query($ligaBD, $lineProposite);

// Go get the proposal proposal $npropostaLine = "SELECT nproposta FROM propostas ORDER BY nproposta DESC"; $result = mysqli_query($ligaBD, $npropostaLine); $nproposta = ""; printf($npropostaLine); $Row = mysqli_fetch_array($result, MYSQLI_NUM); printf("==> "); print_r($Row[0]); $nproposta = $Row[0];

$insertAutorizacao = "INSERT INTO authorization (nproposta,typo_authorization,autorizacao_obs) VALUES ($nproposta,'$type_authorization','$authorized_obs')";

$result = mysqli_query($ligaBD, $insertAuthorization); $selectAuthorization = "SELECT authorization FROM bids WHERE nproposta='$nproposta'";

if ($result = mysqli_query($ligaBD, $selectAuthorization)) {

/* fetch associative array */ while ($rowline = mysqli_fetch_assoc($result)) { if ($rowline["authorization"] == "authorization") { $updateAuthorization = "UPDATE PROPOSALS SET authorization='Authorized' WHERE nproposta='$nproposta'"; } Else if ($rowline["authorization"] == "authorization $updateAuthorization = "UPDATE PROPOSALS SET authorization='Not Authorized' WHERE nproposta='$nproposta'"; } if ($rowline["authorization"] != "Authorized") { mysqli_query($ligaBD, $updateAuthorization); } } /* free result set */ mysqli_free_result($result);}
  • Thanks for the answer. However I tried this code and gave this error http://prntscr.com/nak4ul

  • tries to exchange the if code for that; if ($rowline["authorization"] == "authorization"" || $rowline["authorization"] == "authorization mysqli_query($ligaBD, $updateAuthorization); }

  • if ($rowline["authorization"] == "authorization" || $rowline["authorization"] == "authorization" ) { mysqli_query($ligaBD, $updateAuthorization); }

  • can already solve, thank you very much

  • You’re welcome! Whatever and just talk.

Browser other questions tagged

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