duanhong8839 2016-05-17 20:40
浏览 38
已采纳

为什么每次刷新浏览器时数据库都会更新?

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>
  • 写回答

2条回答 默认 最新

  • duanbin198788 2016-05-17 20:45
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题
  • ¥60 高价有偿求java辅导。工程量较大,价格你定,联系确定辅导后将采纳你的答案。希望能给出完整详细代码,并能解释回答我关于代码的疑问疑问,代码要求如下,联系我会发文档