dongtanzhu5417 2014-12-15 09:02 采纳率: 100%
浏览 47
已采纳

如果第一个php函数抛出异常,我怎么能停止实现第二个PHP函数

hello i am working on a project where by, data is collected from a form, this data is posted from the form to a webservice and a database. they both work properly, but i need to put some checks in place.

i am try to halt the process flow of my functions based on when ever the first function fails.

the first function is the one that posts the data collected to a webservice. below :

<?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">';

}
?>

and this is the second function that i want to be halted whenever my first function fails below :

<?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);
?>

the second function submits the data to a database. ordinary i was hoping the second function would fail to run when the first function failed, but imwas wrong, it moved on to the other function, which then submitted the unwanted data to the database.

so am wondering how i can halt the second function based on the failure of the first.

  • 写回答

1条回答 默认 最新

  • dongxiequ3724 2014-12-15 09:12
    关注

    You should try using a try...catch block. Second function will be called only when first doesn't throw an exception.

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

    Read more about exceptions at php.net

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站