使用PHP从mySQL数据库的表单提交生成电子邮件

时间:2016-12-08 01:52:25

标签: php mysql forms email

我正在尝试让这段代码正常运行。我有两个问题 - 第一个:

//loop the fetch and concactenate into string
        while ($row = $result->fetch_assoc()) { 
            $string .= "Team ID: ".$row[teamID]."<br>"."Team Name: ".$row[teamName] ."Class: ".$row[class] ."<br><br>";

&#34;类&#34;是表中正确的列标题,但不提取此列中的信息。此表中的每个其他列标题都可以正常工作。

下一个问题。如果我用$ bibNumber替换一个值,这段代码就完美无缺。为什么在这种情况下我无法使用此变量? (或者实际上是任何变量)

//grab all rows from the table for bib# and put into array
        $string = "";
        $sql = "SELECT teamID, teamName, class FROM Teams WHERE bibNumber = ". $bibNumber;
        $result = $conn->query($sql);

以下是完整的代码块(减去HTML)

// dont use button to check submit, just check the post var
if(isset($_POST)) {
    //enter personal information into Authentication table
    $firstName = check_input(@$_POST['firstName']);
    $lastName = check_input(@$_POST['lastName']);
    $password = trim($_POST['pass']); // do not lead with @, it ignores errors.
    $hash = password_hash($password, PASSWORD_DEFAULT);
    $isMaster = check_input(@$_POST['ageCheck']); 
    $region = check_input(@$_POST['region']);
    $email = check_email(@$_POST['email']);
    $region = $_POST['region']; 
    $teamCount = $_POST['teamCount'];// not necessary to scrub, its a select box.

    $teamSqlStatement = "SELECT * FROM Authentication WHERE email='".$_POST['email']."'";

    $teamSql = $conn->query($teamSqlStatement);

    $row = $teamSql->fetch_array(MYSQLI_ASSOC);

    if($row) { 
        if($password = $row['password']) {
        $messageOne = "Your account has been successfully located.";
        }else {
            die("You already have an account but you did not enter the correct password.");
        }
            $bibNumber = $row['bibNumber'];
          $isMaster = $row['isMaster']; 
    }else { 
    $sql = "INSERT INTO Authentication (firstName, lastName, email, password, isMaster ) VALUES ('$firstName', '$lastName', '$email', '$hash', '$isMaster')";
    }

        if($teamCount == 1) {
            $messageTwo = "You owe $30.00 USD.";
        }else { 
            $messageTwo = "You owe $" . (($teamCount * 25) + 5) . ".00"; 
        }

    for($i = 1; $i <= $teamCount; $i++) { 
        $teamNameVar = 'team' . $i . 'Name';
        $teamName = $_POST[$teamNameVar];
        $class = $_POST['team' . $i . 'size'];


        $sql = "INSERT INTO Teams (bibNumber, teamName, class, isMaster, isLeader, region) VALUES 
            ('$bibNumber', '$teamName', '$class', '$isMaster', '0', '$region');";

            $teamSql = $conn->query($sql); 

            if(!$teamSql) {
                echo "An error occured while adding your teams, one of the team names are likely taken.";
            }
    }

    if ($conn->query($sql) === TRUE) {
        $messageOne =  $firstName . " " . $lastName . ", your personal information has been added successfully"."<br>";
        $bibNumber = $conn->insert_id;
        $headers  = "From: someWebsite.com < p@someWebsite.com >\n";
        $headers .= "Cc: testsite < p@someWebsite.com >\n"; 
        $headers .= "X-Sender: someWebsite.com < p@someWebsite.com >\n";
        $headers .= 'X-Mailer: PHP/' . phpversion();
        $headers .= "X-Priority: 1\n"; // Urgent message!
        $headers .= "Return-Path: signup@someWebsite.com\n"; // Return path for errors
        $headers .= "MIME-Version: 1.0\r\n";
        $headers .= "Content-Type: text/html; charset=iso-8859-1\n";
        $subject = 'Signup Confirmation';

        //grab all rows from the table for bib# and put into array
        $string = "";
        $sql = "SELECT teamID, teamName, class FROM Teams WHERE bibNumber = ". $bibNumber;
        $result = $conn->query($sql);

        //loop the fetch and concactenate into string
        while ($row = $result->fetch_assoc()) { 
            $string .= "Team ID: ".$row[teamID]."<br>"."Team Name: ".$row[teamName] ."Class: ".$row[class] ."<br><br>"; 
        }

        $message = "We have received your registration information." . "<br>". "Your 2017 Team(s): <br><br>" . 
            $string . "<br>". "Please save this email for your reference";

        mail($email, $subject, $message, $headers);
    } else {
       die("Error! You can only register once. Please contact us to fix any issues.");
    }

}

1 个答案:

答案 0 :(得分:0)

'Class'是PHP中的一个函数。

$row[class]更改为$row['class'] ....请注意单引号?我建议你为其他人做同样的事情。

其次,请尝试更改$sql = "SELECT teamID, teamName, class FROM Teams WHERE bibNumber = ". $bibNumber;

$sql = "SELECT teamID, teamName, class FROM Teams WHERE bibNumber = '$bibNumber'";

如果使用双引号,则无需关闭字符串以包含变量。