473,396 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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
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
3 13506
johny10151981
1,059 1GB
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
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
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

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

Similar topics

0
by: Dinu | last post by:
Hi All I am trying to insert data within a transaction, into a Db2 database from asp.net. I have created a System DSN using ISeries Access Driver(32 bit) for Windows. I am then connecting...
1
by: Azel | last post by:
Hi, I am trying to learn ADO.net and I keep running into problems trying to insert data into my Access Database: data.mdb. here is my code: <code> // Database Variables
1
by: Pratik Gupte | last post by:
I have created a database in .mdf format, but I am unable to insert data into its tables. Can anybody help how to insert data using ASP.Net 2.0 using SQL Server 2005 Express edition in windows...
0
by: laxmanravi2002 | last post by:
dose vb supports OOPS concepts? plz explain me
1
by: Doc11 | last post by:
I'm trying to allow users insert data into a database using the form view. But when I click the insert button I get this error: Server Error in '/Customer Database' Application....
3
by: Indrachapa | last post by:
I want to get how to add data to table in SQL server database using a datatable using ASP.net
5
by: eihabisaac | last post by:
hi i have a field in my database (status) which have the value 0 or 1 how can i update these value using check box if checked then 1 if not checked then 0 and also how to insert values...
0
by: akshalika | last post by:
I am new to Biztalk. In my project we need to connect oracle database and insert data into oracle table using BizTalk project. I use WCF Adapter pack(SP2). I create biztalk project then using Consume...
1
by: psbasha | last post by:
Hi, Whether the application performance will be reduced ( Time taken will be more for processing the data ) with the implementation of OOPS Concepts with better memory ussage. Can you please...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.