2013-08-21 75 views
-3

我並不確定問題是什麼,但我知道的是,當我將session_start()從我的代碼中取出時,它將打破整個事件,並且不會發送電子郵件並且數據不會被插入到數據庫中。但在session_start()存在的情況下,用戶遇到問題,即發送電子郵件但數據未插入到數據庫中。take session_start()out break everything

這裏是我的代碼整片:

<?php 

$error = ""; 
$to = ""; 
$subject = ""; 
$ForumName = ""; 
$DriverNumber = ""; 
$Division = ""; 
$TruckDriving = ""; 
$Cargo = ""; 
$DepotLeaving = ""; 
$CityLeaving = ""; 
$CountyStart = ""; 
$TimeStart = ""; 
$MileageStart = ""; 
$DepotArriving = ""; 
$CityArriving = ""; 
$CountyEnd = ""; 
$TimeEnd = ""; 
$MileageEnd = ""; 
$CargoDamage = ""; 
$TruckDamage = ""; 
$FuelCost = ""; 
$FuelAmount = ""; 
$TravelExpenses = ""; 
$FinesCost = ""; 
$OnTimeOrLate = ""; 
$LateFee = ""; 
$TotalProfit = ""; 

//GET THE VARIABLES FROM THE FORM ON THE PREVIOUSE PAGE 

if (isset($_POST['forum-name']) && $_POST['forum-name'] !== "" && $_POST['forum-name'] !== "Registered Forum Name") { 
    $ForumName = $_POST['forum-name']; 
} else { 
    $error .= "ERROR: You have not entered a registered forum name in your log.Please try again|"; 
} 

if (isset($_POST['driver-number']) && $_POST['driver-number'] !== "" && $_POST['driver-number'] !== "Driver Number") { 
    $DriverNumber = $_POST['driver-number']; 
} else { 
    $error .= "ERROR: You have not entered your driver number in your log.Please try again|"; 
} 

if (isset($_POST['email-address']) && $_POST['email-address'] !== "" && $_POST['email-address'] !== "Email Address") { 
    $EmailAddress = $_POST['email-address']; 
} else { 
    $error .= "ERROR: You have not entered your email address in your log.Please try again|"; 
} 

//var_dump($ForumName); 
//var_dump($DriverNumber); 

if (isset($_POST['dropdown1'])) { 
    $Division = $_POST['dropdown1']; 
} 
else{ 
    $error .= "ERROR: You have not entered your division in your log.Please try again|"; 
} 

//var_dump($Division); 


if (isset($_POST['dropdown6'])) { 
    $TruckDriving = $_POST['dropdown6']; 
} 
else{ 
    $error .= "ERROR: You have not entered the truck that you drove in your log.Please try again|"; 
} 

if (isset($_POST['cargo']) && $_POST['cargo'] !== "" && $_POST['cargo'] !== "Cargo") { 
    $Cargo = $_POST['cargo']; 
} else { 
    $error .= "ERROR: You have not entered a cargo that you hauled in your log.Please try again|"; 
} 

if (isset($_POST['dropdown5'])) { 
    $DepotLeaving = $_POST['dropdown5']; 
} 
else{ 
    $error .= "ERROR: You have not entered a depot that you left in your log.Please try again|"; 
} 

if (isset($_POST['city-leaving']) && $_POST['city-leaving'] !== "" && $_POST['city-leaving'] !== "City Leaving") { 
    $CityLeaving = $_POST['city-leaving']; 
} else { 
    $error .= "ERROR: You have not entered a city that you left in your log.Please try again|"; 
} 

if (isset($_POST['country-start']) && $_POST['country-start'] !== "" && $_POST['country-start'] !== "Country Starting In") { 
    $CountryStart = $_POST['country-start']; 
} else { 
    $error .= "ERROR: You have not entered a country that you left from in your log.Please try again|"; 
} 

if (isset($_POST['dropdown2'])) { 
    $DayStart = $_POST['dropdown2']; 
} 
else{ 
    $error .= "ERROR: You have not entered a day that you started on in your log.Please try again|"; 
} 

if (isset($_POST['time-start']) && $_POST['time-start'] !== "") { 
    $TimeStart = $_POST['time-start']; 
} else { 
    $error .= "ERROR: You have not entered a time that you started on in your log.Please try again|"; 
} 

if (isset($_POST['mileage-start']) && $_POST['mileage-start'] !== "" && $_POST['mileage-start'] !== "Mileage Start") { 
    $MileageStart = $_POST['mileage-start']; 
} else { 
    $error .= "ERROR: You have not entered a mileage that you started on in your log.Please try again|"; 
} 

if (isset($_POST['dropdown7'])) { 
    $DepotArriving = $_POST['dropdown7']; 
} 
else{ 
    $error .= "ERROR: You have not entered a depot that you arrived at in your log.Please try again|"; 
} 

if (isset($_POST['city-arriving']) && $_POST['city-arriving'] !== "" && $_POST['city-arriving'] !== "City Arriving") { 
    $CityArriving = $_POST['city-arriving']; 
} else { 
    $error .= "ERROR: You have not entered a city that you arrived at in your log.Please try again|"; 
} 

if (isset($_POST['country-end']) && $_POST['country-end'] !== "" && $_POST['country-end'] !== "Country Finishing In") { 
    $CountryEnd = $_POST['country-end']; 
} else { 
    $error .= "ERROR: You have not entered a country that you finished at in your log.Please try again|"; 
} 

if (isset($_POST['dropdown3'])) { 
    $DayEnd = $_POST['dropdown3']; 
} 
else{ 
    $error .= "ERROR: You have not entered a day that you finished on in your log.Please try again|"; 
} 

if (isset($_POST['time-end']) && $_POST['time-end'] !== "") { 
    $TimeEnd = $_POST['time-end']; 
} else { 
    $error .= "ERROR: You have not entered a time that you finished on in your log.Please try again|"; 
} 

if (isset($_POST['mileage-end']) && $_POST['mileage-end'] !== "" && $_POST['mileage-end'] !== "Mileage End") { 
    $MileageEnd = $_POST['mileage-end']; 
} else { 
    $error .= "ERROR: You have not entered a mileage that you finished on in your log.Please try again|"; 
} 

if (isset($_POST['cargo-damage']) && $_POST['cargo-damage'] !== "" && $_POST['cargo-damage'] !== "£") { 
    $CargoDamage = $_POST['cargo-damage']; 
} else { 
    $error .= "ERROR: You have not entered a damage cost for your cargo in your log.Please try again|"; 
} 

if (isset($_POST['truck-damage']) && $_POST['truck-damage'] !== "" && $_POST['truck-damage'] !== "£") { 
    $TruckDamage = $_POST['truck-damage']; 
} else { 
    $error .= "ERROR: You have not entered a damage cost for your truck in your log.Please try again|"; 
} 

if (isset($_POST['fuel-cost']) && $_POST['fuel-cost'] !== "" && $_POST['fuel-cost'] !== "£") { 
    $FuelCost = $_POST['fuel-cost']; 
} else { 
    $error .= "ERROR: You have not entered a fuel cost for your load in your log.Please try again|"; 
} 

if (isset($_POST['fuel-amount']) && $_POST['fuel-amount'] !== "" && $_POST['fuel-amount'] !== "Fuel Amount (Litres)") { 
    $FuelAmount = $_POST['fuel-amount']; 
} else { 
    $error .= "ERROR: You have not entered a fuel amount for your load in your log.Please try again|"; 
} 

if (isset($_POST['travel-expenses']) && $_POST['travel-expenses'] !== "" && $_POST['travel-expenses'] !== "£") { 
    $TravelExpenses = $_POST['travel-expenses']; 
} else { 
    $error .= "ERROR: You have not entered a travel expense for your load in your log.Please try again|"; 
} 

if (isset($_POST['fines-cost']) && $_POST['fines-cost'] !== "" && $_POST['fines-cost'] !== "£") { 
    $FinesCost = $_POST['fines-cost']; 
} else { 
    $error .= "ERROR: You have not entered a fines cost for your load in your log.Please try again|"; 
} 

if (isset($_POST['dropdown4'])) { 
    $OnTimeOrLate = $_POST['dropdown4']; 
} 
else{ 
    $error .= "ERROR: You have not entered whether you were on time or late in your log.Please try again|"; 
} 


if (isset($_POST['late-fee']) && $_POST['late-fee'] !== "" && $_POST['late-fee'] !== "£") { 
    $LateFee = $_POST['late-fee']; 
} else { 
    $error .= "ERROR: You have not entered a late fee for your load in your log.Please try again|"; 
} 

if (isset($_POST['total-profit']) && $_POST['total-profit'] !== "" && $_POST['total-profit'] !== "£") { 
    $TotalProfit = $_POST['total-profit']; 
} else { 
    $error .= "ERROR: You have not entered a total profit for your load in your log.Please try again|"; 
} 

$body = 
"Name on forum: $ForumName 
\nDriver Number: $DriverNumber 
\nEmail Address: $EmailAddress 
\nDivision: $Division 
\nTruck Driving: $TruckDriving 
\nCargo: $Cargo 
\nDepot Leaving: $DepotLeaving 
\nCity Leaving: $CityLeaving 
\nCountry Start: $CountryStart 
\nDay Start: $DayStart 
\nTime Start: $TimeStart 
\nMileage Start: $MileageStart 
\nDepot Arriving: $DepotArriving 
\nCity Arriving: $CityArriving 
\nCountry End: $CountryEnd 
\nDay End: $DayEnd 
\nTime End: $TimeEnd 
\nMileage End: $MileageEnd 
\nCargo Damage: $CargoDamage 
\nTruck Damage: $TruckDamage 
\nFuel Cost: $FuelCost 
\nFuel Amount: $FuelAmount 
\nTravel Expenses: $TravelExpenses 
\nFines Cost: $FinesCost 
\nOn Time/Late: $OnTimeOrLate 
\nLate Fee: $LateFee 
\nTotal Profit: $TotalProfit"; 

if ($Division == "International") { 
    $to = "[email protected]"; 
    $subject = "$ForumName Log for International"; 
} elseif ($Division == "Samcro") { 
    $to = "[email protected]"; 
    $subject = "$ForumName Log for Samcro"; 
} elseif ($Division == "Port"){ 
    $to = "[email protected]"; 
    $subject = "$ForumName Log for Port'"; 
} elseif ($Division == "Bio"){ 
    $to = "[email protected]"; 
    $subject = "$ForumName Log for Bio-Fuels'"; 
} elseif ($Division == "WnD"){ 
    $to = "[email protected]"; 
    $subject = "$ForumName Log for WnD'"; 
} 

//WHO THE EMAIL IS FROM MUST BE A RANDOM OTHER WISE ERRORS HAPPEN 
$headers = "From: \"JWT Group\" [email protected]\r\n" . "X-Mailer: php"; 

//THE mail() SENDS THE EMAIL WITH THE VARIABLES THAT YOU JUST SET ABOVE 
if ($Division !== " ") { 
    mail($to, $subject, $body, $headers); 
} 
//var_dump($to); 
//var_dump($subject); 
//var_dump($body); 
//var_dump($error); 

//A MESSAGE TO THE PERSON WHO SENT THE ENQUIRY SENT TO THEIR EMAIL TO SAY THEIR MESSAGE WAS SENT 
mail(
     $EmailAddress, "Your log for JWT $Division was sent", "Hi $ForumName 
\n Your log for JWT $Division has been received.", "From: \"JWT Group\" [email protected]\r\n" . "X-Mailer: php"); 

///PHP TO INSERT DRIVER'S BANK DETAILS INTO BANK DATABASE 

    $host=""; // Host name 
    $username=""; // Mysql username 
    $password=""; // Mysql password 
    $db_name=""; // Database name 
    $tbl_name=""; // Table name 

    $un = ""; 
    $usrname = ""; 
    $usrpass = ""; 
    $userID = ""; 

    mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
    mysql_select_db("$db_name")or die("cannot select DB"); 

    if(isset ($_SESSION['usrName'])) 
    { 
     $usrname = $_SESSION['usrName']; 
    } 
    else 
    { 
     echo "4"; 
    } 

    //var_dump ($usrname); 

    if(isset ($_SESSION['usrPass'])) 
    { 
     $usrpass = $_SESSION['usrPass']; 
    } 
    else 
    { 
     echo "5"; 
    } 

    $sql="SELECT * FROM jwtdrivers WHERE username='$usrname' and password='$usrpass'"; 
    $result=mysql_query($sql); 
    $rows=mysql_fetch_array($result); 

    $userID = $rows['id']; 

    //var_dump ($userID); 

    if($userID == "") 
    { 
     echo "3"; 
    } 
    else 
    { 

     $TotalProfitPost = $TotalProfit; 
     $LateFeePost = $LateFee; 
     $FinesCostPost2 = $FinesCost; 
     $TravelExpensesPost2 = $TravelExpenses; 
     $FuelCostPost = $FuelCost; 
     $CargoDamagePost = $CargoDamage; 
     $TruckDamagePost = $TruckDamage; 

     var_dump ($TotalProfitPost); 
     var_dump($FinesCostPost2); 
     var_dump($TravelExpensesPost2); 

     $sql="INSERT INTO jwtdriversbank2 (DriverID, LogID, TotalProfit, LateFee, FinesCost, TravelExpenses, FuelCost, CargoDamage, TruckDamage) VALUES ('$userID', COALESCE((Select MAX(LogID) from jwtdriversbank2 tab2 where tab2.DriverID = '$userID'),0)+1,'$TotalProfitPost','$LateFeePost', '$FinesCostPost2' , '$TravelExpensesPost2' ,'$FuelCostPost','$CargoDamagePost','$TruckDamagePost')"; 

     $result = mysql_query($sql); 

     if($result) 
     { 
     } 
     else 
     { 
      die(mysql_error()); 
     } 

    } 

//POINT BACK TO THE PAGE THAT YOU WERE JUST ON// 

// if ($error == " ") { 
    // header('Location: ./drivers-log.php'); 
// } else { 
    // header('Location: ./drivers-log.php?error=' . $error . ''); 

// } 
?> 

而且這裏是我以前在session_start():

///PHP TO INSERT DRIVER'S BANK DETAILS INTO BANK DATABASE 

session_start() 

$host=""; // Host name 
$username=""; // Mysql username 
$password=""; // Mysql password 
$db_name=""; // Database name 
$tbl_name=""; // Table name 

很抱歉,如果你不需要我的所有代碼,但我我不完全確定你需要的代碼片段,但請放心,除了有時發送電子郵件但沒有將詳細信息輸入到數據庫之前,它們都可以正常工作。

+0

session_start()必須在您的代碼的頂部,因此也在註釋的上方。 – Matheno

+0

看看你的查詢。你可以在沒有問題的情況下執行它(不通過PHP)嗎? – Bono

+0

嘿,夥計們,我剛剛試着問一個新問題,但我收到一條消息,表示問題不再被我的賬戶接受,我該怎麼辦? – Danbyization

回答

0

這種情況發生,因爲你不能使用$_SESSION沒有session_start可以看到參考here

您還可以設置session.auto_start爲true,參考這裏here

+0

但我仍然遇到問題,即使我有session_start()在???? – Danbyization

+0

嘗試把'session_start'放在這個腳本的乞求中 – amdorra

+0

試着把它放在<?php和我得到之後的頂部> [21-Aug-2013 15:40:25 Europe/Minsk] PHP解析錯誤:語法錯誤,在第5行的C:\ inetpub \ wwwroot \ hosting \ Dan \ JWT \ drivers-log-send.php中出現意外'$ error'(T_VARIABLE)「 – Danbyization

0

要使用功能$_SESSION您需要啓動一個會話首先,刪除會話會破壞您的代碼。

+0

但我仍然遇到問題,即使我有session_start()在???? – Danbyization

+0

什麼是錯誤? –

+0

沒有錯誤,只是電子郵件已發送,但數據未添加到數據庫。 – Danbyization