简体   繁体   中英

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. 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:

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

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

`$total = $_POST['total'];`

When you send a post ajax request, data will store in $_POST

您发出发布请求,而PHP您已收到请求

Thanks for the answers - it was the issue with having $_GET instead of $_POST. Also i was pointing to the wrong directory for my dbh.inc.php. Silly errors :) Thanks for the help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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