php - HTML PHP 数据库 - 编辑/删除按钮 - 传递值

标签 php html mysql database

我对 php 相当业余,对 js 更是如此。我创建了一个带有编辑和删除按钮的数据库表,如屏幕截图所示。 (如果有人也能明白为什么我的表头和表体之间有一个间隙那就太好了,我不知道为什么会出现这种情况,似乎不是CSS)。

这个想法是只需单击删除按钮,将“AwbNo”传递到delete.php页面,以便从数据库中删除整行,然后自动返回到页面以查看更新的表,如果可以避免重定向,甚至可以让操作更顺畅。任何帮助将不胜感激,希望我下面的代码足以提供帮助

因此选择要删除的行>单击删除>确认>从数据库中删除行。这就是我想要实现的过程

example database screenshot

<table class="table">
                <thead>
                  <tr>
                    <th>Awb Number</th>
                    <th>Vessel</th>
                    <th>Client</th>
                    <th>Pieces</th>
                    <th>Total Weight</th>
                    <th>Carrier</th>
                    <th>Sender</th>
                    <th>Status</th>
                    <th>Arrival Date</th>
                    <th>Action</th>
                  </tr>
                </thead>
                <tbody>
                    <?php               //BEGINNING OF PHP
                    include("login/dbinfo.inc.php");
                    $comm=@mysql_connect(localhost,$username,$password);
                    $rs=@mysql_select_db($database) or die( "Unable to select database"); 

                    $sql="SELECT AwbNo, VesselName, ClientCode, Pieces, Weight, Carrier, Sender, Status, DATE_FORMAT(ArrivalDate, '%d-%m-%yyyy') FROM tbl_import";
                    $result = mysql_query($sql) or die("SELECT Error: ".mysql_error());
                    $num_rows = mysql_num_rows($result);
                    echo "<p>There are $num_rows records in the Customer table.</p>";
                    echo "<table class=\"table\">\n";
                    while ($get_info = mysql_fetch_array($result))
                    {
                        echo ("<tr>\n");
                        echo ("<td>".$get_info["AwbNo"]."</td>");
                        echo ("<td>".$get_info["VesselName"]."</td>");
                        echo ("<td>".$get_info["ClientCode"]."</td>");
                        echo ("<td>".$get_info["Pieces"]."</td>");
                        echo ("<td>".$get_info["Weight"]."</td>");
                        echo ("<td>".$get_info["Carrier"]."</td>");
                        echo ("<td>".$get_info["Sender"]."</td>");
                        echo ("<td>".$get_info["Status"]."</td>");
                        echo ("<td>".$get_info["ArrivalDate"]."</td>");
                        ?>
                            <td>
                            <div id="outer">
                                <div class="inner"><button type="submit" class="msgBtn" onClick="goToURL()" > Edit </button></div>
                                <div class="inner"><button type="submit" class="msgBtn2" onClick="goToURL1()"> Delete </button></div>
                                </div> 
                            </td>
                        <?php
                        echo ("</tr>\n");
                    }
                    echo "</table>\n";
                    mysql_close();
                    ?>                  <!--END OF PHP-->
                </tbody>
                </table>

下面是点击“编辑”或“删除”按钮时重定向用户页面的 js 脚本。

<script>
function goToURL() {
window.open('php/edit.php');
}
function goToURL1() {
  window.open('php/delete.php');
}
</script>

下面是假设的“delete.php”页面,用于从实时服务器上的数据库中删除记录,这只是一个示例,我不确定它是否正确。

<?php
        include("dbinfo.inc.php");
        $comm=@mysql_connect(localhost,$username,$password);
        $rs=@mysql_select_db($database) or die( "Unable to select database"); 
        $AwbNo=$_POST['AwbNo'];
        $sql="DELETE FROM  tbl_import where AwbNo=$AwbNo";
        mysql_query($sql)or die("Delete Error: ".mysql_error());
        mysql_close();
        echo "Record was successfully deleted.\n";
        ?>

最佳答案

您遇到的问题是因为您需要传递 AwbNo 在您的情况下的主键以及编辑/删除链接,以便正确的记录从数据库中选择。您的情况没有发生这种情况。

表格的代码需要类似于下面提到的编辑和删除链接。

echo '<td><a href="edit.php?id='.$get_info['AwbNo']. '">&nbspEdit&nbsp</a></td>';
echo '<td><a href="javascript:delete_user('.$get_info['AwbNo']. ')">&nbspDelete&nbsp</a></td>'

同时在同一页面中添加此脚本。

 <script>
        function delete_user(uid)
        {
            if (confirm('Are You Sure to Delete this Record?'))
            {
                window.location.href = 'delete.php?id=' + uid;
            }
        }
    </script> 

delete.php 可以只有以下代码:

  <?php
    
        include("dbinfo.inc.php");
        $comm=@mysql_connect(localhost,$username,$password);
        $rs=@mysql_select_db($database) or die( "Unable to select database"); 
        $id = $_GET['id']; // $id is now defined
        mysqli_query($conn,"DELETE FROM tbl_import where AwbNo='".$id."'");
        mysqli_close($conn);
        header("Location: index.php"); //redirect to relevant page
        
    ?>

关于php - HTML PHP 数据库 - 编辑/删除按钮 - 传递值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41857036/

相关文章:

php - PDO SSL - 奇怪的行为

php - 如何在 PHP 中动态定义一个变量

php - 需要从从 php 转换为 jquery 的数组中按值获取索引

html - 负左边距问题和神秘的 4px 差异

javascript - 我需要修改脚本来隐藏和显示 div

javascript - 如何使用 rv-each 构造动态 HTML

Python/MySQL 类型错误 : execute() takes from 2 to 4 positional arguments but 5 were given

mysql - 关于 mysql 的混淆,例如 search 和 = search

php - Mysql 正则表达式查询查找略有不同的重复项

php - 多个测试在 phing 中发生冲突