【问题标题】:Update Mysql database table using ajax使用ajax更新Mysql数据库表
【发布时间】:2021-04-16 00:26:14
【问题描述】:

每当我单击提交按钮时,我的 ajax 都没有给我一个成功错误或失败错误。我的 MySQL 设置正确。我想更新我的数据库,因为出于某种原因,PHP 只是在一列中放了一个 3,所以我只想将它更新为实际的帐户类型(1、2、3、&4、&5)。我在提交按钮上有 2 个 ajax 函数,所以可能是这种情况,但我见过有超过 2 个 ajax 函数的网站,所以这不应该是问题。我认为这与 ajax 中的数据有关,但我不知道该放什么。

ajax:

$("#signUpButton").click(function(){
                    if($("#dproPlayerAccount").html() === "Pro Player Account") {
                        $.ajax({
                            type: "POST",
                            url: "proPlayAccountClicked.php",
                            dataType: "json"
                        }).done(function(proUpdate){
                            console.log(proUpdate);
                        }).fail(function(xhr, textStatus, errorThrown) {
                        
                            console.log("Error Requesting. Please Try Again Later.");
                        
                        }); }
                    else if($("#dfreePlayerAccount").text() === "Free Player Account") {
                        $.ajax({
                            type: "POST",
                            url: "freePlayAccountClicked.php",
                            dataType: "json"
                        }).done(function(freeUpdate){
                            console.log(freeUpdate);
                        }).fail(function(xhr, textStatus, errorThrown) {
                        
                            console.log("Error Requesting. Please Try Again Later.");
                        
                        });
                    }
                    else if($("#dpremiumPlayerAccount").text() === "Premium Player Account") {
                        $.ajax({
                            type: "POST",
                            url: "premiumPlayAccountClicked.php",
                            dataType: "json"
                        }).done(function(update){
                            console.log(update);
                        }).fail(function(xhr, textStatus, errorThrown) {
                        
                            console.log("Error Requesting. Please Try Again Later.");
                        
                        });
                    }
                    else if($("#dproCreatorAccount").text() === "Pro Creator Account") {
                        $.ajax({
                            type: "POST",
                            url: "proCreatorAccountClicked.php",
                            dataType: "json"
                        }).done(function(cproUpdate){
                            console.log(cproUpdate);
                        }).fail(function(xhr, textStatus, errorThrown) {
                        
                            console.log("Error Requesting. Please Try Again Later.");
                        
                        }); }
                    else if($("#dpremiumCreatorAccount").text() === "Premium Creator Account") {
                        $.ajax({
                            type: "POST",
                            url: "premiumCreatorAccountClicked.php",
                            dataType: "json"
                        }).done(function(cpremiumUpdate){
                            console.log(cpremiumUpdate);
                        }).fail(function(xhr, textStatus, errorThrown) {
                        
                            console.log("Error Requesting. Please Try Again Later.");
                        
                        }); 
                    }
                })

我的 PHP 文件具有相同的内容,但只是计划的值不同。例如 plan="1"(2, 3, &4, &5)。

<?php 
  session_start();
  $link = mysqli_connect("********", "********", "********", "********");
    
     
    if(mysqli_connect_error()) {
         
         die("Couldn't connect to the database. try again later.");
         
     } 
 
      $query = "SELECT * FROM `users`";

      if($result = mysqli_query($link, $query)) {
          
          $row = mysqli_fetch_array($result);
          
      }
      
      //The plan value is different for the 5 different PHP files.  
      $query = "UPDATE KingOfQuiz SET plan='1' WHERE ".$row['id'];
      echo json_encode($query);
?>

【问题讨论】:

  • 首先分离你的代码 .. 用简单的console.log('yes') 尝试 if/else 语句,看看 if/else 是否正常工作.. 然后直接使用每个 ajax,看看你会得到什么..在您检查一切正常后将两者结合在一起并检查
  • 好的,我去看看!
  • 控制台不显示任何消息
  • WHERE 子句中的 UPDATE 查询中存在错误。应该有类似" ... WHERE theColumn = " . $row['id'];
  • 我没有看到 MySQL 将'3' 放在任何地方。我什至没有看到INSERT... 另外,您应该使用准备好的语句,这样您的数据库就不会受到攻击。

标签: javascript php jquery mysql ajax


【解决方案1】:

好的,让我指导你一下

  • 仅通过重复此 ajax 代码和 if/else 语句以及检查 .html() ==.text() == 来更改计划,这不是一件好事.. 捕获无线电输入的值或选择任何你会更好使用 .. 检查下一个示例

$(document).ready(function(){
  $('#singupform').on('submit' , function(e){
    e.preventDefault();
    var plan = $('#plan').val();
    console.log(plan);
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="singupform">
  <label>Select Your Plan</label>
  <select id="plan">
    <option value="1">Free</option>
    <option value="2">Pro</option>
    <option value="3">Premium</option>
  </select>
  <button id="signupbutton">Submit</button>
</form>
  • 仅使用一个 ajax 和一个 php 文件来更改基于使用 data: {} 的选择值的计划,也不需要 dataType : 'json',而您只检查一件事

$(document).ready(function(){
  $('#singupform').on('submit' , function(e){
    e.preventDefault();
    var plan = $('#plan').val();
    $.ajax({
      type: "POST",
      url: "ChangeUserPlan.php",
      data : {plan : plan},
    }).done(function(updated){
         console.log(updated);
    }).fail(function(xhr, textStatus, errorThrown) {
         console.log("Error Requesting. Please Try Again Later.");
    });
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="singupform">
  <label>Select Your Plan</label>
  <select id="plan">
    <option value="1">Free</option>
    <option value="2">Pro</option>
    <option value="3">Premium</option>
  </select>
  <button id="signupbutton">Submit</button>
</form>

在你的 ChangeUserPlan.php 中

echo $_post['plan'];

在此之后,您的代码应该在控制台中返回计划值。然后开始更新表并在您的 php 中针对 sql 注入进行检查...

【讨论】:

  • 谢谢,但由于某种原因,我的 PHP 文件根本不知道帖子值或帖子
  • 这是一个简单的答案,我只是在过去几周内无法工作。
  • @PalDhillon 我认为这是因为文件目录 .. 尝试直接从浏览器 url 访问 php 文件,然后将所有 url 复制/粘贴到 ajax 中的url : 'paste the full url here' .. 请PalDhillon 编码本身就很复杂,所以从简单开始,检查每一步,然后一切都会朝着正确的方向发展
  • 我还有一个问题,检查 html/text 不起作用
  • @PalDhillon 不要使用html() ===,它真的很难抓住。正确的值。如果您想比较文本,请使用.text().toLowerCase().trim() === 'text in lowercase here' .. ..toLowerCase() 将文本小写并使用trim() 以避免在if($("#dfreePlayerAccount").text().toLowerCase().trim() === "free player account") 上方的代码中出现任何右/左空格.. .. 祝你好运
猜你喜欢
  • 1970-01-01
  • 2013-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-20
相关资源
最近更新 更多