第一条语句执行,但是第二条语句不执行

时间:2020-04-14 15:55:59

标签: php pdo

我对PHP和MySQL相对较新(一年级计算机科学专业的学生),并且在锁定期间的空闲时间正在从事一个项目,以保持自己的忙碌。将新产品插入产品表后,如何解决插入复合键(wishlist_product)的问题?第一条语句执行,但是第二条语句没有执行,我没有收到错误

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();
}

0 个答案:

没有答案