如果第一个 PHP 函数抛出异常,如何阻止第二个 PHP 函数的实现


how can i stop a second php function from implementing if a first php function throws an exception

你好,我正在做一个项目,通过从表单收集数据,这些数据从表单发布到网络服务和数据库。它们都可以正常工作,但我需要进行一些检查。

我尝试根据第一个函数何时失败来停止函数的流程。

第一个函数是将收集的数据发布到 Web 服务的函数。 下面 :

<?php
require_once('includes/nusoap.php');
$wsdlfile = "https://niid.autoreglive.org/nia_api/service.asmx?wsdl";
$wsdlfile = "https://www.niid.org/NIA_API/Service.asmx?wsdl";
//$wsdlfile = "http://localhost:82/cscart/service/index.php";
 $msg = "<?xml version='"1.0'" encoding='"utf-8'" ?>
<soap:Envelope xmlns:xsi='"http://www.w3.org/2001/XMLSchema-instance'" xmlns:xsd='"http://www.w3.org/2001/XMLSchema'" xmlns:soap='"http://schemas.xmlsoap.org/soap/envelope/'">
<Username>name</Username>
<Password>pass</Password>
<NiaNaicomID>nnn</NiaNaicomID>
<PolicyNo>TP/1/1</PolicyNo>
<InsuredName>Sunky Yaki</InsuredName>
<ContactAddress>Yaba Lagos</ContactAddress>
<GSMNo>08021231234</GSMNo>
<Email>a@b.c</Email>
<EffectiveCoverDate>2013-09-20</EffectiveCoverDate>
<ExpirationDate>2014-09-19</ExpirationDate>
<TypeOfCover>Comprehensive</TypeOfCover
><VehicleCategory>Saloon</VehicleCategory>
<EngineNo>uhdu</EngineNo>
<ChasisNo>dksdj</ChasisNo>
<VehicleColor>green</VehicleColor>
<YearofMake>1999</YearofMake>
<VehicleMake>Toyota</VehicleMake>
<RegistrationNo>gg11jj</RegistrationNo>
<OldRegistrationNo>11122</OldRegistrationNo>
<VehicleType>Saloon</VehicleType>
<EngineCapacity>4</EngineCapacity>
<VehicleModel>Camry</VehicleModel>
<SumAssured>233300</SumAssured>
<Premium>230</Premium>
<CoverNoteNo>1211</CoverNoteNo>
<CertificateNo>test 2</CertificateNo>
<GeographicalZone>North East</GeographicalZone>
</soap:Envelope>
 ";
 $Username = '******';
$Password = '******'; 
$NiaNaicomID = '******'; 
$PolicyNo = $_POST['Policy_Number']; 
$InsuredName = $_POST['Insured_Name'];
$ContactAddress = $_POST['Residential_Address']; 
$GSMNo = $_POST['phone']; 
$Email = $_POST['Email'];
$EffectiveCoverDate = $_POST['Date'];
$ExpirationDate = $_POST['Date_Expiry'];
$TypeOfCover = $_POST['Type_Of_Insurance']; 
$VehicleCategory = 'null'; 
$EngineNo = $_POST['Engine_Number']; 
$ChasisNo = $_POST['Chassis_Number'];
$VehicleColor = $_POST['Colour']; 
$YearofMake = '2004'; 
$VehicleMake = $_POST['Make_Of_Car']; 
$RegistrationNo = $_POST['Registeration_Number']; 
$VehicleType = $_POST['Vehicle_Class']; 
$EngineCapacity = ''; 
$VehicleModel = 'Model'; 
$SumAssured = 2.3; 
$Premium = '2.3'; 
$CoverNoteNo = 'No note';
$CertificateNo = 'No certificate No';
$GeographicalZone = '6';
$params = array('Username' => $Username, 
                                                           'Password' => $Password, 
                                                           'NiaNaicomID' => $NiaNaicomID,
                                                           'PolicyNo' => $PolicyNo,
                                                           'InsuredName' => $InsuredName,
                                                           'ContactAddress' => $ContactAddress, 
                                                           'GSMNo' => $GSMNo, 
                                                           'Email' => $Email,
                                                           'EffectiveCoverDate' => $EffectiveCoverDate,
                                                           'ExpirationDate' => $ExpirationDate,
                                                           'TypeOfCover' => $TypeOfCover,
                                                           'VehicleCategory' => $VehicleCategory, 
                                                           'EngineNo' => $EngineNo, 
                                                           'ChasisNo' => $ChasisNo,
                                                           'VehicleColor' => $VehicleColor,
                                                           'YearofMake' => $YearofMake,
                                                           'VehicleMake' => $VehicleMake, 
                                                           'RegistrationNo' => $RegistrationNo, 
                                                           'VehicleType' => $VehicleType,
                                                           'EngineCapacity' => $EngineCapacity,
                                                           'VehicleModel' => $VehicleModel,
                                                           'SumAssured' => $SumAssured,
                                                           'Premium' => $Premium,
                                                           'CoverNoteNo' => $CoverNoteNo,
                                                           'CertificateNo' => $CertificateNo,
                                                           'GeographicalZone' => $GeographicalZone
                                                    );
$s = new nusoap_client($wsdlfile, 'wsdl');
//$s = new nusoap_client($wsdlfile);
// if (empty($proxyhost))
// {
// }else
// {
//         $s->setHTTPProxy($proxyhost,$proxyport,$proxyusr,$proxypassword);
// }

$result = $s->call('Vehicle_Policy_Push', $params, '', '', false, true);
if($result){
print_r($result);
echo '<META HTTP-EQUIV="Refresh" Content="3; URL=account.php">';

}else{
    echo '<META HTTP-EQUIV="Refresh" Content="3; URL=policy_active.php">';
}
?>

这是我在下面第一个函数失败时想要停止的第二个函数:

<?php
$con=mysqli_connect
("localhost","*******","*******","*******");
//Checkconnection
if(mysqli_connect_errno())
{
echo"FailedtoconnecttoMySQL:".mysqli_connect_error();
}
$check="SELECT * FROM transactions WHERE year_find = '$_POST[year_find]'";
$rs = mysqli_query($con,$check);
$data = mysqli_fetch_array($rs, MYSQLI_NUM);
if($data[0] > 1) {
    echo '<META HTTP-EQUIV="Refresh" Content="3; URL=policy_active.php">';
}
else
{
$sql="INSERT INTO transactions(month_search,month,year_search,year,year_find,Username,Insured_Name,combined,Residential_Address,Telephone,Email,Make_Of_Car,Model,Engine_Number,Year_Of_Manufacture,Chassis_Number,Vehicle_Class,Colour,Registeration_Number,Product_Type,Premium,Policy_Number,Start_Date,Expiry_Date,Date_Begin,Type_Of_Insurance,Status,  Transaction_id)VALUES('$_POST[month_search]','$_POST[month]','$_POST[year_year]','$_POST[newyear]','$_POST[year_find]','$_POST[Username]','$_POST[Insured_Name]','$_POST[combined]','$_POST[Residential_Address]','$_POST[phone]','$_POST[Email]','$_POST[Make_Of_Car]','$_POST[Model]','$_POST[Engine_Number]','$_POST[Year_Of_Manufacture]','$_POST[Chassis_Number]','$_POST[Vehicle_Class]','$_POST[Colour]','$_POST[Registeration_Number]','$_POST[Product_Type]','$_POST[Premium]','$_POST[Policy_Number]','$_POST[Date]','$_POST[Date_Expiry]','$_POST[Date_Begin]','$_POST[Type_Of_Insurance]','$_POST[Status]','$_POST[Transaction_id]')";
}
if(!mysqli_query($con,$sql))
{
die('Error:'.mysqli_error
($con));
}
echo '<META HTTP-EQUIV="Refresh" Content="3; URL=account.php">';
mysqli_close($con);
?>

第二个函数将数据提交到数据库。普通我希望当第一个函数失败时第二个函数无法运行,但 im 错了,它转移到另一个函数,然后将不需要的数据提交到数据库。

所以我想知道我如何根据第一个函数的失败来停止第二个函数。

您应该尝试使用 try...catch 块。仅当第一个函数未引发异常时,才会调用第二个函数。

function postData(){
    // code to post data here
    // throw exception when something fails
    throw new Exception('I am an exception message');
}
function saveData(){
    // code to save data here
}
try{
    postData();
    saveData();
}
catch( Exception $e ){
    echo $e->getMessage();
}

在 php.net 阅读有关例外的更多信息