-2

Something is up with my code but I';m not sure what. I have created a very basic music library input form that saves to a mySQL table and outputs the data to the screen as well. For some reason it updates everytime I refresh my browser page and saves to the database. Can somebody tell me where I'm going wrong here? Thanks, Tim

<?php
    //connect
    $link = mysqli_connect("localhost", "my_username", "mypassword", "mydb");

    //create user feedback if library has been updated
    if(isset($_POST['submit'])) {
        $cleanartist = mysqli_real_escape_string($link, $_POST['Artist']);
        $cleantitle = mysqli_real_escape_string($link, $_POST['Title']);

        //create query statement
        $sql = "INSERT INTO albums (Artist, Title) VALUES ('$cleanartist', '$cleantitle')";

        //run query
        mysqli_query($link, $sql);

        echo "Thanks, added to library";
    }

    mysqli_close($link);
?>


<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Music Library</title>
    </head>
    <body>
        <form class="" action="music_library.php" method="post">
            <p>Add Artist<input type="text" name="Artist"></p>
            <p>Album Title<input type"text" name="Title"></p>
            <input type="submit" name="submit" value="submit">
        </form>

        <?php

            //connect to db
            $link = mysqli_connect("localhost", "my_username", "mypassword", "mydb");

            //create query statement
            $qry = "SELECT * FROM albums";

            //run query and store result in variable
            $result = mysqli_query($link, $qry);

            //loop through returned data and output to screen
            while($row = mysqli_fetch_assoc($result)) {
                echo '<tr>';
                foreach($row as $field) {
                    echo '<td>' . $field . '</td>' . '</br>';
                }
            }

            mysqli_close($link);

        ?>

    </body>
</html>
Hossein
  • 1,301
  • 1
  • 12
  • 23
Tim Williams
  • 69
  • 1
  • 9
  • why you refresh page, browsers asks you something like "data will be resent, are you sure?" what do you click on this window? – Iłya Bursov May 17 '16 at 20:44
  • When you refresh, are you resending your form data? – Andy Holmes May 17 '16 at 20:44
  • once you submit the data the variable contain the value you enter, so every time you refresh the browser the same value previously enter are insert into database... Sorry fro my bad english...The problem is that you set the variable once you submit the form – Sajjad Khan May 17 '16 at 21:04
  • Hello Tim, I've edited the answer to use your page link. Can you undo the -1? Thanks – Richard May 18 '16 at 07:13
  • Rich, no problem, how do I go about doing that? I'm a newish novice member here and not sure of what to do to achieve what you need? – Tim Williams May 18 '16 at 07:18

2 Answers2

0

You can find some hints on one of these 2 answers

PHP Redirect to another page after form submit

PHP header() redirect with POST variables

basically, you need to call header("Location: music_library.php"); when the operation is succesfull.

Community
  • 1
  • 1
Richard
  • 1,045
  • 7
  • 11
0

When you refresh a page that before it submitted data to it, its again submitted! you can simply unset variable after storing data to database with this function :

unset($_POST['submit']);
Hossein
  • 1,301
  • 1
  • 12
  • 23