jcadima
5/16/2015 - 8:40 PM

Insert Product and Several Products

Insert Product and Several Products

<?php
// INSERT SINGLE PRODUCT
    $query = 'INSERT INTO categories ( categoryName  )
              VALUES  ( :category_name ) ';
    $statement = $db->prepare($query);
    $statement->bindValue(':category_name', $category_name );
    $statement->execute(); 
    $statement->closeCursor();
    
    
    
// INSERT SEVERAL PRODUCTS
// hardcoded values for illustration purposes only, this would come from a query string
$category_id = 1;
$code = 'strat';
$name = 'Fender Washer';
$price = 699.99;

$query = " INSERT INTO products
              (categoryID, productCode, productName, listPrice)
          VALUES
              (:category_id, ':code', ':name', :price) " ;
              // note category_id is not on quotes because their value is not enclosed on quotes $category_id = 1
              // the others like $code value are enclosed on quotes
$statement = $db->prepare($query);
$statement->bindValue(':category_id', $category_id) ;
$statement->bindValue(':code', $code);              
$statement->bindValue(':name', $name);
$statement->bindValue(':price', $price);
$statement->execute();
$statement->closeCursor();