fonction update avec foreach dans pdo
Bonjour à tous, je rencontre deux problèmes lorsque je souhaite mettre à jour une fiche article avec pdf joint et les catégories auxquelles se rattache l'article.
En mettant à jour la fiche, les informations concernants la fiche article sont prises en compte, les variables des catégories sont présentes dans le print_r de l'array posté mais ne s'intègrent pas en base de données (l'id_product est rentré mais pas l'id_cat.)
Mon foreach est-il correct en pdo ? Je n'arrive pas à trouver l'erreur.
Voici ma fonction :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| public function update($product)
{
$q = $this->db->prepare('UPDATE products SET name = :name, brand = :brand, description = :description, article = :article, SEO_Title = :SEO_Title ,SEO_Description = :SEO_Description ,SEO_Keywords = :SEO_Keywords WHERE id = :id');
$q->bindValue(':id', $product->id(), PDO::PARAM_INT);
$q->bindValue(':name', $product->name());
$q->bindValue(':brand', $product->brand());
$q->bindValue(':description', $product->description());
$q->bindValue(':article', $product->article());
$q->bindValue(':SEO_Title', $product->SEO_Title());
$q->bindValue(':SEO_Description', $product->SEO_Description());
$q->bindValue(':SEO_Keywords', $product->SEO_Keywords());
$q->execute();
foreach ($_POST['category'] as $key => $value)
{
$q = $this->db->prepare('UPDATE product_cat SET id_cat = :id_cat, id_product =:id_product');
$q->bindValue(':id_cat', $key);
$q->bindValue(':id_product',$product->id());
$q->execute();
}
} |
et voici l'envoi :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
if (isset($_POST['edit']) && isset($_POST['id']))
{
print_r($_POST);
$rand = rand();
$chemin_destination = '../article/';
$product = new Product(array('name' => $_POST['name'], 'description' => $_POST['description'], 'article' => $rand.'.pdf', 'SEO_Title' => $_POST['seo_title'], 'SEO_Description' => $_POST['seo_description'], 'SEO_Keywords' => $_POST['seo_keywords'], 'brand' => $_POST['brand'],'category' => $_POST['category'],'id'=>$_POST['id']));
move_uploaded_file($_FILES['article']['tmp_name'], $chemin_destination . $rand . '.pdf');
$manager->update($product);
} |
Deuxième question, peut-on faire un isset sur un fichier envoyé pour mettre à jour celui-ci uniquement s'il existe avant d'enchainer avec ceci :
Code:
1 2 3 4
|
$rand = rand();
$chemin_destination = '../article/';
move_uploaded_file($_FILES['article']['tmp_name'], $chemin_destination . $rand . '.pdf'); |
Merci pour votre aide !