您好我正在开发一个项目,其中从表单收集数据,此数据从表单发布到Web服务和数据库。 他们都运作正常,但我需要做一些检查。
我试图根据第一个函数失败的时间停止我的函数的处理流程。
第一个功能是将收集的数据发布到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);
?>
第二个函数将数据提交给数据库。 普通的我希望第二个函数在第一个函数失败时无法运行,但是错了,它转移到另一个函数,然后将不需要的数据提交给数据库。
所以我想知道如何根据第一个功能的失败来停止第二个功能。
答案 0 :(得分:1)
您应该尝试使用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
的例外情况