Executing Multiple Queries Using PDO

后端 未结 3 1552
既然无缘
既然无缘 2021-01-28 04:13

I am trying to execute the following query using a PDO Prepared Statement, but when I call $query->fetch(); it throws an exception SQLSTATE[HY000]: General

3条回答
  •  爱一瞬间的悲伤
    2021-01-28 04:58

    Run your first query which is the insert then after success on that one get the last insertid then use the id on your next query.. Eg.

    prepare("INSERT INTO orders (order_type`, `item`, `amount`, `price`, `price_btc`, `status`, `timestamp`, `placed_by`, `secret`, `first_name`, `last_name`, `address_1`, `address_2`, `city`, `zip_code`, `country`, `state`, `phone_number`) VALUES(:order_type, :item, :amount, :price, :price_btc, :status, :timestamp, :placed_by, :secret, :first_name, :last_name, :address_1, :address_2, :city, :zip_code, :country, :state, :phone_number)");
    
            $query->execute(array( /* your values*/ ));
    
    
            $lastId = $db->lastInsertId(); // fetch last insert id, after success.
    
    
            $order = $db->prepare("SELECT * FROM `orders` WHERE `ID`=?");
            $order->bindValue(1, $lastId);
            $order->execute();
            //Fetch your records and display.
    
    
    }
    catch (PDOException $e) {
            echo "Error : " . $e->getMessage();
    
    }
    
    ?>
    

    I left some part of the codes like you did, but the important thing is to run the insert first then collect the last

提交回复
热议问题