2015-04-17 50 views
-2

我遇到了点击计数器程序的问题。我想停止在其已经与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

使用'isset()函数'/'空()'或标题。 –

+0

可以详细说明一下...用我的代码感谢ADVANCE –

回答

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引用。

脚注(S):

你现在的代码是开放的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> 
+0

这是一次只有一次,当我第一次加载页面,但刷新页面仍然有效。在这里,我需要做的事情只有按钮点击时,当我刷新页面“确认重新提交页面”警报带有CONTINUE和CANCEL按钮,我真的不需要.....总是感谢你的投票 –

+0

@ShariqueSheikh我发布的内容,你需要把你的代码放在里面。这应该工作,否则不会对您的数据库做任何事情。 –

+0

弗里德爵士 - 我做了建议的事情,但仍然F5(刷新浏览器)减少数量... –