-2

I've problem of my click counter program. I want to stop running on pressing F5 or page refresh in browser which already connects with phpmyadmin database and i want it works when button click only...

<?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 Answers1

1

As I said in comments and to show you a graphical way of doing this, use isset() with a conditional statement and wrap your entire executable code inside that and based on your named submit button.

I.e.:

<?php

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

        $host="localhost"; // Host name 

...

mysql_close();
        }

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

You can also use a header to redirect to the same page, inside the conditional statement.

I.e.:

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

Sidenote: Make sure you're not outputting before header.


  • You should probably remove onclick="" from your submit button. There isn't a JS reference for it in your posted code.

Footnote(s):

Your present code is open to SQL injection. Use mysqli with prepared statements, or PDO with prepared statements, they're much safer.

  • mysql_* functions are deprecated and will be removed from future PHP releases.

Edit, full rewrite #2:

N.B.:

Replace http://www.yoursite.com/your_page.php below with your site and the page name you are using for the script.

Try either this one: (another below this one)

<?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>

or this one:

<?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>
Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • it is works only once when i load page first time, but refresh page still works. Here I need to do the things when only button clicks, when I refresh page "confirm re-submission page" alert comes with CONTINUE and CANCEL button that I really don't requires.....Always thanks for your vote – Sharique Sheikh Apr 17 '15 at 15:10
  • @ShariqueSheikh What I posted, you need to place your code inside of. This should work and will not do anything with your database otherwise. – Funk Forty Niner Apr 17 '15 at 15:13
  • Sir FRED - I m doing the suggested thing but still f5 (refresh browser) decrease the number count... – Sharique Sheikh Apr 17 '15 at 15:21
  • @ShariqueSheikh Reload my answer and look under **Edit, full rewrite:**. Copy/paste into a new file and re-upload and reload it. That will not cause your DB to rewrite, only when the button is pressed will it do that. – Funk Forty Niner Apr 17 '15 at 15:24
  • Sir FRED - my database has no problem, is still working well. I think I fail to understand my problem to u....it reloads well as u say it works when i click on submit button but at the same time when I press F5 its also works that i don't want to work, As we use POST method in
    tag it through the "confirm Form Resubmission" alert into the browser with continue and cancel button, when I cancel. When I click on CONTINUE button it works as per program which I don't want.
    – Sharique Sheikh Apr 17 '15 at 15:39
  • I want just load program and user has only one option to decrease count with the help of submit button. If user uses page refresh by using F5 button or refresh page it would be not works. – Sharique Sheikh Apr 17 '15 at 15:41
  • @ShariqueSheikh Reload my answer again and look and read under **Edit, full rewrite #2:**. One of those should work the way you like it to. – Funk Forty Niner Apr 17 '15 at 15:57
  • @ShariqueSheikh So, where are we with this? – Funk Forty Niner Apr 17 '15 at 21:14
  • Sir FRED - First script working great...Thanks its working good, but message not shown on the page which we use in code echo "You 're visitors No. "; echo $addcounter; pls help once THANKS AGAIN :) – Sharique Sheikh Apr 18 '15 at 08:13
  • Sir FRED - Can u pls know me how echo message n variable value prints...as asked previous comment... – Sharique Sheikh Apr 19 '15 at 07:42