阻止PHP在刷新时重复查询

时间:2016-05-02 08:10:52

标签: php html mysql

我正在制作一个Web应用程序,而且每次重新加载页面时,我都无法弄清楚如何阻止PHP对我的数据库进行查询。

基本上,当提交表单时,我希望PHP对添加变量的数据库进行查询,但是,因为它已将这些值保存在$ _POST中,所以它每隔时间进行查询我用相同的值刷新。

我希望它能够进行查询,然后可能取消设置$ _POST值或某些内容以使其不满足if条件,并且当提交按钮没有时,它会阻止它使用重复值查询数据库用新值更新它们。

对不起,如果那令人费解,我试图尽我所能解释我的问题。

这是PHP

<?php  

//Require login gile -- Fatal error if not found
require_once('login.php');
require_once('app.html');

//Connect w/ MySQL database  [login variables]
$con = new mysqli($hn, $un, $pw, $db);

//If connection error -> kill application and display error
if ($con->connect_error) die('Connect Error (' . $con->connect_errno . ') '. $con->connect_error);

//If both goal and difficulty are set 
if (!empty($_POST['goal']) && !empty($_POST['difficulty'])) {
    $goal = get_post($con, 'goal');
    $difficulty = get_post($con, 'difficulty');
    $query = "INSERT INTO items VALUES" . "('$goal', 1, '$difficulty', NULL)";
    $result = $con->query($query);
    if(!$result) echo "INSERT failed: $query<br>" . $con->error . "<br><br>";
}

unset($_POST['goal']);
unset($_POST['difficulty']);


//close connection
$con->close();

function get_post($conn, $var) {
    return $conn->real_escape_string($_POST[$var]);
}

?>

HTML

<!DOCTYPE html>
<html>
<head>
<title>Testing</title>
<!--Page Style-->
<link type="text/css" rel="stylesheet" href="Style.css">
<!--jQuery-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>

<!--RateYo | http://prrashi.github.io/rateYo/-->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/rateYo/2.0.1/jquery.rateyo.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/rateYo/2.0.1/jquery.rateyo.min.js"></script>
</head>
<body>
<div id="parent1">
    <form id="goalForm" method="post" action="PHP.php">
        <!--Goal Value-->
        <input id="goalName" type="text" name="goal" maxlength="150" placeholder="Type your goal here!">

        <!--Star Rating-->
        <div id="rateYo"></div><br> 

        <!--Value from stars to be submitted-->
        <input id="dif" type="hidden" name="difficulty">

        <input class="but1" id="firstSubmit" type="button" value="Submit"> 
        <input class="but1" id="submitton" type="submit" style="display:none" value="Difficulty?">
    </form>
</div>
<script>

    $("#goalName").val("");


    //First Submit Onclick
    $("#firstSubmit").click(function() {
        if($("#goalName").val() == "") {
            alert("Please insert a value.");
        }
        else {

            $("#goalName").fadeOut("slow" , function() { 
                $("#rateYo").rateYo({
                    numStars: 10,
                    rating: 0, 
                    fullStar: true,
                    starWidth: "70px",
                    ratedFill: "#E74C3C",
                    maxValue: 10
                }); 

                $("#rateYo").fadeIn("slow");
            });

            $(this).hide();
            $("#submitton").show();
        }
    });

    //Star Submit
    $("#goalForm").submit(function() {

        var $rateYo = $("#rateYo").rateYo();
        var rating = $rateYo.rateYo("rating");

        if($("#goalName").val() == "") {
            alert("Please insert a value.");
            return false;
        }
        else if(rating == 0) {
            alert("Please set a difficulty level.");
            return false;
        }
        else {
            $("#dif").val(rating);
        }



    });


</script>

1 个答案:

答案 0 :(得分:2)

试试吧PLZ

<?php  

//Require login gile -- Fatal error if not found
require_once('login.php');
require_once('app.html');

//Connect w/ MySQL database  [login variables]
$con = new mysqli($hn, $un, $pw, $db);

//If connection error -> kill application and display error
if ($con->connect_error) die('Connect Error (' . $con->connect_errno . ') '. $con->connect_error);

//If both goal and difficulty are set 
if (!empty($_POST['goal']) && !empty($_POST['difficulty'])) {
    $goal = get_post($con, 'goal');
    $difficulty = get_post($con, 'difficulty');
    $query = "INSERT INTO items VALUES" . "('$goal', 1, '$difficulty', NULL)";
    $result = $con->query($query);
    if(!$result) echo "INSERT failed: $query<br>" . $con->error . "<br><br>";
    //You can write samepage name in location as window.location='abc.php'
    echo "<script>window.location=''</script>";
}

//close connection
$con->close();

function get_post($conn, $var) {
    return $conn->real_escape_string($_POST[$var]);
}

?>