首页 > 解决方案 > 第一条语句执行,但第二条语句不执行

问题描述

我对 PHP 和 MySQL 比较陌生(一年级 comp sci 学生),我正在锁定期间的空闲时间从事一个项目,以保持自己忙碌。将新产品插入产品表后,如何插入复合键 (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();
}

标签: phppdo

解决方案


推荐阅读