拿session_start()会破坏一切

时间:2013-08-21 12:10:34

标签: php

我不确定问题是什么,但我知道的是,当我从我的代码中取出session_start()时,它会破坏整个事情并且电子邮件不会被发送并且数据不会被插入进入数据库。但是在session_start()存在的情况下,用户遇到了发送电子邮件但数据未插入数据库的问题。

这是我的整段代码:

<?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 = "jwtmanagementteam@gmail.com";
    $subject = "$ForumName Log for International";
} elseif ($Division == "Samcro") {
    $to = "jwtsamcrotransport@gmail.com";
    $subject = "$ForumName Log for Samcro";
} elseif ($Division == "Port"){
    $to = "jwtportlogistics@gmail.com";
    $subject = "$ForumName Log for Port'";
} elseif ($Division == "Bio"){
    $to = "jwtbiofuels@gmail.com";
    $subject = "$ForumName Log for Bio-Fuels'";
} elseif ($Division == "WnD"){
    $to = "jwtwagonanddrag@gmail.com";
    $subject = "$ForumName Log for WnD'";
}

//WHO THE EMAIL IS FROM MUST BE A RANDOM OTHER WISE ERRORS HAPPEN
$headers = "From: \"JWT Group\" noreply@jwt-group.co.uk\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\" noreply@jwt-group.co.uk\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

很抱歉,如果您不需要我的所有代码,但我不确定您需要的代码是什么,但请放心,除了有时电子邮件已发送但详细信息之外它一切正常不要进入数据库。

2 个答案:

答案 0 :(得分:0)

发生这种情况是因为如果没有$_SESSION您无法使用session_start,就可以看到引用here

您也可以将session.auto_start设置为true,在此处引用here

答案 1 :(得分:0)

要使用函数$_SESSION,您需要先启动会话,因此删除会话会破坏您的代码。