Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
67 views
in Technique[技术] by (71.8m points)

javascript - Update Mysql database table using ajax

My ajax is not giving me a success error or a fail error whenever I click my submit button. My MySQL is properly set up. I want to update my database because for some reason the PHP just puts a 3 in one column, so I just want to update it to the actual account type(1, 2, 3, &4, &5). I have 2 ajax functions on the submit button, so maybe that might be the case but I've seen websites with more than 2 ajax functions, so that shouldn't be the issue. I think it has to do with the data in ajax but I don't know what to put there.

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.");
                        
                        }); 
                    }
                })

My PHP files have the same thing but just the plan is a different value. For example 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);
?>
question from:https://stackoverflow.com/questions/65660464/update-mysql-database-table-using-ajax

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Ok let me guide you a bit

  • To change just a plan with repeating this ajax code and if/else statements and by checking .html() == and .text() == its not a good thing .. it will be better to catch the value of radio input or select whatever you use .. check the next example

$(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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...