Kamel Mili Kamel Mili - 1 month ago 10
MySQL Question

Simple insert in oop PHP

am trying to learn about

oop php
best practice and it's difficult just to debug , i know this is maybe a duplicated question and am hoping not to get a lot of negative votes so let's get to the problem :

I have a
Product
class in
Products.php


class Product {

public $id;
public $name;
public $price;
public $description;

function __construct($id, $name, $price, $description)
{
$this->$id = $id;
$this->$name = $name;
$this->$price = $price;
$this->$description = $description;

}
}


a
ProductManger
class in
ProductsManager.php


include('Products.php');
include('config.php');

class ProductManager {


function addProduct($conn,Products $p)
{

$querry="INSERT INTO `products`( `name`, `description`, `price`) VALUES ('".$name."','".$price."','".$description."')";
$c->exec($querry);

}
}


a
config
Class in
config.php


class config {

function connect() {

$serverName="localhost";
$dbName="phppoo";
$user="root";
$pass="";

return $conn = new PDO("mysql:host=$serverName;dbname=$dbName",$user,$pass);
}
}
//to be clear i think there are no issue related to connection


a Html page Where i submit the form

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">

</head>

<body>
<form action="AddProduct.php" method="POST">
<table border="3">
<tr>
<td>Reference<td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>Nom<td>
<td><input type="text" name="desc"></td>
</tr>

<tr>
<td>date de création <td>
<td><input type="number" name="price"></td>
</tr>


<tr>
<td> <td>
<td><input type="submit" name="ajouter" value="ajouter"></td>
</tr>


</form>
</table>


</body>
</html>


and finally the
AddProduct.php
Where i instantiate the product manager and send data so i can Execute the
querry


include('ProductsManager.php');

$co = new config();
$c=$co->connect();


if(isset($_POST['name']) and isset($_POST['desc'])and isset($_POST['price']) ) {
$p=new ProductManager($_POST['name'],$_POST['desc'],$_POST['price']);
$p->addProduct($c,$p);
}


now if i try to submit the
form
from the
html
page ,am redirected to a blank page
AddProduct.php
and it's normal cause am not returning a value.

the problem here is , am not getting the new values submitted inserted in my
Database
.

Thanks to any help , PS: if you think this isn't the best practice please mention a better method or a tuto. Thank you

Answer

To modify the property within a class you do this:

$this->price = $price;

Not this:

$this->$price = $price;

What the above does is try to the property based on the value of $price. So if price is 5.0 it will look for the property of $this->5.0.

Your updated class would look like so:

class Product {

    public $id;
    public $name;
    public $price;    
    public $description;

    function __construct($id, $name, $price, $description)
    {
        $this->id = $id; 
        $this->name = $name;
        $this->price = $price;
        $this->description = $description;

    }  
} 

Your add product method would look like this:

function addProduct($conn,Products $p) {

    $querry="INSERT INTO `products`( `name`, `description`, `price`) VALUES    ('".$p->name."','".$p->description."','".$p->price."')";
    $conn->exec($querry);

}

The properties on the $p variable were not being accessed and must be accessed like $p->yourProperty. Description and price were also in the incorrect locations.

There are additional issues related to SQL injection which are not being covered within this answer but at the very least this will help you to see what is wrong with the OOP itself.

The last error is that you want to call addProduct from the instance ($p) of the ProductManager class and pass an instance of the Product variable to the addProduct method:

$p = new ProductManager(); 
$product = new Product(-1, $POST['name'],$POST['desc'],$POST['price']);
$p->addProduct($c, $product);

I set -1 for the $id parameter since the row hasn't been inserted yet. After the insert you would likely want to get the last row insert id and assign it to the instance property, e.g., $this->id = $lastInsertId;

and, you need to fix your if statement:

if(isset($_POST['name']) and isset($_POST['desc'])and isset($_POST['price']) ) {

It is $_POST not $POST_... we were not seeing the result of the statement so we did not see any errors related to the OO code.

Comments