需要使用DAO编辑和删除

时间:2012-02-14 11:57:54

标签: php

我想知道是否有人可以帮我解决这个问题。 我有一个ReminderDAO类,包含删除,编辑,插入等方法以及带有构造函数和get和sets的Reminder类。

然后我有一个视图提醒,它只列出所有提醒。 我希望能够在此视图页面中添加编辑和删除功能。

要在我的ReminderDAO类中使用删除和编辑功能,我需要通过该功能传递提醒对象,我不太确定如何执行此操作。

如果有人能帮助我,那将会有很大的帮助,我对这种语言不熟悉,所以如果不是很好的代码,我会道歉。

提前谢谢!

提醒DAO     

class ReminderDAO extends DAO {

    public function __construct() {
        parent::__construct();
    }

    public function insert($reminder) {
        if (!isset($reminder)) {
            throw new Exception("Reminder required");
        }
        $sql = "INSERT INTO Reminders(member_id, title, details, reminder_type) VALUES (?, ?, ?, ?)";
        $params = array($reminder->getMember_id(), $reminder->getTitle(), $reminder->getDetails(), $reminder->getType());
        $stmt = $this->link->prepare($sql);
        $status = $stmt->execute($params);
        if ($status != true) {
            $errorInfo = $stmt->errorInfo();
            throw new Exception("Could not save Reminder: " . $errorInfo[2]);
        }

        $sql = "SELECT LAST_INSERT_ID()";
        $stmt = $this->link->prepare($sql);
        $status = $stmt->execute();
        if ($status != true) {
            $errorInfo = $stmt->errorInfo();
            throw new Exception("Could not retrieve new reminder's id: " . $errorInfo[2]);
        }
        $row = $stmt->fetch();
        $id = $row[0];
        $reminder->setId($id);
    }

    public function delete($reminder) {
        if (!isset($reminder)) {
            throw new Exception("Reminder required");
        }
        $id = $reminder->getId();
        if ($id == null) {
            throw new Exception("Reminder id required");
        }
        $sql = "DELETE FROM Reminders WHERE id = ?";
        $params = array($reminder->getId());
        $stmt = $this->link->prepare($sql);
        $status = $stmt->execute($params);
        if ($status != true) {
            $errorInfo = $stmt->errorInfo();
            throw new Exception("Could not delete reminder: " . $errorInfo[2]);
        }
    }

    public function update($reminder) {
        if (!isset($reminder)) {
            throw new Exception("Reminder required");
        }
        $id = $reminder->getId();
        if ($id == null) {
            throw new Exception("Reminder id required");
        }
        $sql = "UPDATE Reminders SET member_id = ?, title = ?, details = ?, reminder_type = ? WHERE id = ?";
        $params = array($reminder->getMember_id(), $reminder->getTitle(), $reminder->getDetails(), $reminder->getType());
        $stmt = $this->link->prepare($sql);
        $status = $stmt->execute($params);
        if ($status != true) {
            $errorInfo = $stmt->errorInfo();
            throw new Exception("Could not update Reminder: " . $errorInfo[2]);
        }
    }

    public function getReminder($id) {
        $sql = "SELECT * FROM Reminders WHERE id = ?";
        $params = array($id);
        $stmt = $this->link->prepare($sql);
        $status = $stmt->execute($params);
        if ($status != true) {
            $errorInfo = $stmt->errorInfo();
            throw new Exception("Could not retrieve Reminder: " . $errorInfo[2]);
        }

        $reminder = null;
        if ($stmt->rowCount == 1) {
            $row = $stmt->fetch();
            $id = $row['id'];
            $member_id = $row['member_id'];
            $title = $row['title'];
            $details = $row['details'];
            $type = $row['reminder_type'];
            $reminder = new ReminderDAO($id, $member_id, $title, $details, $type);
        }
        return $reminder;
    }

    public function getReminders() {
        $sql = "SELECT * FROM  Reminders";
        $stmt = $this->link->prepare($sql);
        $status = $stmt->execute();
        if ($status != true) {
            $errorInfo = $stmt->errorInfo();
            throw new Exception("Could not retrieve reminders: " . $errorInfo[2]);
        }

        $reminders = array();
        $row = $stmt->fetch();
        while ($row != null) {
            $id = $row['id'];
            $member_id = $row['member_id'];
            $title = $row['title'];
            $details = $row['details'];
            $type = $row['reminder_type'];


            $reminder = new Reminder($id, $member_id, $title, $details,  $type);
            $reminders[$id] = $reminder;

            $row = $stmt->fetch();
        }
        return $reminders;
    }
}
?>

提醒课程

<?php
class Reminder {
    private $id;
    private $member_id;
    private $title;
    private $details;
    private $reminder_type;


    public function __construct($i, $m_id, $title, $det, $type) {
        $this->id = $i;
        $this->member_id = $m_id;
        $this->title = $title;
        $this->details = $det;
        $this->reminder_type = $type;
    }
    public function getId() { return $this->id; }
    public function getMember_id() { return $this->member_id; }
    public function getTitle() { return $this->title; }
    public function getDetails() { return $this->details; }
    public function getType() { return $this->reminder_type; }


    public function setId($i) { $this->id = $i; }
    public function setMember_id($mID) { $this->member_id = $mID; }
    public function setTitle($t) { $this->title = $t; }
    public function setDetails($d) { $this->details = $d; }
    public function setType($type) { $this->reminder_type = $type; }


}
?>

查看提醒

<?php
ob_start();
require_once 'includes/Member.php';
require_once 'includes/MemberDAO.php';
require_once 'includes/Reminder.php';
require_once 'includes/ReminderDAO.php';
require_once 'includes/session.php';
confirm_logged_in(); // needs to come before any html because it does a redirect
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        require 'toolbar.php';
        $member = ($_SESSION['member']);

        $reminderDAO = new ReminderDAO();
        $reminders = $reminderDAO->getReminders();



        echo "<p>Hello " . $member->getFN() . "</p>";
        echo "<p>These are the current reminders:  </p>";


        foreach ($reminders as $rem) {
            echo "<b>Title:</b> " . $rem->getTitle() . "<br />";
            echo "<b>Details:</b> " . $rem->getDetails() . "<br />";
            echo "<b>Type: </b>" . $rem->getType() . "<br />";



           echo "</p>";

        }


        echo $display; ?>
        <a href="add_reminder_form.php">Add Reminder?</a>



    </body>
</html>
<?php ob_flush(); ?>

edit_reminder_form.php类

<?php
ob_start();
require_once 'includes/session.php';
require_once 'includes/Member.php';
require_once 'includes/MemberDAO.php';
require_once 'includes/Reminder.php';
require_once 'includes/ReminderDAO.php';
require_once 'includes/session.php';
confirm_logged_in(); // needs to come before any html because it does a redirect
?>

<?php

 $reminderDAO = new ReminderDAO();
 $reminder = $reminderDAO->getReminder($_GET['id']);




?>

<html>
    <head>
        <title>Edit Reminder</title>
    </head>
    <body>

        <table>
            <tr>
            <td>
            <h2>Edit Reminder</h2>
             <?php if (isset($_GET['errorMessage'])) echo "<p>".$_GET['errorMessage']."</p>"; ?>
               <form action="edit_reminder.php" method="POST">
                   Title: <input type="text" name="title" value="<?php $reminder->getTitle(); ?>" /><br/>
                   Details: <input type="text" name="details" value="<?php $reminder->getDetails()?> " /><br/>

                   <select name="reminder_type" value="<?php $reminder->getType();?>">
                <option value="Choose">Please choose a reminder type!</option>
                <option value="Bill">Bill</option>
                <option value="Shopping">Shopping</option>
                <option value="Event">Event</option>
                <option value="Birthday">Birthday</option>
                <option value="Other">Other</option>
                </select>
                <br />
                <input type="submit" name="reminder" value="Edit Reminder" />
               </form>
            <br />
            <a href ="view_reminders.php"> Cancel </a>
            </td>
            </tr>
        </table>
    </body>
    <?php
    //5.Close connection
    if(isset($connection)) {
     mysql_close($connection);
    }
    ?>
</html>

<?php ob_flush(); ?>

1 个答案:

答案 0 :(得分:0)

您可以将ID的{​​{1}}发送到编辑/删除提醒的下一页。

reminder

foreach ($reminders as $rem) { echo "<b>Title:</b> " . $rem->getTitle() . "<br />"; echo "<b>Details:</b> " . $rem->getDetails() . "<br />"; echo "<b>Type: </b>" . $rem->getType() . "<br />"; echo "[<a href='edit.php?id=" . $rem->getID() . "'>Edit</a>] "; echo "[<a href='delete.php?id=" . $rem->getID() . "'>Delete</a>] "; echo "</p>"; } 中,您使用ID(例如edit.php)获取提醒对象,使用ReminderDAO从数据库加载数据并创建一个填充了提醒值的表单。在该表单中,您还应该输入提醒ID,因此当他将表单提交到$_GET['id']时,您可以识别已编辑的提醒。 保存更改后,您可以使用Save changes功能将其重定向回提醒列表。

类似地,在header中,您可以使用ID删除提醒(例如delete.php),然后将用户重定向到提醒列表。