Hello! I’m new in php, I studied by my own. I manage to run a simple add, edit,delete. I just downloaded this code and I modified it to have my desired output. I just want to see if I did it right. There is no problem in running, I just want to know if I’m doing right to avoid future errors. I’m new in php oop and pdo.
here is my code
<?php
class Guitar{
private $id;
private $make;
private $model;
private $colour;
private $price;
private static $db;
public function __construct($id=null, $make, $model, $colour, $price){
$this->id = $id;
$this->make = $make;
$this->model = $model;
$this->colour = $colour;
$this->price = $price;
}
public function id()
{
return $this->id;
}
public function viewItem($id){
$this ->id = $id;
$fields = array('id', 'make', 'model', 'colour', 'price');
self::conn();
try {
$sql = "SELECT ".implode(',',$fields)." FROM dbo.guitar WHERE id=:id";
$q = self::$db->prepare($sql);
$q->execute(array(':id' => $this->id));
$row = $q->rowCount();
if ($row == 0)
{
echo 'no records found.';
}
else
{
$results = $q->fetchAll(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE,
"Guitar",$fields);
}
}catch (Exception $e){
print "Error!: " . $e->getMessage();
}
return $results;
}
public function getMake(){
return $this->make;
}
public function getModel(){
return $this->model;
}
public function getColour(){
return $this->colour;
}
public function getPrice(){
return $this->price;
}
public function setMake( $make ) {
$this->make = $make;
}
public function setModel( $model ){
$this->model = $model;
}
public function setColour( $colour ){
$this->colour = $colour;
}
public function setPrice( $price ){
$this->price = $price;
}
public function save()
{
self::conn();
try {
$data = array('make' => $this->make,
'model' => $this->model,
'colour' => $this->colour,
'price' => $this->price);
if($this->id){
$sql = "UPDATE guitar SET make=:make, model=:model, colour=:colour, price=:price WHERE id=:id";
$data['id'] = $this->id;
}else{
$sql = "INSERT INTO guitar (make,model,colour,price) VALUES (:make,:model,:colour,:price)";
}
$q = self::$db->prepare($sql);
$q->execute($data);
echo ''.$q->rowCount().'affected';
} catch (Exception $e) {
print "Error!: " . $e->getMessage();
}
}
public static function getAll(){
self::conn();
try{
$sql = "SELECT * FROM dbo.guitar WHERE del_stat IS NULL";
$q = self::$db->prepare($sql);
$q->execute();
$row = $q->rowCount();
if ($row == 0){
echo 'no records found.';
}else {
$results = $q->fetchAll(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE,
"Guitar",
array('id', 'make', 'model', 'colour', 'price'));
}
} catch (Exception $e){
print "Error!: " . $e->getMessage();
}
return $results;
}
public function delete(){
self::conn();
try{
$sql = "UPDATE dbo.guitar SET del_stat ='1' WHERE id=:id";
$q = self::$db->prepare($sql);
$q->bindValue(':id', $this->id, PDO::PARAM_INT);
$q->execute();
echo $q->rowCount().'deleted';
} catch (Exception $e){
print "Error!: " . $e->getMessage();
}
}
public static function conn(){
if (!self::$db instanceof PDO){
$dsn = "sqlsrv:myserver; Database=test";
try {
self::$db = new PDO($dsn);
self::$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
print "Error!: " . $e->getMessage();
}
}
}
}
?>
I really need your advice. THANK YOU!