0

I want to make a program what will count clicks after click button.

I have this code but it don't work. I use mysqli to connect to database and I use query to insert value to database and query to select from database.

<html>
    <head>
        <meta charset="UTF-8">
            <title>Click</title>
    </head>
    <body>
        <form action="#" method="post">
            <input type="submit" name="click" value="Klikni mě">
            <br>
            <?php
            if(isset($_POST["click"])){
                $connection=new mysqli("hidden","hidden","hidden","hidden");
                if($connection == false){
                    die("Sorry jako");
                }
                $query="INSERT INTO klik (klikcount) VALUES ('$klik')";
                if($connection->query($query) == false){
                    die("Promiň");
                }
                $sql="SELECT klikcount FROM klik";
                $result=$connection->query($sql);
                if($result->num_rows>0){
                    while($row=$result->fetch_assoc()){
                        echo $row["klikcount"];
                    }
                }
                $klik=$klik+1;
            }
            ?>
        </form>
    </body>
</html>

thanks.

2
  • Any errors? What "dont work"? Commented Oct 22, 2018 at 17:46
  • 2
    $klik is not defined before you attempt to insert it. Commented Oct 22, 2018 at 17:46

1 Answer 1

1

I try solve your code and I made some changes.

  1. Change position of "$klik = $klik+1;"
  2. Add another SELECT

My new code:

<html>
    <head>
        <meta charset="UTF-8">
        <title>Click</title>
    </head>
    <body>
        <form action="#" method="post">
            <input type="submit" name="click" value="Klikni mě">
            <br>
            <?php
            if(isset($_POST["click"])) {
                $connection = new mysqli("hidden","hidden","hidden","hidden");

                if($connection == false) {
                    die("Sorry jako");
                }

                $sql="SELECT klikcount FROM klik";
                $result=$connection->query($sql);

                if($result->num_rows > 0) {
                    while($row = $result->fetch_assoc()){
                        $klik = $row["klikcount"];
                    }
                }

                $klik = $klik+1;
                $query = "INSERT INTO klik (klikcount) VALUES ('$klik')";

                if($connection->query($query) == false) {
                    die("Promiň");
                }

                $sql = "SELECT klikcount FROM klik";
                $result = $connection->query($sql);

                if($result->num_rows > 0) {
                    while($row = $result->fetch_assoc()) {
                        echo $row["klikcount"];
                    }
                }
            }
            ?>
        </form>
    </body>
</html>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.