douwanc63652 2017-01-25 16:43
浏览 91
已采纳

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

im rather amateur with php and even more so with js. I have created a database table with an edit & delete button, as shown in the screenshot. (if anyone is also able to see why there is a gap between my header and body of table that would be great, i have no clue why this is cropping up, doesnt seem to be css).

The idea is to just click the delete button, pass the 'AwbNo' over to the delete.php page in order to delete the entire row from the database, and then automatically return to the page to see the updated table, if redirection can be avoided, even better just to make the operation smoother. Any help would be greatly appreciated, hope my code below is enough for aid

so select a row to delete>click delete>confirmation>row deleted from db. That is the process i am aiming to achieve

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\">
";
                    while ($get_info = mysql_fetch_array($result))
                    {
                        echo ("<tr>
");
                        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>
");
                    }
                    echo "</table>
";
                    mysql_close();
                    ?>                  <!--END OF PHP-->
                </tbody>
                </table>

Below is the js script to redirect user page when clicking on the 'edit' or 'delete' button.

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

And below is the supposing 'delete.php' page to delete the record from the db on a live server, this is only an example, im not exactly sure if it is correct.

<?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.
";
        ?>
  • 写回答

1条回答 默认 最新

  • duanjuduo4573 2017-01-25 17:17
    关注

    Welcome to StackOverflow!

    The issue your having is because you need to pass the primary key that AwbNo in you case, along with the Edit /Delete link, so that the correct record is selected from DB. This is not happening in your case.

    The code for the table needs to look something like mentioned below for the edit & delete links.

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

    Also add this script in same page.

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

    delete.php can have just this code:

      <?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
    
        ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图