Please what's wrong with my codes

  You are currently not logged in. You can view the forums, but cannot post messages. Log In | Register

03-Apr-12 13:56
Below is the code for my database connection class:



class DatabaseManager
{

//variable to hold connection object.
protected static $connection;

//constructor - called on object creation
private function __construct() {

try {
// assign new PDO object to db variable
self::$connection = new PDO( 'mysql:host=localhost; dbname=mydb', 'username', 'pass' );
//sets encoding to UTF-8
#self::$connection->exec("SET CHARACTER SET utf8");
//Enable error mode - use try/catch to catch error
self::$connection->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}
catch (PDOException $e) {
//Output error - would normally log this to error file rather than output to user.
echo "Connection Error: " . $e->getMessage();
}

}

// get connection function. Static method - accessible without instantiation
public static function getConnection() {

//Guarantees single instance, if no connection object exists then create one.
if (!self::$connection) {
//new connection object.
new DatabaseManager();
}

//return connection.
return self::$connection;
}




Here is the code for the method I'm calling from AssetManager Class:



public function insertAsset()
{
// Does the Asset object already have an ID?
if ( !is_null( $this->id ) ) trigger_error ( "AssetManager::insertAsset(): Attempt to insert an Asset object that already has its ID property set (to $this->id).", E_USER_ERROR );

// Insert the Asset
$conn = DatabaseManager::getConnection();
$sql = "INSERT INTO table_asset ( productName, productSerialNumber, purchaserOrSupplier, supplierPhoneNo, status, purchaseDate,
departmentOfUsage, quantityPurchased, costPerUnit, remark) VALUES ( :productName, :productSerialNumber,
:purchaserOrSupplier, :supplierPhoneNo, :status, FROM_UNIXTIME(:purchaseDate),
:departmentOfUsage, :quantityPurchased, :costPerUnit, :remark )";
$st = $conn->prepare ( $sql );
$st->bindValue( ":productName", self::productName, PDO::PARAM_STR );
$st->bindValue( ":productSerialNumber", self::productSerialNumber, PDO::PARAM_STR );
$st->bindValue( ":purchaserOrSupplier", self::purchaserOrSupplier, PDO::PARAM_STR );
$st->bindValue( ":supplierPhoneNo", self::supplierPhoneNo, PDO::PARAM_INT );
$st->bindValue( ":status", self::status, PDO::PARAM_STR );
$st->bindValue( ":purchaseDate", self::purchaseDate, PDO::PARAM_INT );
$st->bindValue( ":departmentOfUsage", self::departmentOfUsage, PDO::PARAM_STR );
$st->bindValue( ":quantityPurchased", self::quantityPurchased, PDO::PARAM_INT );
$st->bindValue( ":costPerUnit", self::costPerUnit, PDO::PARAM_INT );
$st->bindValue( ":remark", self::remark, PDO::PARAM_STR );
$st->execute();
$this->id = $conn->lastInsertId();
$conn = null;
}



It keep giving me this error:




( ! ) Fatal error: Using $this when not in object context in C:\wamp\www\ec\classes\AssetManager.php on line 197
Call Stack
# Time Memory Function Location
1 0.0013 371032 {main}( ) ..\assetProcessor.php:0
2 0.0191 470720 AssetManager::insertAsset( ) ..\assetProcessor.php:11


Please how do I make this thing work? I use ellipse and it runs the classes involved very well.
05-Apr-12 06:14
@papadammy: What's on line 197? And how are you calling AssetManager::insertAsset()?

--
Matt Doyle, Elated
3rd Edition of my jQuery Mobile book out now! Learn to build mobile web apps. Free sample chapter: http://store.elated.com/
07-Apr-12 06:16
Matt, thanks a lot. I discovered that in the script that processes the input form for submission, I didn't create a new object before attempting to call insertAsset(), I did that last night and everything worked fine. Thanks a lot.

 
New posts
Old posts

Follow Elated