0

I'm relatively new to PHP and MySQL (1st year comp sci student) and I'm working on a project in my free time during lockdown to keep myself busy. How do I go around inserting into the compound key (wishlist_product) after inserting a new product into the products table? The first statement executes, however the second statement doesn't, I don't get an error

try {
    //Begin the transaction
    $connect->beginTransaction();

    //Step 1:- Prepare the statement
    $stmt = $connect->prepare("INSERT INTO product(product.productURL, product.productName, product.productCost, product.productQuantity)
                               VALUES(:productURL, :productName, :productCost, :wishlistName);");

    //Step 2:- Bind the values with the variable
    $stmt->bindparam(":productURL", $productURL);
    $stmt->bindparam(":productName", $productName);
    $stmt->bindparam(":productCost", $productCost);
    $stmt->bindparam(":wishlistName", $listNameURL);

    //Step 3:- Execute the statement
    $stmt->execute();

    //Get the ID of the product inserted
    $insertID = $connect->lastInsertId();

    //Step 1:- Prepare the statement
    $stmt = $connect ->prepare("INSERT INTO wishlist_product(wishlistID, productID)
                                SELECT wishlistID, productID
                                FROM wishlist, product
                                WHERE wishlist.wishlistName=':wishlistName'
                                AND product.productID=':productID';");

    //Step 2:- Bind the values with a variable
    $stmt->bindParam(":wishlistName", $listNameURL);
    $stmt->bindParam(":productID", $insertID);

    //Step 3:- Execute the statement
    $stmt->execute();

    //Commit the changes
    $connect->commit();
}
catch(PDOException $e) {
    //Something went wrong, rollback DB
    $connect->rollback();
    throw $e;
    die();
}
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Steven
  • 11
  • 1
  • 3

0 Answers0