簡體   English   中英

使用ajax和php更新數據庫變量

[英]Updating database variable using ajax and php

我遇到問題,似乎看不到代碼中的錯誤。 我正在嘗試使用網頁上的ajax發布函數更新數據庫中的變量total 當我單擊按鈕但我的數據庫未更新時,該警報將生成具有正確值的警報,因此該功能起作用。 這是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.");
    }
   }
 });
 } 

這是發布到的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);
}
?>

如果您能向我指出我的錯誤所在的正確方向,那我將非常感激。 我覺得這很簡單或很小! 謝謝。

因為您的php文件中的$ total為NULL,所以您將其更改為

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

當您發送post ajax請求時,數據將存儲在$ _POST中

您發出發布請求,而PHP您已收到請求

感謝您的回答-這是使用$ _GET而不是$ _POST的問題。 我也指向我的dbh.inc.php錯誤的目錄。 愚蠢的錯誤:)感謝您的幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM