doucu7525 2014-02-13 01:31
浏览 43
已采纳

PHP事件侦听器中的多个回显删除编辑按钮

I am trying a trivial PHP assignement. I am running my own SQL server locally and I have created a DB on it called student. This database contains many tables. One of them is called announcement. The fields of this table are id, date, subject, text.

I am asked to display those announcements to a user that has the authority to delete and/or modify those entries of the DB. Each entry needs to be seperated from the next one and each entry has to have it's own Delete and Edit button. New entries can also be added to the database so the # of entries currently on the DB is not known.

So far I have done something like this:

<?php
    $db = mysql_connect("localhost", "root", "");
    mysql_select_db("student",$db);
    mysql_set_charset('utf8',$db);
    $result = mysql_query("SELECT * FROM announcement",$db);
    $announcementID = 1;
    WHILE($myrow = mysql_fetch_array($result))
    {
        echo "<br><h2>Announcement No".$announcementID."</h2>";
        echo "<input type=\"submit\" name=\"Delete\" value=\"Delete\"><input type=\"submit\" name=\"Edit\" value=\"Edit\"><br>";
        echo "<br>Date: ".$myrow["date"];
        echo "<br>Subject: ".$myrow["subject"];
        echo "<br>Text: ".$myrow["text"];
        $announcementID=$announcementID+1;
        echo '<br><hr />';
    }
 ?>

This is a part of a larger php file that displays a webpage with the entries properly formatted.

Although I do create the separate buttons needed for each distinct announcement I do not think this can work out since I can't create an ActionListener (forgive me but I do not know how this is called in PHP) for those buttons and I am not even sure it is possible considering that all of those buttons will have the same name. Any workaround?

  • 写回答

3条回答 默认 最新

  • douxing5199 2014-02-13 02:02
    关注

    For PHP to be able to indetify that the user has clicked the button, you would need to surround the each row of inputs. I've improved your code a little, as we need to pass over the ID of the announcement to the delete-record.php script for it to be able to identify which record to delete from the table.

    $db = mysql_connect("localhost", "root", "");
    mysql_select_db("student",$db);
    mysql_set_charset('utf8',$db);
    $result = mysql_query("SELECT * FROM announcement",$db);
    while($myrow = mysql_fetch_array($result))
    {
        echo '<form action="delete-record.php" method="POST">';
        echo '<input type="hidden" name="id" value="' . $myrow["id"] . '">';
        echo "<br><h2>Announcement No".$announcementID."</h2>";
        echo "<input type=\"submit\" name=\"delete\" value=\"delete\"><input type=\"submit\" name=\"edit\" value=\"edit\"><br>";
        echo "<br>Date: ".$myrow["date"];
        echo "<br>Subject: ".$myrow["subject"];
        echo "<br>Text: ".$myrow["text"];
        echo '<br><hr />';
        echo "</form>";
    }
    

    And then in the delete-record.php you can go with this:

    if(isset($_POST['id'], $_POST['delete'])) {
        $announcementid = $_POST['id'];
        mysql_query("DELETE FROM announcement WHERE id = $announcementid");
    }
    

    For future reference, instead of using $announcementID = $announcementID+1; you can simply use the post-incremental operator $announcementID++;

    I also suggest you to read up on MySQLi or PDO's prepared statements to secure yourself against SQL Injections and other SQL vulnerabilities.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效