How to execute INSERT, UPDATE, and DELETE statements

How to execute an INSERT statement

      $category_id = 1;
      $code = 'strat';
      $name = 'Fender Stratocaster';
      $price = 699.99;
      $query = "INSERT INTO products (categoryID, productCode, productName, listPrice) 
                VALUES (:category_id, :code, :name, :price)";
      $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(); 
      

How to execute an UPDATE statement

      $product_id = 4;
      $price = 599.99;
      $query = "UPDATE products 
                SET listPrice = :price 
                WHERE productID = :product_id";
      $statement = $db->prepare($query);
      $statement->bindValue(':price', $price);
      $statement->bindValue(':product_id', $product_id);
      $statement->execute(); $statement->closeCursor();
      

How to execute a DELETE statement

      $product_id = 4;
      $query = "DELETE FROM products 
                WHERE productID = :product_id";
      $statement = $db->prepare($query);
      $statement->bindValue(':product_id', $product_id);
      $statement->execute();
      $statement->closeCursor();
      

Back