Search code examples
phpmysqlsqlsymfonysilex

PHP/MySQL foreign keys saves as id=0 to database


I'm new to php and mysql but there is a project I have to do on my classes with Silex 2.0 and Symfony and I have problems with my php code and foreign keys in my products table in database.

The problem is with proper way of save functions in my program.

Here's what happens in my data base when I try to add or edit new product - I get idCategory and idProducent = 0. :/

And there is part of my code in ProductModel.php responsible for saving it to database(to table products):

public function addProduct($data)
{
    $sql = 'INSERT INTO 
                `products` ( `id`, `idCategory`, `idProducent`, `name`,  `price_netto`, `price_brutto`, `desc` ) 
            VALUES 
                (NULL,?,?,?,?,?,?)';
    $data['price_netto'] = $data['price_brutto'] * 1.22;
    $this->_db->executeQuery(
        $sql, array(
        $data['idCategory'], $data['idProducent'], $data['name'], 
        $data['price_netto'], $data['price_brutto'], $data['desc'])
    );
}
public function saveProduct($data)
{
    if (isset($data['id']) && ctype_digit((string)$data['id'])) {
        $sql = "UPDATE 
                    products 
                SET 
                    idCategory = ?, idProducent = ?, name = ?, 
                    price_netto = ?, price_brutto = ?, `desc` = ? 
                WHERE 
                    id = ?";
        $data['price_netto'] = $data['price_brutto'] * 1.22;
        $this->_db->executeQuery(
            $sql, array(
            $data['idCategory'], $data['idProducent'], $data['name'], 
            $data['price_netto'], $data['price_brutto'], $data['desc'], $data['id'])
        );
    } else {
        $sql = "INSERT INTO 
                    `products` ( `id`, `idCategory`, `idProducent`, `name`, `price_netto`, `price_brutto`, `desc` ) 
                VALUES 
                    (NULL,?,?,?,?,?,?)";
        $data['price_netto'] = $data['price_brutto'] * 1.22;
        $this->_db->executeQuery(
            $sql, array($data['id']), array(
            $data['idCategory'], $data['idProducent'], $data['name'], 
            $data['price_netto'], $data['price_brutto'], $data['desc'])
        );        
    }
}

Here you have ProductController.php [the part I think is important here]: https://codeshare.io/5w4rRD

Also functions from ProducentsModel.php and CategoriesModel.php:

public function getProducents()
{
    $sql = 'SELECT * FROM producents;';
    return $this->_db->fetchAll($sql);
}
public function getCategories()
{
    $sql = 'SELECT * FROM categories;';
    return $this->_db->fetchAll($sql);
}

Sorry for giving you only links but this is my first time with Stack Overflow, hope you'll understand.


Solution

  • Database was fine. The bug was in ProductController.php which you can find in link up there. This is how it suppose to look like in both edit and add options:

    for ( $i=0; $test[$i] != NULL; $i++) { $choiceCategory[$test[$i]['name']] = $test[$i]['id'];; }

        $producentModel = new ProducentsModel($app);
        $test = $producentModel->getProducents();
    
        $choiceProducent = array();
    
        for ( $i=0; $test[$i] != NULL; $i++) {
            $choiceProducent[$test[$i]['name']] = $test[$i]['id'];
        }
    

    And delete this: choice_label until placeholder=>'Choose'.

    Sorry for bothering you guys and thank you for quick answer.