簡體   English   中英

如何使用bootstrap和Ajax基於所選ID從MySQL數據庫獲取數據?

[英]How can I get the data from MySQL database based on the selected ID using bootstrap and Ajax?

我有兩個php頁面,第一個php頁面包含表格,該表格選擇使用Bootstrap Modal編輯哪條記錄,第二個php頁面處理MySQL數據庫中要更新的數據。

這是第一個php頁面(edit_account.php)

<?php
$cn = mysqli_connect("localhost","root","","testdb");
$sql = "SELECT * FROM tblinfo";
$qry = mysqli_query($cn,$sql);
$nrows = mysqli_num_rows($qry);
if ($nrows > 0) {
    while ($rows = mysqli_fetch_array($qry)) {
        $id = $rows['userid'];
        $fn = $rows['fullname'];
?>
        <tr>
            <td><?php echo $id; ?></td>
            <td><?php echo $fn; ?></td>
            <td><a href = "#edit<?php echo $id; ?>" data-toggle = "modal" class = "btn btn-success btn-sm">Edit</a></td>
        </tr>

        <div class = "modal" id = "edit<?php echo $id; ?>">
            <div class = "modal-dialog">
                <div class = "modal-content">
                    <div class = "modal-header">
                        <h5 class = "modal-title">Edit Information</h5>
                        <button class = "close" data-dismiss = "modal">&times;</button>
                    </div>
                    <div class = "modal-body">
                        <input type = "text" id = "user_id" value = "<?php echo $id; ?>" class = "form-control" />
                        <input type = "text" id = "full_name" value = "<?php echo $fn; ?>" class = "form-control" />
                    </div>
                    <div class = "modal-footer">
                        <button class = "btn btn-primary btn-sm" id = "update<?php echo $id; ?>">Update</button>
                        <button class = "btn btn-danger btn-sm" data-dismiss = "modal">Close</button>
                    </div>
                </div>
            </div>
        </div>

        <script>
            $("#update<?php echo $id; ?>").click(function() {
                var id = $("#user_id").val();
                var fn = $("#full_name").val();
                if (confirm("Are you sure you want update this record?")) {
                    $.ajax({
                        url: "edit_account_process.php",
                        type: "POST",
                        data: {
                            userid: id,
                            fullname: fn
                        },
                        success: function(data) {
                            $("#showData").html(data);
                        }
                    });
                }
            });
        </script>                       
 <?php      

    }
}
 ?>

第二個php頁面(edit_account_process.php):(此頁面將用於更新記錄)

<?php
$cn = mysqli_connect("localhost","root","","testdb");
$id = $_POST['userid'];
$fn = $_POST['fullname'];

//for Testing
echo $id . " " . $fn;

$sql = "UPDATE tblinfo SET fullname = '$fn' WHERE userid = '$id' ";
$result = mysqli_query($cn,$sql);

if ($result) {
    echo "<script>alert('Successfully updated!');</script>";
}
else {
    echo "<script>alert('Unable to update the record!');</script>";
}
?>

我的代碼現在發生的是它總是選擇我數據庫中的第一條記錄,即使我試圖選擇其他記錄。 這是

錯誤圖片

如何定位將要更新的特定ID?

先感謝您

您不需要定義多個模態,使用多個腳本 - 正如您現在所做的那樣(在循環內部,您創建一個新模式和一個新腳本)。

而是添加一個動態賦值的模態,並使用一個腳本來偵聽按鈕上的點擊。 該單擊從data-*屬性中獲取所需的值,並將它們放入模態中的輸入字段的值中。

<?php
$cn = mysqli_connect("localhost","root","","testdb");
$sql = "SELECT * FROM tblinfo";
$qry = mysqli_query($cn,$sql);
$nrows = mysqli_num_rows($qry);
if ($nrows > 0) {
    while ($rows = mysqli_fetch_array($qry)) {
        $id = $rows['userid'];
        $fn = $rows['fullname'];
        ?>
        <tr>
            <td><?php echo $id; ?></td>
            <td><?php echo $fn; ?></td>
            <td><a href="#" class="btn btn-success btn-sm toggleEditModal" data-uid="<?php echo $id; ?>" data-fullname="<?php echo $fn; ?>">Edit</a></td>
        </tr>                  
        <?php      
    }
}
?>

<script>
    $(".toggleEditModal").on("click", function(e) {
        e.preventDefault();
        $('#user_id').val($(this).data('uid'));
        $('#full_name').val($(this).data('fullname'));
        $("#editModal").modal('show');
    });

    $("#updateUser").click(function() {
        var id = $("#user_id").val();
        var fn = $("#full_name").val();
        if (confirm("Are you sure you want update this record?")) {
            $.ajax({
                url: "edit_account_process.php",
                type: "POST",
                data: {
                    userid: id,
                    fullname: fn
                },
                success: function(data) {
                    $("#showData").html(data);
                    $("#editModal").modal('hide');
                }
            });
        }
    });
</script>     

<div class = "modal" id = "editModal">
    <div class = "modal-dialog">
        <div class = "modal-content">
            <div class = "modal-header">
                <h5 class = "modal-title">Edit Information</h5>
                <button class = "close" data-dismiss = "modal">&times;</button>
            </div>
            <div class = "modal-body">
                <input type = "text" id = "user_id" value = "" class = "form-control" />
                <input type = "text" id = "full_name" value = "" class = "form-control" />
            </div>
            <div class = "modal-footer">
                <button class = "btn btn-primary btn-sm" id = "updateUser">Update</button>
                <button class = "btn btn-danger btn-sm" data-dismiss = "modal">Close</button>
            </div>
        </div>
    </div>
</div>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM