By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,241 Members | 760 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,241 IT Pros & Developers. It's quick & easy.

How to insert data from database using oops php concepts?

P: 2
Hi,

I am new for PHP OOP concept.
Pls give some example source code for "How to insert data using oops concept?" I want to improve my knowledge. pls any one help me....

I know below the basic concept
insert_db.php
Expand|Select|Wrap|Line Numbers
  1. $sql="INSERT INTO Persons (FirstName, LastName, Age)
  2. VALUES
  3. ('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";
  4.  
  5. if (!mysql_query($sql,$con))
  6.   {
  7.   die('Error: ' . mysql_error());
  8.   }
  9. echo "1 record added";
  10.  
  11. mysql_close($con)
  12.  
advance thanks,
Jul 25 '11 #1
Share this Question
Share on Google+
3 Replies


100+
P: 1,059
OOP Concept and insertion is not same thing
they are not even related to each other


even though a simple example

Expand|Select|Wrap|Line Numbers
  1. /*
  2. Table tblustomer
  3. CustomerId,
  4. CustomerName,
  5. CustomerPhoneNumber
  6. */
  7. <?php
  8. class TableCustomer
  9. {
  10.  private $conn;
  11.  private $ErrorMessage;
  12.  private $res;
  13.  public function Insert($CustomerName,$CustomerPhoneNumber='')
  14.  {
  15.   if($CustomerName=='')
  16.   {
  17.    $this->ErrorMessage="Customer Name Cannot be Empty";
  18.    return ;
  19.   }
  20.   $InsertSQL="INSERT INTO tblustomer(CustomerName,CustomerPhoneNumber) values('".$CustomerName."','".$CustomerPhoneNumber."')";
  21.   $res=mysql_query($InsertSQL,$this->conn);
  22.   if($res==false)
  23.   {
  24.    $this->ErrorMessage=mysql_error($conn);
  25.    return;
  26.   }
  27.   else
  28.   {
  29.    $this->ErrorMessage='';
  30.   }//there is also try catch through in php but i dont use.
  31.  
  32.  }
  33.  function __construct($user, $password,$host)
  34.  {
  35.   $this->conn= mysql_connect($host,$user, $password);
  36.   if($this->conn==false)
  37.   {
  38.     $this->ErrorMessage="Connection Failed";
  39.   }
  40.   else $this->ErrorMessage='';
  41.  }
  42.  function __destruct()
  43.  {
  44.   if($this->res!=false)
  45.   {
  46.     mysql_free_result($this->res);
  47.   }
  48.   if($this->conn!=false)
  49.  {
  50.    mysql_close($this->conn);
  51.  }
  52.  }
  53. };
  54. ?>
  55.  

you will also get available widely used and well documented class, try pear, i am not sure how to use it, never tried.

you can try though
Jul 26 '11 #2

P: 2
Thank you for your help,

I try to insert one person detail, it's inserted successfully. But same data insert 3 times in the DB. Please help me to solve the error.

I had this data in the Database.
Expand|Select|Wrap|Line Numbers
  1. id      name        dob           gen
  2. 1       James       12-03-1977     M
  3. 2       James       12-03-1977     M
  4. 3       James       12-03-1977     M
  5.  

Expand|Select|Wrap|Line Numbers
  1. class Detail
  2. {
  3. function savePerson_detail($vars){
  4.     foreach($vars as $key => $value){
  5.        if(is_numeric($key) && $value >0){
  6.          $qry = sprintf("INSERT INTO cca_student_list(per_name, per_dob, per_gen) VALUES('%s', '%s', '%s')",
  7.         mysql_real_escape_string($vars['name']),
  8.         mysql_real_escape_string($vars['dob']),
  9.                 mysql_real_escape_string($vars['gen']));
  10.         mysql_query($qry) or die(mysql_error());
  11.          if($qry)
  12.     {
  13.     print 'Successfully Insert your details';
  14.     }
  15.    }
  16. }
  17.  
I used this form:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. detail =new Detail();
  3. if(isset($_POST['btnSaveDetail'])){
  4.  
  5.     $detail ->savePerson_detail($_POST);
  6.  
  7. }
  8.  
  9. ?>
  10.  
Aug 1 '11 #3

P: 20
Abisiva,

This isn't a direct answer to your question, but if you are just starting out learning OOP principles, then I would strongly recommend you save yourself a lot of effort and instead begin with the currently popular approach of using an MVC framework.

I highly recommend you spending some time with CodeIgniter (codeigniter.com) as I find it quite small and the online tutorials are fairly easy reading.

This way you'll jump ahead of the crowd rather than playing catch-up.
Aug 1 '11 #4

Post your reply

Sign in to post your reply or Sign up for a free account.