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 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据