【问题标题】:How to code in OO PHP?如何在 OO PHP 中编写代码?
【发布时间】:2010-02-02 13:34:46
【问题描述】:

我如何在 OO PHP 中做到这一点:

  1. 一个表单('in newstudent.php')要求用户输入他的姓名、课程和年份。
  2. 选择“提交”按钮后,页面将转到“records.php” records.php - 包含一个显示所有记录的表(列:名称、课程、年份)
  3. 当用户选择“提交”时,新记录将被添加到具有名为 STUDENTS 的表的数据库中

SQL 代码

CREATE TABLE STUDENTS(
   NAME VARCHAR(25) NOT NULL,
   COURSE VARCHAR(25) NOT NULL,
   YEAR INT NOT NULL,
CONSTRAINT STUDENTS_PK PRIMARY KEY(NAME));

*请不要介意主键,因为我知道使用名称作为主键是不准确的。这只是为了举例。

还有...如何使用 OO PHP 操作数据库中的数据? 谢谢

【问题讨论】:

    标签: php mysql database oop forms


    【解决方案1】:
    1. 读一本书
    2. 谷歌搜索
    3. 创建学生对象
    4. 创建数据库对象
    5. 查询数据库对象以插入学生对象

    【讨论】:

      【解决方案2】:

      好吧,如果您想切换到在数据库中表示学生的 OO 方法,那么看起来类似于下面定义的“学生”类怎么样(尽管这是非常基本的,并且在任何方面都不是完整的 ORM )。它会将您带到ActiveRecord 风格方法的一半。

      请注意,我假设您将使用整数 id 列,不这样做会使整个班级烦人。

      class Student {
      
         var $id = -1;
         var $name;
         var $course;
         var $year; 
      
         public static function newFromID ($id) 
         {
           //fetch a row ($row) from the students table matching the given id
           //perhaps returning false if the student doesn't exist?
           return self::newFromRow($row);
         }
      
         // this method should return a new student object given a specific db row
         // and should be called from newFromID.  This function means that if the table
         // changes, modifications only have to be made in one place
         public static function newFromRow($row) 
         {
           $obj = new Student();
      
           //fill in the fields of the object based on the content of the row
      
           return $obj;
         }
      
         public static function getAllStudents()
         {
           //perhaps return an array of student objects, by doing a broad select,
           //and passing each row to newFromRow?
         }
      
         //this should save the object to the database, either inserting or updating as appropriate
         public function save()
         {
           if($this->id == -1) 
           {
              //insert, store the auto_increment id in $this->id
           } else {
              //update
           }
      
         }
      
      }
      

      所以,创建一个新学生,并将其保存到数据库中:

      $student = new Student();
      
      $student->name = "John Smith";
      $student->course = "French";
      $student->year = 2;
      
      $student->save();
      

      实际上,使用现有的 ORM 系统通常更明智,但如果这不是一个选项,您可以考虑编写自己的。

      【讨论】:

      • 如何在函数 save() 中添加用于插入的 sql 代码?还是这样: $result = mysql_query("INSERT INTO STUDENT(NAME, COURSE, YEAR) values('$_POST[Name]', '$_POST[Course]', '$_POST[Year]'") 或 die( mysql_error()); ??
      • 首先,停止。现在谷歌“SQL 注入”,阅读你找到的内容,然后转到 mysql_real_escape_string 函数上的 php 文档。切勿以刚刚粘贴的方式执行查询。
      • 一旦你修改了该查询使其安全,你可以或多或少只做 mysql_query($the_safe_query);然后,您将需要 mysql_insert_id 函数来获取最后插入的列的 id。
      【解决方案3】:

      也许您在谈论 ORM - 对象关系映射模式?有许多不同的方法可以将 SQL 数据对象映射到 PHP 类:Propel、Doctrine(都可以与 Symfony 框架一起使用)、ActiveRecord。

      当然,您可以尝试实现自己的 ORM 系统。您需要为此 ORM 编写数据访问层、描述 SQL 表的类和许多其他内容。这很有趣(出于教育目的)。

      【讨论】:

        猜你喜欢
        • 2012-05-22
        • 2016-11-24
        • 1970-01-01
        • 2011-05-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-10
        • 2018-03-31
        相关资源
        最近更新 更多