首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用PHP从mySQL数据库从表单提交中生成电子邮件

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

Stack Overflow用户
提问于 2016-12-08 01:52:25
回答 1查看 36关注 0票数 0

我正试着让这段代码正常工作。我有两个问题-第一个问题:

代码语言:javascript
复制
//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>";

"class“是表中正确的列标题,但不获取来自该列的信息。这个表中的其他列标题都工作得很好。

下一个问题。如果我用一个值来代替$bibNumber,那么这段代码就能很好地工作。为什么在这种情况下我不能使用这个变量?(或真正的任何变量)

代码语言:javascript
复制
//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);

这里是完整的代码块(减去)

代码语言:javascript
复制
// 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.");
    }

}
EN

回答 1

Stack Overflow用户

发布于 2016-12-08 03:16:59

类是PHP中的一个函数。

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

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

代码语言:javascript
复制
$sql = "SELECT teamID, teamName, class FROM Teams WHERE bibNumber = '$bibNumber'";

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41030503

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档