dongwuzun4630 2016-01-25 21:25
浏览 57

AJAX PHP MYSQL值更新无法正常工作

i have this script which updates a counter in a mysql database but it's not working at all and i can't see what wrong as i am new into php and ajax:

$(function () {
        $('.press_me').click(function(){

    var button_ID= $(this).parent('tr').attr($row['ID']);
    $.post('counter.php', {button_ID: button_ID}, function(result){

          $($row['Teme_nefacute']).html(result);
        });
request.done(function( msg ) {

                                    alert('Success');
                                    return;

                              });
                              request.fail(function(jqXHR, textStatus) {
                                    alert( "Request failed: " + textStatus );
                                });
});
});

and counter.php:

    <?php
// Connection to database
  $connection=mysqli_connect("host","user","pass","db");
// Check connection
  if (mysqli_connect_errno())
    {
    echo 'NOT_OK';
    //echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

// Increasing the current value with 1
 mysqli_query($connection,"UPDATE table SET amount= (amount + 1) WHERE ID=' " . $_POST["button_ID"] ." ' ");

$get_count= mysqli_query("SELECT * FROM `table` WHERE `ID`=' " . $_POST["button_ID"] . " ' ");
$count_data= mysqli_fetch_array($new_count_data);
echo($count_data["amount"]);

  mysqli_close($connection);

  ?>

can anyone help me make it work async?

  • 写回答

1条回答 默认 最新

  • douqie6454 2016-01-25 21:35
    关注

    First of all, PHP is a backend technology which works in server and sends result to client (browser, curl, wget etc.) Javascript is a frontend technology which works in the client. So in that point of view

    Your php code should be between of <?php //code ?> tags, and it would executed in backend then result would be printed in javascript code.

    As a result your view should be a php file. But it should contain html,js and php mixed.

       <script>
        $(function () {
          $('.press_me').click(function() {
    
            var button_ID= $(this).parent('tr').attr("<?php echo $row['ID']; ?>");
            $.post('counter.php', {button_ID: button_ID}, function(result) {
    
              $("<?php echo $row['Teme_nefacute']; ?>").html(result);
            });
            request.done(function(msg) {
    
              alert('Success');
              return;
    
            });
            request.fail(function(jqXHR, textStatus) {
              alert( "Request failed: " + textStatus );
            });
          });
        });
        </script>
    

    Your php code looks OK. When you try like this what is the result ?

    Updated :

    If your table name is table then please place it between this characters `` like in SELECT query. table has a special meaning in SQL.

    评论

报告相同问题?

悬赏问题

  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线