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.

    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮