simple object oriented add record to mysql

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
SoreE yes
Forum Commoner
Posts: 32
Joined: Wed Oct 11, 2006 3:59 am

simple object oriented add record to mysql

Post by SoreE yes »

I have developed a couple of small web sites using php procedural and have now decided to learn object oriented programming. I know a little about OOPS, but not enough to put the pieces together. If somebody could fill in the blank spaces I would be very grateful. I provide here a cut down example, where I wish to add a new record to an existing mysql database. There are four files. Firstly file called addvendor.html:

Code: Select all

<form name="form1" method="post" action="addex.php">
  <label>Enter businessname
  <input type="text" name="businessname">
  </label><br />
   <label>Enter 1st Line of Address
  <input type="text" name="firstlineaddress">
  </label><br />
  <input type="submit" name="submit">
</form>
Secondly a file called db_connect.php

Code: Select all

<?php
$dbconnect  = NULL;
$dbhost     = "localhost";
$dbusername = "root";
$dbuserpass = "";
$query      = NULL;
 
function db_connect($gardenwholesale)
{
   global $dbconnect, $dbhost, $dbusername, $dbuserpass;
   
   if (!$dbconnect) $dbconnect = mysql_connect($dbhost, $dbusername, $dbuserpass);
   if (!$dbconnect) {
      return 0;
   } elseif (!mysql_select_db($dbname)) {
      return 0;
   } else {
      return $dbconnect;
   } 
   
} 
 
?>
The third file is class.vendor.php as follows:

<?php

Code: Select all

class Vendor{
 
    var $firstlineaddress;
    var $businessname;
 
    function __construct(){ 
        $this->businessname=$businessname;
        $this->firstlineaddress=$firstlineaddress;
        }
        
    function addvendor($businessname){
    
    }
}
?>
and finally there is the file addex.php:

Code: Select all

<?php
 
include ("class.vendor.php");
include ("db_connect.php");
 
$businessname=$_POST["businessname"];
$firstlineaddress=$_POST["firstlineaddress"];
 
$newbusiness = new Vendor;
 
$sql="INSERT into vendors(businessname, firstlineaddress) values ($businessname,$firstlineaddress)";
    
    
    
?>
As you can see, there are a few notable missing lines. I just don't know how to join up the puzzle and I think I might be barking up the wrong tree. Your assistance would be greatly received.
Post Reply