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

4

Let's say I have button value="+1" on my page, and every time I click on this button an update is made to the database, the page refreshes and displays a message of success or error. This update process is simple! Only when returning from refresh the information is not updated.

Example

The levels field in the X table currently has a value of 10. I use a query and display this information! In that same page I add button with the code of UPDATE , and every time I click on button it increases one level in the DB, only that by clicking on this button the page goes from refresh and will display the message . But even if everything has happened ideally, the return of the information will be old, that is, it will still display LEVEL = 10 , and has already been changed in the DB, in> is already equal to 11.

How can you 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();       
        }
?>
    
asked by anonymous 02.06.2015 / 04:19

2 answers

3

To get the information already updated is necessary to make a select after the update, as you already have it ready, simply transform it 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 one at the beginning and another after the update.

//código omitido
$up_props_executa = $up_props->execute(); 
$novo = buscarNivel($pdo, $id);
    
02.06.2015 / 16:44
0

You must execute the SELECT query, which pulls the information from the database, after the UPDATE query, which adds +1 in the case.

    
02.06.2015 / 04:29