按下按钮时更新数据

时间:2014-04-23 18:48:08

标签: php mysqli

此代码用于更新客户的访问次数。 因此,当员工按下"添加访问"按钮,访问次数应增加1,日期也应该是最新的。

代码中没有错误,但我在更新中遇到问题。 问题是访问次数增加,即使用户没有点击"添加访问"按钮。

所以,我不知道代码中的错误是什么。

那么,你能帮我吗?

 //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>";
      }

        ?>

行。这是我在编辑更新部分的代码后得到的 但它没有执行该功能。 我不知道我应该将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');
            }
           }
            ?>

那么,你能告诉我哪里出错了。

1 个答案:

答案 0 :(得分:0)

在您编辑代码之前,我完成了代码,这对我来说很重要,可能与您的问题有关:

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

其他一些说明:

// 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":
{

如果刷新页面或按下后退按钮,则没有任何内容可以阻止重新提交表单。

进行一些基本的错误检查。逐行遵循代码的逻辑流程并输出:

echo 'here';
exit;

直到您缩小具体问题区域。打开错误报告和/或检查日志。