使用javascript / AJAX在mysql表中插入数据

时间:2016-12-28 12:12:42

标签: javascript php mysql ajax

当用户点击按钮时我想更新mysql表中的值,但不知何故我做错了什么都没有。

<button class="btn btn-primary" id= "openchest" onclick="insertvalue();"> Open </button>

的javascript

function insertvalue(){
   makeRequest('addgold.php');
}

编辑:更改了javascript但它仍然没有做任何事情.. addgold.php不会显示任何mysql错误。

function insertvalue(){



$.ajax({
   type: "POST",
            url: "addgold.php",
            cache: false,
            data:{id:'openchest'},
            }).done(function( msg ) {  console.log(msg);
                });

}

addgold.php

<?php include ("connection.php"); 
if(isset($_REQUEST)) {    
      $sql= "UPDATE members SET coins = coins + 10 WHERE id ='".mysqli_real_escape_string($link, $_SESSION['id'])."' LIMIT 1";
      $result = mysqli_query($link, $sql);    
}
?>

1 个答案:

答案 0 :(得分:0)

you have to use ajax function on click and dont forget to include jquery libray    

function insertvalue(){
    $.ajax({
       type: "POST",
                url: "addgold.php",
                cache: false,
                data:{id:'anything'},
                }).done(function( msg ) {  console.log(msg);
                    });

    }