doukai2839 2016-01-30 13:21
浏览 23
已采纳

将MYSQL行id传递给javascript变量

I have this script which is triggered when a button with the class .press_me is pressed.The buttons are on a column from a php generated mysql table:

$result = mysqli_query($con,"SELECT * FROM tbname");
echo "<table id='main'>"; 
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td class='right-middle user'>" . $row['ID'] . "</td>";
echo "<td class='right-middle user'>" . $row['Nume'] . "</td>";
echo "<td class='right-middle done'>" . $row['Teme_facute'] . "</td>";
echo "<td class='right-middle check'>" . "<img src='img/check.png' class='press_me'>" ."</td>";
echo "<td class='right-middle undone'>" . $row['Teme_nefacute'] . "</td>";
echo "<td class='right-middle uncheck'>" . "<img src='img/uncheck.png'>" . "</td>";
echo "<td class='side-table resetDone'>" . "<img src='img/resetDone.png'>" . "</td>";
echo "<td class='side-table resetUndone'>" . "<img src='img/resetUndone.png'>" . "</td>";
echo "</tr>";
}
echo "</table>";

And the script:

<script>
   $(function (){
            $('.press_me').click(function(){
        var id=<?php echo json_decode('$row[ID]'); ?>;          
            var request = $.ajax({
                                    type: "POST",
                                    url: "counter.php"
                                  });
                                  request.done(function( msg ) {

                                        alert('Success');
                                       location.reload();
                                        return;

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

And counter.php:

<?php
echo $_POST["id"];
if(!empty($_POST["id"]))
{
    $id = $_POST["id"];

    $connection=mysqli_connect("host","user","pass","db");
    if (mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
        exit;
    }

    mysqli_query($connection,"UPDATE tbname SET amount= (amount+ 1) WHERE ID = '" . $id . "'");

    mysqli_close($connection);
    echo 'OK';
}
else
{
    echo 'NO ID PASSED';
}
?>

I'm having trouble updating only the value on the same row as the button pressed.When i run the page in this configuration counter.php returns no id passed and i think the problem is with the passing of the row id. Can anyone help me update only the value on the row with the pressed button?

I'm aware of sql injection but it's not the main problem now

  • 写回答

3条回答 默认 最新

  • 普通网友 2016-01-30 13:27
    关注

    Your id is empty try this

    echo "<td class='right-middle check'>" . "<img data-id='{$row['ID']}' src='img/check.png' class='press_me'>" ."</td>";
    

    And in the script use this

      var id=$(this).data("id");  
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献