Dalia
Dalia

Reputation: 7

update data when a button pressed

This code is for updating the number of visits for a customer. So, when the employee presses on "Add visit" button, the number of visits should increases by 1 and also the date should be up-to-date.

There is no error in the code, but I'm facing a problem in the update. the problem is that the number of visits increased even if the user doesn't click on "Add visit" button.

So, I don't know what is the mistake in the code.

So, can you help me please.

 //update visit
        $visitno= $row['visit'];
        $currentdate = date('Y-m-d');
        $disc0 = "0%";
        $disc1 = "5%";
        $disc2 = "10%";
        $disc3 = "Free Service";

        switch ($visitno){
            case "1":
            {

                echo "<center><b>";
                echo "The client has 1 visit";
                echo "<br/>";
                echo "To update the visits, press the button";
                echo "<br/>";

                echo "<button id='shiny' onclick='alertFunction()'>Add visit</button>";
                echo "</center></b>";

                $query1= "UPDATE clients.clients SET visit='$visitno'+1, vdate='$currentdate', dis='$disc0' WHERE $category = '$criteria' AND visit='1'" ;
                $result1= mysqli_query($con, $query1);

                 if(!$result1){
                     echo "Error ". mysqli_error($con);
                      echo "<br>";
                    }//END IF

            }// end of case1
            break;
        case "2":
            {
            echo "<center><b>";
                echo "The client has 2 visit";
                echo "<br/>";
                echo "To update the visits, press the button";
                echo "<br/>";
                echo "<button id='shiny' onclick='alertFunction()'>Add visit</button>";
                echo "</center></b>";
                $query1= "UPDATE clients.clients SET visit='$visitno'+1, vdate='$currentdate', dis='$disc0' WHERE $category = '$criteria' AND visit='2'" ;
                $result1= mysqli_query($con, $query1);

                 if(!$result1){
                     echo "Error ". mysqli_error($con);
                      echo "<br>";
                    }//END IF

               }// end of case2
            break;
        case "3":
            {
                echo "<center><b>";
                echo "The client has 3 visit";
                echo "<br/>";
                echo "To update the visits, press the button";
                echo "<br/>";
                echo "<button id='shiny' onclick='alertFunction()'>Add visit</button>";
                echo "</center></b>";
                $query1= "UPDATE clients.clients SET visit='$visitno'+1, vdate='$currentdate', dis='$disc1' WHERE $category = '$criteria' AND visit='3'" ;
                $result1= mysqli_query($con, $query1);


                 if(!$result1){
                     echo "Error ". mysqli_error($con);
                      echo "<br>";
                    }//END IF
            }// end of case3
            break;
        case "4":
            {
               echo "<center><b>";
                echo "The client has 4 visit";
                echo "<br/>";
                echo "To update the visits, press the button";
                echo "<br/>";
                echo "<button id='shiny' onclick='alertFunction()'>Add visit</button>";
                echo "</center></b>";
                $query1= "UPDATE clients.clients SET visit='$visitno'+1, vdate='$currentdate', dis='$disc2' WHERE $category = '$criteria' AND visit='4'" ;
                $result1= mysqli_query($con, $query1);


                 if(!$result1){
                     echo "Error ". mysqli_error($con);
                      echo "<br>";
                    }//END IF
            }// end of case4
            break;
        case "5":
            {
               echo "<center><b>";
                echo "The client has 5 visit";
                echo "<br/>";
                echo "To update the visits, press the button";
                echo "<br/>";
                echo "<button id='shiny' onclick='alertFunction()'>Add visit</button>";
                echo "</center></b>";
                $query1= "UPDATE clients.clients SET visit='$visitno'+1, vdate='$currentdate', dis= '$disc3' WHERE $category = '$criteria' AND visit='5'" ;
                $result1= mysqli_query($con, $query1);


                 if(!$result1){
                     echo "Error ". mysqli_error($con);
                      echo "<br>";
                    }//END IF
            }// end of case5
            break;
        case "6":
            {
            echo "<center><b>";
                echo "Cannot add more visits!";
                echo "</center></b>";
            }// end of case6
            break;

        } //end of switch
        echo "<script>
        function alertFunction()
        {
            var visitnu = $visitno +1 ;
            alert('Client has ' + visitnu + ' visits now');
            }
</script>";
      }

        ?>

OK. This what I got after editing the code for the update part But it is not executing the function. I don't know id should I put it in a specific place in the code or where is the problem.

<?php     
$visitno= $row['visit'];
            $disc0 = "0%";
            $disc1 = "5%";
            $disc2 = "10%";
            $disc3 = "Free Service";

            if ($visitno == 6){
                echo "Cannot add more visit!";
                updatevisit(6,$disc3);
            } else if($visitno == 5){
                updatevisit(5,$disc2);
            } else if($visitno == 4){
                updatevisit(4, $disc1);
            } else if($visitno == 3){
                updatevisit(3, $disc0);
            } else if($visitno == 2){
                updatevisit(2, $disc0);
            } else if($visitno == 1){
                updatevisit(1, $disc0);
            }
            }  


             function updatevisit($visitno, $disc) {
            $currentdate = date('Y-m-d');
            $newvisit = $visitno +1;
            $discount = $disc;
            echo "The customer has $visitno visits";
            echo "Please press the button to update the visits";
           echo "<form method='post' action='tryupdate.php'>";
            echo "<input type='submit' id='updatevisit' name='updatevisit' value='Add visit'/>";
            echo "</form>";
            if(isset($_POST['updatevisit'])){
            $query1 = "UPDATE clients.clients SET visit='$newvisit', vdate='$currentdate', dis='$discount'" ;
            $result1 = mysqli_query($con, $query1) or die ('Error Updating');
            }
           }
            ?>

So, can you tell me where is the mistake.

Upvotes: 0

Views: 129

Answers (1)

mister martin
mister martin

Reputation: 6252

I walked through the code before you edited it, the main thing that stood out to me which might be related to your problem:

// this line is not finished
$result= mysqli_query($con, $quer

Some other notes:

// align is mispelled
echo '<tr style="background-color: #80E6CC;color:white;" aligh="center">';

// incorrect use of styles, should be: style='padding:15px'
echo "<tr><td align='center'padding:15px>";

// you don't need brackets here
case "1":
{

You have nothing in place to prevent forms from being resubmitted if the page is refreshed or back button is pressed.

Do some basic error checking. Follow the logical flow of the code line by line and output:

echo 'here';
exit;

Until you narrow down the specific problem area. Turn on error reporting and / or check your logs.

Upvotes: 0

Related Questions