views:

125

answers:

3

How can I do this in OO PHP:

  1. A form ('in newstudent.php') asks the user to enter his name, course and year.
  2. After selecting 'Submit' button, the page will go to 'records.php' records.php - contains a table that displays all the records (columns: name, course, year)
  3. when the user selects 'Submit', the new record will be added to the database which has a table named STUDENTS

SQL code

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

*please don't mind about the primary key coz i know it's not accurate to use name as the primary key. this is just for exmple purposes.

and also...How can i manipulate data in DB using OO PHP? Thanks

A: 

Maybe you talk about ORM - Object Relation Mapping patterns? There are many different approaches to get mapped SQL data objects to PHP classes: Propel, Doctrine (both can be used with Symfony framework), ActiveRecord.

Of course, you can try to implement your own ORM system. You need to write data access layer for this ORM, classes which describes SQL tables and many other things. It is very interesting (for educational purposes).

Sergey Kuznetsov
+4  A: 
  1. Read a book
  2. Search Google
  3. Create Student Object
  4. Create Database Object
  5. Query Database Object to insert Student Object
Eddie
any site suggestions?
anonymous
+1  A: 

Well, if you want to switch to a OO method of representing students in a database, how about a 'Student' class that looks something like the definition below (although this is very basic, and not a full ORM in any way). It takes you halfway to an ActiveRecord style approach.

Note that I have assumed you will use an integer id column, not doing so makes the whole class annoying.

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
     }

   }

}

So, to create a new student, and save it to the database:

$student = new Student();

$student->name = "John Smith";
$student->course = "French";
$student->year = 2;

$student->save();

In reality, it is often more sensible to use an existing ORM system, but if that isn't an option, you can consider writing your own.

Kazar
how can i add the sql code for insert inside function save()?still like this:$result = mysql_query("INSERT INTO STUDENT(NAME, COURSE, YEAR) values('$_POST[Name]', '$_POST[Course]', '$_POST[Year]'") or die(mysql_error());??
anonymous
First off, stop. Now google "SQL injection", read what you find, and then go to the php docs on the mysql_real_escape_string function. Never execute a query in the way you have just pasted.
Kazar
Once you have modified that query so it is secure, you can more or less just do mysql_query($the_safe_query); You will then need the mysql_insert_id function in order to get the id of the last inserted column.
Kazar