【问题标题】:HTML PHP database - edit/delete button - passing valueHTML PHP 数据库 - 编辑/删除按钮 - 传递值
【发布时间】:2021-05-20 21:58:53
【问题描述】:

我对 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";
        ?>

【问题讨论】:

标签: php html mysql database


【解决方案1】:

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

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

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

【讨论】:

  • 感谢 Neha 为我指明了正确的方向,我可以理解上述内容并了解主键是如何传递的。确认页面等有效,但是当我单击“确定”删除记录时,我重定向到错误 404:找不到文件或目录。我假设delete.php 页面没有获取ID?还是网址“delete.php?id=159847263”
  • id 正在正确传递。您需要在此处标出重定向页面:header("Location: index.php"); //redirect to relevant page。我不知道你的重定向页面是什么,所以我只是将它指向index.php
  • 发现了问题,不得不在脚本中更改重定向。完美运行,但对我的数据库没有影响,记录没有被删除
  • 试试Location:livedashboard.php"。我认为它只需要文件名,除非重定向文件是不同的文件夹位置
  • sry 编辑了我以前的版本,希望在您阅读以前的版本之前完成它。一切正常,只是删除没有发生在服务器端
猜你喜欢
  • 1970-01-01
  • 2015-12-15
  • 1970-01-01
  • 1970-01-01
  • 2016-10-23
  • 1970-01-01
  • 1970-01-01
  • 2016-08-04
  • 1970-01-01
相关资源
最近更新 更多