dongshang6062 2019-07-10 19:28
浏览 127
已采纳

如何在php while循环中使用Ajax运行SQL查询?

I have a table of Data which is pulled via SQL into DataTables. I want to use AJAX to run an SQL query which deleted the row based on $id = $row["id"];.

Index.php:

$link = mysqli_connect("localhost", "bradlyspicer_root", "", "bradlyspicer_ResellerDB");

$id = $_POST['id'];
$deleterow = "DELETE FROM Offences WHERE id = ?";
    if($stmt = mysqli_prepare($link, $deleterow)){ // $link being your connection
        mysqli_stmt_bind_param($stmt, "s", $id);
        mysqli_stmt_execute($stmt);
        echo 'success';
        echo $id;
    } else {
        echo 'fail!';
        printf("Error: %s.
", mysqli_stmt_error($stmt));
    }

Functions.php:

$id = $_POST['id'];
$deleterow = "DELETE FROM Offences WHERE id = ?";
    if($stmt = mysqli_prepare($link, $deleterow)){ // $link being your connection
        mysqli_stmt_bind_param($stmt, "s", $id);
        mysqli_stmt_execute($stmt);
        echo 'success';
    } else {
        echo 'fail!';
        printf("Error: %s.
", mysqli_stmt_error($stmt));
    }

Custom.js:

$( ".delbtn" ).click(function(){
    var itemID = $(this).attr("itemID");
    console.log(itemID)
    $.ajax({
        url:"functions.php", //the page containing php script
        data: { id: itemID}, // itemID passed as id
        type: "POST", //request type
        success:function(result){
            alert(result);
        },
        error: function() {
            alert('Error occured');
        }
    });
});

I can't find where I pass the $id in the button from Index.php to Functions.php, any explanation would be appreciated.

Update: Since updating the script and trying to Debug, I'm not getting much of a response from the error which outputs:

fail!Error: .

  • 写回答

2条回答 默认 最新

  • donglu6303 2019-07-10 19:40
    关注

    Index.php:

    Add a delete button identifier class delbtn and a data attribute that carries this row's id data-itemID

    <?php
        while($row = mysqli_fetch_array($dataTablesResult)){
            $id = $row["id"];
            echo '
            <tr>
              <td>
                <button type="button" data-itemID="'.$id.'" class="delbtn btn btn-danger" >Remove</button>
              </td>
            </tr>
            ';
        }
    ?>
    

    Functions.php:

    Capture $_POST['id'] sent by ajax

    $id = $_POST['id'];
    $deleterow = "DELETE FROM Offences WHERE id = ?";
    if($stmt = mysqli_prepare($link, $deleterow)){ // $link being your connection
        mysqli_stmt_bind_param($stmt, "s", $id);
        mysqli_stmt_execute($stmt);
    }
    

    Custom.js:

    Run the jQuery function when a button with .delbtn class is clicked. Capture and store the row id from data attribute as $(this).data("itemID"). Then send the data using data: { id: itemID} within ajax request

    $(".delbtn").click(function(){
        itemID = $(this).data("itemID");
        $.ajax({
            url:"functions.php", //the page containing php script
            data: { id: itemID}, // itemID passed as id
            type: "POST", //request type
            success:function(result){
                alert(result);
            }
        });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上