C_plus_plus_Rookie
C_plus_plus_Rookie

Reputation: 71

PHP Not updating on reload

I'm trying to create a system that when i submit the form, after the page refresh it should show the new values that i get from the database. The values work well when they go into the databse but they dont show after submited, only when i refresh again. Thanks for helping

<?php

include("connect.php");

    $query = "SELECT * FROM `laliga`";
    $result = mysql_query($query);

    while($row = mysql_fetch_assoc($result)){
        $id = $row['id'];
        $home = $row['home'];
        $away = $row['away'];
        $win = $row['win'];
        $draw = $row['draw'];
        $lose = $row['lose'];
    }

    echo "<h2>La Liga</h2>";

    echo $home, " - ", $away;

    if (isset($_POST) && $_POST['laliga'] == 1){
        $select = mysql_real_escape_string($_POST['laliga']);
        $select = $win + $select;
        mysql_query("UPDATE laliga SET win='$select'");
    }else if (isset($_POST) && $_POST['laliga'] == 'X'){
        $select = mysql_real_escape_string($_POST['laliga']);
        $select = '1';
        $select = $draw + $select;
        mysql_query("UPDATE laliga SET draw='$select'");
    }else if (isset($_POST) && $_POST['laliga'] == 2){
        $select = mysql_real_escape_string($_POST['laliga']);
        $select = '1';
        $select = $lose + $select;
        mysql_query("UPDATE laliga SET lose='$select'");
    } 

    header('Location: ../laliga.php');


    ?>

    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

        <input type="radio" name="laliga" value="1">1
        <input type="radio" name="laliga" value="X">X
        <input type="radio" name="laliga" value="2">2

        <input type="submit" name="submit" value="Submit"/>

    </form>

    <br/>
    <?php

    echo $home, " -> ", $win;
    echo "<br/>Barazim  -> ", $draw,"<br/>";
    echo $away, " -> ", $lose;

    ?>

Upvotes: 0

Views: 1049

Answers (2)

Charlie Walton
Charlie Walton

Reputation: 312

You should handle all of the post data at the top of the PHP file, whilst the header function will solve your problem it's a silly and inefficient way of approaching it. by handling the post data and updating the database first, by the time you query the database the data is there! at the moment you are trying to find the data and then adding it. does this make sense? Good luck!

Upvotes: 1

xRed
xRed

Reputation: 1997

Add:

header('Location: <mypage.php>');

After mysql_query.

Upvotes: 0

Related Questions