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

时间:2017-01-25 16:43:56

标签: php html mysql database

我非常喜欢使用php,甚至更喜欢使用js。我创建了一个带有编辑和放大器的数据库表。删除按钮,如屏幕截图所示。 (如果有人也能看出为什么我的标题和表格的主体之间存在差距会很好,我不知道为什么会出现这种情况,似乎不是css)。

想法是只需单击删除按钮,将'AwbNo'传递给delete.php页面,以便从数据库中删除整行,然后自动返回页面查看更新的表,如果可以避免重定向,甚至更好只是为了使操作更平滑。任何帮助将不胜感激,希望我的下面的代码足够援助

所以选择要删除的行>点击删除>确认>从db中删除的行。这是我打算实现的过程

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'页面从实时服务器上的db中删除记录,这只是一个例子,我不确定它是否正确。

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

1 个答案:

答案 0 :(得分:1)

欢迎使用StackOverflow!

您遇到的问题是因为您需要传递primary key AwbNo以及编辑/删除链接,以便从DB中选择正确的记录。在你的情况下不会发生这种情况。

该表的代码需要看起来像下面提到的编辑&amp;删除链接。

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

    ?>