How to insert data from database using oops php concepts?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • abisiva
    New Member
    • Jul 2011
    • 2

    #1

    How to insert data from database using oops php concepts?

    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
    Code:
    $sql="INSERT INTO Persons (FirstName, LastName, Age)
    VALUES
    ('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";
    
    if (!mysql_query($sql,$con))
      {
      die('Error: ' . mysql_error());
      }
    echo "1 record added";
    
    mysql_close($con)
    advance thanks,
  • johny10151981
    Top Contributor
    • Jan 2010
    • 1059

    #2
    OOP Concept and insertion is not same thing
    they are not even related to each other


    even though a simple example

    Code:
    /*
    Table tblustomer
    CustomerId,
    CustomerName,
    CustomerPhoneNumber
    */
    <?php
    class TableCustomer
    {
     private $conn;
     private $ErrorMessage;
     private $res;
     public function Insert($CustomerName,$CustomerPhoneNumber='')
     {
      if($CustomerName=='')
      {
       $this->ErrorMessage="Customer Name Cannot be Empty";
       return ;
      }
      $InsertSQL="INSERT INTO tblustomer(CustomerName,CustomerPhoneNumber) values('".$CustomerName."','".$CustomerPhoneNumber."')";
      $res=mysql_query($InsertSQL,$this->conn);
      if($res==false)
      {
       $this->ErrorMessage=mysql_error($conn);
       return;
      }
      else
      {
       $this->ErrorMessage='';
      }//there is also try catch through in php but i dont use.
     
     }
     function __construct($user, $password,$host)
     {
      $this->conn= mysql_connect($host,$user, $password);
      if($this->conn==false)
      {
        $this->ErrorMessage="Connection Failed";
      }
      else $this->ErrorMessage='';
     }
     function __destruct()
     {
      if($this->res!=false)
      {
        mysql_free_result($this->res);
      }
      if($this->conn!=false)
     {
       mysql_close($this->conn);
     }
     }
    };
    ?>

    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

    Comment

    • abisiva
      New Member
      • Jul 2011
      • 2

      #3
      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.
      Code:
      id      name        dob           gen
      1       James       12-03-1977     M
      2       James       12-03-1977     M
      3       James       12-03-1977     M

      Code:
      class Detail
      {
      function savePerson_detail($vars){
      	foreach($vars as $key => $value){
      	   if(is_numeric($key) && $value >0){
      	     $qry = sprintf("INSERT INTO cca_student_list(per_name, per_dob, per_gen) VALUES('%s', '%s', '%s')",
      		mysql_real_escape_string($vars['name']),
      		mysql_real_escape_string($vars['dob']),
                      mysql_real_escape_string($vars['gen']));
      		mysql_query($qry) or die(mysql_error());
               if($qry)
      	{
      	print 'Successfully Insert your details';
      	}
         }
      }
      I used this form:
      Code:
      <?php
      detail =new Detail();
      if(isset($_POST['btnSaveDetail'])){
      
      	$detail ->savePerson_detail($_POST);
      
      }
      
      ?>

      Comment

      • John Doe
        New Member
        • Jun 2011
        • 20

        #4
        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.co m) 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.

        Comment

        Working...