繁体   English   中英

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

[英]Generate email from form submission from mySQL database using PHP

我正在尝试并使此代码正常工作。 我遇到两个问题-第一个:

//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>";

“类”是表中正确的列标题,但未从该列获取信息。 该表中的所有其他列标题都可以正常工作。

下一个问题。 如果我用$ 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.");
    }

}

“类”是PHP中的一个函数。

$row[class]更改$row[class] $row['class'] ....注意单引号吗? 我建议您对其他人也这样做。

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM