[英]Updating database variable using ajax and php
I'm having an issue and i can't seem to see where the error is in my code. 我遇到问题,似乎看不到代码中的错误。 I'm trying to update a variable
total
in my database using a ajax post function on my webpage. 我正在尝试使用网页上的ajax发布函数更新数据库中的变量
total
。 The function works as the alert is generated with the correct values when i click the button but my database is not updated. 当我单击按钮但我的数据库未更新时,该警报将生成具有正确值的警报,因此该功能起作用。 Here is the javascript function:
这是javascript函数:
function buyeqc(){
var total = $('#eqctotal').val();
$.ajax({
url:"buyeqc.php", //the page containing php script
data: 'total='+total,
type: "POST", //request type
success:function(result){
if (total < "1") {
alert("Please enter a value greater than 0");
} else if (total > "1") {
alert("Thank you for your purchase of "+total+" EQC. Please refresh the page to view your updated balance.");
}
}
});
}
And here is the PHP script that it's posting to: 这是发布到的PHP脚本:
<?php
if (isset($_GET['total'])) {
session_start();
include_once 'dbh.inc.php';
$user = $_SESSION['u_uid'];
$eqcbal = $_SESSION['EQCBal'];
$total = $_GET['total'];
$sql = "UPDATE users SET EQCBal = '$total' WHERE user_uid = '$user';";
mysqli_query($conn, $sql);
}
?>
If you can point me in the right direction as to where my error is I would be greatful. 如果您能向我指出我的错误所在的正确方向,那我将非常感激。 I have a feeling it's something very simple or small!
我觉得这很简单或很小! Thanks.
谢谢。
It because $total in the your php file is NULL, You shold change it to 因为您的php文件中的$ total为NULL,所以您将其更改为
`$total = $_POST['total'];`
When you send a post ajax request, data will store in $_POST 当您发送post ajax请求时,数据将存储在$ _POST中
您发出发布请求,而PHP您已收到请求
Thanks for the answers - it was the issue with having $_GET instead of $_POST. 感谢您的回答-这是使用$ _GET而不是$ _POST的问题。 Also i was pointing to the wrong directory for my dbh.inc.php.
我也指向我的dbh.inc.php错误的目录。 Silly errors :) Thanks for the help!
愚蠢的错误:)感谢您的帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.