将MYSQL行id传递给javascript变量

时间:2016-01-30 13:21:33

标签: javascript php jquery mysql

我有这个脚本,当按下类.press_me的按钮时触发。按钮位于php生成的mysql表的列上:

$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>";

脚本:

<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>

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';
}
?>

我无法仅更新按下按钮的同一行上的值。当我在此配置中运行页面时counter.php返回没有ID传递,我认为问题随着行id的传递。任何人都可以帮助我用按下的按钮更新行上的值吗?

我知道sql注入但它现在不是主要问题

3 个答案:

答案 0 :(得分:2)

你的身份证是空的 试试这个

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

在脚本中使用此

  var id=$(this).data("id");  

答案 1 :(得分:2)

更改您的javascript,看起来您根本没有发送数据

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

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

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

答案 2 :(得分:0)

替换以下脚本:

<script>
 $(function (){
    $('.press_me').click(function(){
      var id=<?php echo $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>
  

注意:我假设您正在处理单个记录。如果不是那么它   会出错。

如果这样做有误,请将.press_me行替换为以下内容:

$id = $row['ID'];
echo "<td class='right-middle check'>" . "<img src='img/check.png' class='press_me' id='<?php print($id);?>' >" ."</td>";

脚本就像:

var id = $(this).attr("id");

希望这对你有所帮助!