-5

I'm using oop to insert some data that will be fill from a form into my data base.

I'm able to connet the php to the database using pdo but I'm not having the data going into the database, can you please give me an example.

This is my code:

<?php

class goods
{  
    public $name_goods;
    public $price;
}

try {
    $db_conn = new PDO('mysql:host=localhost;dbname=database','root','');
} catch (PDOException $e) {
    echo "Could not connect to database";
}

$name_goods = $_POST['name_goods'];
$price = $_POST['price'];
$sql = 'INSERT INTO goods(name_goods, price,)VALUES ($name_goods, $price)'; 

?>
Kevin
  • 41,694
  • 12
  • 53
  • 70

3 Answers3

3

Your $sql query is never executed.

edit : And you have syntax errors. Comma after "price" and missing quotes for values.

Kevin Labécot
  • 2,005
  • 13
  • 25
1

Use prepared statements!

$sql = 'INSERT INTO goods(name_goods, price) VALUES (:name_goods, :price)';  
$q = $conn->prepare($sql);
$q->execute(array(':name_goods'=>$name_goods,
                  ':price'=>$price));
Cheesi
  • 483
  • 4
  • 14
0

This does not look OOP at all. The above responses are right, your SQL isn't executing (examples here: http://php.net/manual/en/function.mysql-query.php)

But if you're looking for a quick start with PHP OOP, I'd advise looking at some PHP frameworks.

Noy
  • 1,258
  • 2
  • 11
  • 28