按钮点击php / html运行mysql查询而不刷新

时间:2015-09-29 10:36:32

标签: php mysql ajax html5

这似乎不起作用!没有任何反应。

PHP FILE INDEX.PHP

<button class="buttonminus" onclick="minuswarmupset()" type="submit"  value="-"> - </button> 

<script>

    function minuswarmupset () {
      $.ajax({
        url:"minus_warmup_sets.php", //the page containing php script
        type: "POST", //request type
        success:function(result){


       }
     });
     }

     </script>

MYSQL文件

 <?php include "../config.php"; ?>
    <?php include('../databaselist.php'); ?>

    <?php

    $query = "UPDATE users SET 

    `warmup_sets_today`=(`warmup_sets_today`+1),
      WHERE username='$username'";

    $result=mysql_query($query) or die("Query Failed : ".mysql_error());
    echo " ";

    ?>

3 个答案:

答案 0 :(得分:0)

我建议从查询中删除逗号:

$query = "UPDATE users SET 

`warmup_sets_today`=(`warmup_sets_today`+1)
  WHERE username='$username'";

答案 1 :(得分:0)

首先,您必须将数据发送到您的php文件

function minuswarmupset () 
{ 
$.ajax({
  method: "POST",
  url: "minus_warmup_sets.php",
  data: { uname: "USERNAME" }
})
  .done(function( msg ) {
    alert( "Data Saved: " + msg );
  });
}

然后你必须从php文件

中捕获发布的值
<?php include "../config.php"; ?>
<?php include('../databaselist.php'); ?>

<?php
$username=$_POST'uname'];
$query = "UPDATE users SET 

`warmup_sets_today`=(`warmup_sets_today`+1)  WHERE username='$username'";

$result=mysql_query($query) or die("Query Failed : ".mysql_error());
echo " ";

?>

答案 2 :(得分:0)

删除WHERE子句之前的逗号:

$query = "UPDATE users SET 
`warmup_sets_today`=(`warmup_sets_today`+1)
  WHERE username='$username'";

..并打印回复。例如,使用 mysql_affected_rows()

mysql_query($query) or die("Query Failed : " . mysql_error());

echo "Records updated: " . mysql_affected_rows();