停止点击页面刷新时的计数器程序代码

时间:2015-04-17 14:04:22

标签: php

我的点击计数器程序存在问题。我想停止在已经连接phpmyadmin数据库的浏览器中按F5或页面刷新运行,我希望它仅在按钮点击时才有效...

<?php

        $host="localhost"; // Host name 
        $username="root"; // Mysql username 
        $password=""; // Mysql password 
        $db_name="mypage"; // Database name 
        $tbl_name="counter"; // Table name 
        $message = "offer End";

        // Connect to server and select database.
        mysql_connect("$host", "$username", "$password")or die("cannot connect to server "); 
        mysql_select_db("$db_name")or die("cannot select DB");

        $sql="SELECT * FROM $tbl_name";
        $result=mysql_query($sql);
        $rows=mysql_fetch_array($result);
        $counter=$rows['visitors'];

        // if have no counter value set counter = 1
        if(empty($counter))
        {
                $counter=1;
                $sql1="INSERT INTO $tbl_name(visitors) VALUES('$counter')";
                $result1=mysql_query($sql1);
        }
        if($counter>1)
        {
                // count more value
                $addcounter=$counter-1;
                $sql2="update $tbl_name set visitors='$addcounter'";
                $result2=mysql_query($sql2);
                echo "You 're visitors No. ";
                echo $addcounter;

                mysql_close();
        }
        else 
        {
                //$counter=0;
                echo "<script type='text/javascript'>alert('$message');</script>";  
                mysql_close();
        }
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<form method="POST" action="">
    <input type="submit" name="insert" value="submit" onclick="" />
</form>

</body>
</html>

1 个答案:

答案 0 :(得分:1)

正如我在评论中所说,并向您展示了这样做的图形化方式,将isset()与条件语句一起使用,并根据您的命名提交按钮将整个可执行代码包装在其中。

即:

<?php

if(isset($_POST['insert'])){

        $host="localhost"; // Host name 

...

mysql_close();
        }

} // closing brace for  if(isset($_POST['insert']))

您还可以使用标题重定向到条件语句中的同一页面。

即:

header("Location: http://www.example.com/");
exit;

旁注:确保您没有在标题之前输出。


  • 您可能应该从提交按钮中删除onclick=""。在您发布的代码中没有针对它的JS参考。

脚注:

您目前的代码向SQL injection开放。使用mysqli with prepared statementsPDO with prepared statements它们更安全

  • mysql_*函数已弃用,将从以后的PHP版本中删除。

编辑,完全重写#2:

<强> N.B:

将下面的http://www.yoursite.com/your_page.php替换为您的网站以及您用于脚本的网页名称。

尝试这一个:(另一个低于这一个)

<?php

        $host="localhost"; // Host name 
        $username="root"; // Mysql username 
        $password=""; // Mysql password 
        $db_name="mypage"; // Database name 
        $tbl_name="counter"; // Table name 
        $message = "offer End";

        // Connect to server and select database.
        mysql_connect("$host", "$username", "$password")or die("cannot connect to server "); 
        mysql_select_db("$db_name")or die("cannot select DB");

        $sql="SELECT * FROM $tbl_name";
        $result=mysql_query($sql);
        $rows=mysql_fetch_array($result);
        $counter=$rows['visitors'];

        // if have no counter value set counter = 1
        if(empty($counter))
        {
                $counter=1;
                $sql1="INSERT INTO $tbl_name(visitors) VALUES('$counter')";
                $result1=mysql_query($sql1);

        }
        if($counter>1)
        {
            if(isset($_POST['insert'])){
                // count more value
                $addcounter=$counter-1;
                $sql2="update $tbl_name set visitors='$addcounter'";
                $result2=mysql_query($sql2);
                echo "You 're visitors No. ";
                echo $addcounter;

                mysql_close();

        header("Location: http://www.yoursite.com/your_page.php");
        exit;
            } // closing brace for if(isset($_POST['insert']))
        }
        else 
        {
                //$counter=0;
                echo "<script type='text/javascript'>alert('$message');</script>";  
                mysql_close();

        }


?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<form method="POST" action="">
    <input type="submit" name="insert" value="submit" />
</form>

</body>
</html>

或者这个:

<?php

        $host="localhost"; // Host name 
        $username="root"; // Mysql username 
        $password=""; // Mysql password 
        $db_name="mypage"; // Database name 
        $tbl_name="counter"; // Table name 
        $message = "offer End";

        // Connect to server and select database.
        mysql_connect("$host", "$username", "$password")or die("cannot connect to server "); 
        mysql_select_db("$db_name")or die("cannot select DB");

        $sql="SELECT * FROM $tbl_name";
        $result=mysql_query($sql);
        $rows=mysql_fetch_array($result);
        $counter=$rows['visitors'];

        // if have no counter value set counter = 1
        if(empty($counter))
        {
                $counter=1;
                $sql1="INSERT INTO $tbl_name(visitors) VALUES('$counter')";
                $result1=mysql_query($sql1);

        }

    if(isset($_POST['insert'])){
        if($counter>1)
        {

                // count more value
                $addcounter=$counter-1;
                $sql2="update $tbl_name set visitors='$addcounter'";
                $result2=mysql_query($sql2);
                echo "You 're visitors No. ";
                echo $addcounter;

                mysql_close();

        header("Location: http://www.yoursite.com/your_page.php");
        exit;
            }
        } // closing brace for if(isset($_POST['insert']))
        else 
        {
                //$counter=0;
                echo "<script type='text/javascript'>alert('$message');</script>";  
                mysql_close();

        }


?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<form method="POST" action="">
    <input type="submit" name="insert" value="submit" />
</form>

</body>
</html>