简体   繁体   English

按下按钮时更新数据

[英]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. 因此,当员工按下“添加访问”按钮时,访问次数应增加1,并且日期也应该是最新的。

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. 我不知道id我应该将其放在代码中的特定位置还是问题所在。

<?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. 所以,你能告诉我哪里出了问题。

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. 打开错误报告和/或检查日志。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM