2015-04-17 57 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(刷新瀏覽器)減少數量... –