Update the information on the page when making a change in the BD

Asked

Viewed 1,422 times

4

Let’s say I have button value="+1" on my page, and every time I click on that button is updated in the database, the page refreshes and displayed a message of success or error. This update process is simple! Only when you return from refresh the information is not updated.

Example

The field levels on the table X currently has the value of 10. I use a query and display this information! On the same page I add the button with the code of UPDATE, and each time I click on button increases a level in the BD, only by clicking on this button the page goes refresh and will display the message. But even if everything has been done optimally, the return of the information will be old, IE, still display NIVEL = 10, which has already been amended in the BD, namely the level is already equal to 11.

How then to make the return of this information already "current"?

<?php  
    include_once("head.php"); 
    $ID = 1;
    $niveis = $pdo->prepare("SELECT * FROM x WHERE ID = ?");
    $niveis->bindValue(1, $ID, PDO::PARAM_INT);
    $niveis->execute();    
    $niveis_dados = $niveis->fetchObject();
?>

    NIVEL ATUAL: <?php echo $niveis_dados->NIVEL ?>
    <div class="DA">Aumentar/Diminuir :         </div>
    <div class="DB">
                    <form method="post" enctype="multipart/form-data">
                        <button value="1" name="UPDOWN" type="submit">+</button>
                        <button value="-1" name="UPDOWN" type="submit">-</button>
                    </form>
    </div>
</div> 


<?php
    //CODIGO DE UPDATE DO NIVEL
    if(isset($_POST['UPDOWN']))
        {

        //AUMENTAR ou DIMINUIR
            $UPDOW = $_POST['UPDOWN'];

        //NOVO NIVEL    
            $novo_nivel = $niveis_dados->NIVEL + $UPDOW;
            echo "Novo nivel: ".$novo_nivel;

        //UPDATE 1 ::: NOVO NIVEL
            $up_props = $pdo->prepare("UPDATE x SET NIVEL = :1 WHERE ID = :2");
            $up_props->bindParam(":1", $novo_nivel , PDO::PARAM_INT);
            $up_props->bindParam(":2", $ID , PDO::PARAM_INT);
            $up_props_executa = $up_props->execute();       
        }
?>
  • 1

    Puts the source code, to ensure vc need to make a new select to get the updated information.

2 answers

3


To get the information already updated it is necessary to make a select after the update, as you already have it just turns into a function that asks for two parameters, the connection and the id, if the id is not informed the default value will be 1.

function buscarNivel($pdo, $id=1){
    $niveis = $pdo->prepare("SELECT * FROM x WHERE ID = ?");
    $niveis->bindValue(1, $id, PDO::PARAM_INT);
    if($niveis->execute() === false){
        return false;
    }
    return $niveis->fetchObject();
}

The function should be called twice at the beginning and once after the update.

//código omitido
$up_props_executa = $up_props->execute(); 
$novo = buscarNivel($pdo, $id);
  • Excuse me!! But if you can show me an example already working? Why I’m trying here and not getting it!

  • @Ivanveloso, what are you not getting?

  • I’m still unable to bring the updated information! I used the function at the beginning and soon after the update! Only it’s no use!!

0

You should run the SELECT query, the one that pulls the database information after the UPDATE query, which adds +1 in the case.

  • 1

    can explain a little better? Why I added here the code of prepare only that has not been updated!

  • Take all the second PHP code you have, and play before the first.

Browser other questions tagged

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