PHP脚本未知错误

时间:2015-05-12 16:54:13

标签: php if-statement post for-loop

我有一个用户输入数据的注册表单。然后将它发布到同一页面并检查空字段后,变量放在$_SESSION数组中,用户被定向到另一个表单在表格中输入另一组数据。
在发布这些变量之后,从$_SESSION中提取上一页中的变量,并检查新值是否为空条目。在for循环中检查它们之后,php脚本神秘地停止(屏幕上不再显示die("<h1> GOT HERE! </h1>"))并且页面继续加载。等待一段时间后,页面重新加载。
我一直在使用die()一段时间来查找错误,但它只是在for循环和if语句之间没有回应,并且没有明显的原因它不应该。看看:

    <?php
     session_start();
    function sanitize($data) {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    } 
    if($_SESSION["registering"] != 1){
    die("This page is to be used only when registering. <a href=\"tmcgb.com\">Go to home</a> page and select the seminar you want and click Register");
    }else if($_SESSION["registered"] == 1){
    die("You have already registered. Thank you. You can no longer access this page. To view your registration report, <a href=\"registration_report.php\"> click here. </a>");

    }else{

    $id = sanitize($_SESSION["id"]);
    $attendees = sanitize($_SESSION["attendees"]);
    $ref_code = sanitize($_SESSION["Ref_Code"]);
    $email = sanitize($_SESSION["email"]);
    $prefix = sanitize($_SESSION["prefix"]);
    $first_name = sanitize($_SESSION["first_name"]);
    $last_name = sanitize($_SESSION["last_name"]);
    $company = sanitize($_SESSION["company"]);
    $address1 = sanitize($_SESSION["address1"]);
    $address2 = sanitize($_SESSION["address2"]);
    $user_city = sanitize($_SESSION["city"]);
    $phone = sanitize($_SESSION["phone"]);
    $responsibility = sanitize($_SESSION["responsibility"]);
    $who_referred = sanitize($_SESSION["who-referred"]);
    $role = sanitize($_SESSION["role"]);

    $server = "MYREAL_DATABASE_SERVER";
    $username = "CORRECT_USERNAME";
    $password = "CORRECT_PASSWORD";
    $dbname = "DB_NAME";

    $conn = new mysqli($server, $username, $password, $dbname);

    $query = "
        SELECT *
        FROM Seminar_Detail
        WHERE Detail_id = '". $id ."'
    ";

    $result = $conn->query($query);
    if($result->num_rows == 0 ){
      header("Location: ManagementSeminars.php");
    }

    $seminar = $result->fetch_assoc();
    $name = $seminar["Seminar_Name"];
    $city = $seminar["City"];
    $from = $seminar["From"];
    $to = $seminar["To"];
    $fee = '';

    $query = "SELECT Value FROM Fee WHERE Seminar_Name = '". $name ."' AND Currency = 'GBP'";
    $result = $conn->query($query);
    if($result->num_rows > 0){
        $row = $result->fetch_assoc();
        $fee = $row["Value"];
    }


    if($_SERVER["REQUEST_METHOD"] == "POST"){
        $terminate = 0;
        for($i = 1; i < ($attendees + 1); $i++){
            if(isset($_POST["prefix-".$i]) && isset($_POST["first_name-".$i]) && isset($_POST["last_name-".$i]) && isset($_POST["position-".$i])){
                $terminate = 0;
            }else{
                $terminate = 1;
            }
        }
        die("<h1>".$terminate."</h1>");
            if($terminate != 1){
                $server = "SERVER";
                $username = "USERNAME";
                $password = "PASSWORD";
                $dbname = "DBNAME";

                $conn = new mysqli($server, $username, $password, $dbname);
                $query = "
                INSERT INTO Registry (Seminar_Name, Number_Attendees, Email, Prefix, First_Name, Last_Name, Company, `Address 1`, `Address 2`, City, Phone, Responsibility, Role, Who_Referred, Ref_Code)
                VALUES ('". $name ."', '". $attendees ."', '".$email."', '".$prefix."', '".$first_name."', '".$last_name."', '".$company."', '".$address1."', '".$address2."', '".$user_city."', '".$phone."', '".$responsibility."', '".$role."', '".$who_referred."', '".$ref_code."')
                ";
                $conn->query($query);

                //ignore this part please
                /*$query = "SELECT Registry_ID FROM Registry WHERE Ref_Code = '". $_SESSION["Ref_Code"] ."'";
                $result = $conn->query($query);
                $row = $result->fetch_assoc();
                $registry_id = $row["Registry_ID"];
                $attendee_first_name = "";
                $attendee_last_name = "";
                $attendee_position = "";

                $stmt = $conn->prepare("
                INSERT INTO Attendee (First_Name, Last_Name, Position, Registry_ID)
                Values (?, ?, ?, ?)
                ");
                $stmt->bindParam("ssss", $attendee_first_name, $attendee_last_name, $attendee_position, $registry_id);
                for($i = 1; $i < $_SESSION["attendees"] + 1; $i++){
                    $attendee_first_name = sanitize($_POST["first_name-".$i]);
                    $attendee_last_name = sanitize($_POST["last_name-".$i]);
                    $attendee_position = sanitize($_POST["position-".$i]);
                    $stmt->execute();
                }*/
            }else{
                $errorMessage = "<div class='alert alert-danger alert-dismissable'>
                    <strong>Oops!</strong> You have not entered all values.
                </div>";
            }

    }
}
?>

我很肯定这不是语法错误。任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:2)

问题可能出在这一行

for($i = 1; i < ($attendees + 1); $i++){

您错过了$登录i。它应该是:

for($i = 1; $i < ($attendees + 1); $i++){

使用error reporting后,会发出undefined constant i通知信号。

error reporting添加到文件的顶部,这有助于查找错误。

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

旁注:错误报告应仅在暂存时完成,而不是生产。