gpt4 book ai didi

php - 阻止 PHP 在刷新时重复查询

转载 作者:行者123 更新时间:2023-11-29 06:13:50 25 4
gpt4 key购买 nike

我正在制作一个 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>

最佳答案

试试吧

<?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]);
}

?>

关于php - 阻止 PHP 在刷新时重复查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36977883/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com