如果在其他表中存在,则在删除前检查公司

时间:2015-04-07 11:44:33

标签: php mysql database

当我要先删除记录时,会检查记录ID是否存在于其他表中?现在代码可以正常使用一个数组键,但是当我添加第二个数组键时,它不会检查另一个数组键。我的代码是:

$response = checkCompaniesExisting($auctionCompanyId, array("auctionCompanyInOrders" => "auctionCompanyInOrders"));

第一个工作正常,但是当我添加第二个数组键时,它根据我的要求不起作用

$response = checkCompaniesExisting($companyId, array("clientInOrders" => "clientInOrders", "brokerAndShipperInBookings" => "brokerAndShipperInBookings"));

我的功能是

function checkCompaniesExisting($recordId, $checkingCases) {
$responseArray = array();
foreach($checkingCases as $key => $value) {
    switch($key) {
        case 'clientInOrders':
            $checkClientInBookings = "SELECT clientId FROM ".DB_PREFIX."order WHERE clientId = '".$recordId."'";
            $checkClientInBookingsQuery = @mysql_query($checkClientInBookings);
            $checkClientInBookingsQueryArray = @mysql_fetch_assoc($checkClientInBookingsQuery);
            if($checkClientInBookingsQueryArray["clientId"] == $recordId) {
                $responseArray["response"] = "exist";
                $responseArray["responseText"] = "Client exists in orders";
            } else {
                $responseArray["response"] = "notExist";
            }
        break;

        case 'auctionCompanyInOrders':
            $checkAuctionCompanyInOrders = "SELECT auctionCompanyId FROM ".DB_PREFIX. "order WHERE auctionCompanyId = '".$recordId."'";
            $checkAuctionCompanyInOrdersQuery = @mysql_query($checkAuctionCompanyInOrders);
            $checkAuctionCompanyInOrdersQueryArray = @mysql_fetch_assoc($checkAuctionCompanyInOrdersQuery);
            if($checkAuctionCompanyInOrdersQueryArray["auctionCompanyId"] == $recordId) {
                $responseArray["response"] = "exist";
                $responseArray["responseText"] = "Auction company exists in orders";
            } else {
                $responseArray["response"] = "notExist";
            }
        break;

        case 'brokerAndShipperInBookings':
            $checkBrokerAndShipperInbookings = "SELECT bookingBrokerId, bookingShipperId FROM ".DB_PREFIX."bookings WHERE bookingBrokerId = '".$recordId."' OR bookingShipperId = '".$recordId."'";
            $checkBrokerAndShipperInbookingsQuery = @mysql_query($checkBrokerAndShipperInbookings);
            $checkBrokerAndShipperInbookingsQueryArray = @mysql_fetch_assoc($checkBrokerAndShipperInbookingsQuery);
            if($checkBrokerAndShipperInbookingsQueryArray["bookingBrokerId"] == $recordId || $checkBrokerAndShipperInbookingsQueryArray["bookingShipperId"] == $recordId) {
                $responseArray["response"] = "exist";
                $responseArray["responseText"] = "Broker OR Shipper exists in bookings";
            } else {
                $responseArray["response"] = "notExist";
            }
        break;
    }
}
return $responseArray;
}

现在第一个数组键clientInOrdersswitch case中无法正常工作,但第二个数组键在切换案例中工作brokerAndShipperInBookings。这段代码有什么问题。

1 个答案:

答案 0 :(得分:0)

问题是,您始终会覆盖结果array键。

在foreach循环中,将$key添加到结果数组以创建多维关联数组。这只是循环中的一个,也是为其他人做的。

           if ($checkAuctionCompanyInOrdersQueryArray["auctionCompanyId"] == $recordId) {
                $responseArray[$key]["response"] = "exist";
                // Added $key ^^^^^^
                $responseArray[$key]["responseText"] = "Auction company  exists in orders";
                // Added $key ^^^^^^
            } else {
                $responseArray[$key]["response"] = "notExist";
                // Added $key ^^^^^^
            }

我在你的数组中添加了$key。试试这个。 在此之后,尝试var_dump($responseArray);进行调试。