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 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么