简体   繁体   中英

Generate email from form submission from mySQL database using PHP

I am trying and trying to get this code to work. I am having two problems - first one:

//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" is the correct column heading in the table, but the information from this column is not fetched. Every other column heading from this table works just fine.

Next problem. If I substitute a value for $bibNumber, this code works perfectly. Why am I not able to use this variable in this situation? (Or really any variable)

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

Here is the full block of code (minus the 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.");
    }

}

'Class' is a function in PHP.

Change $row[class] to $row['class'] .... notice the single quote? I suggest you to do the same for the others.

Secondly, try changing $sql = "SELECT teamID, teamName, class FROM Teams WHERE bibNumber = ". $bibNumber; $sql = "SELECT teamID, teamName, class FROM Teams WHERE bibNumber = ". $bibNumber;

to

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

You don't need to close your strings to include variable if youre using double quote.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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