simple object oriented add record to mysql
Posted: Fri Jul 18, 2008 10:12 am
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:
Secondly a file called db_connect.php
The third file is class.vendor.php as follows:
<?php
and finally there is the file addex.php:
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.
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>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;
}
}
?><?php
Code: Select all
class Vendor{
var $firstlineaddress;
var $businessname;
function __construct(){
$this->businessname=$businessname;
$this->firstlineaddress=$firstlineaddress;
}
function addvendor($businessname){
}
}
?>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)";
?>