0

hi all ive tried different things to solve this like remove any white space in php but i cant seem to get rid of this error can anyone suggest anything please.

<?php
include("connect.php");
?> <?php
// define variables and set to empty values
$usernameErr = $emailErr = $passwordErr = $sexErr = $dayErr = $monthErr = $yearErr = "";
$username = $email = $password = $sex = $day = $month = $year = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
   if (empty($_POST["username"])) {
     $usernameErr = "Username is required";
   } else {
     $username = $_POST["username"];
     // check if name only contains letters and whitespace
     if (!preg_match("/^[a-zA-Z ]*$/",$username)) {
       $usernameErr = "Only letters and white space allowed";
     }
   }   
   if (empty($_POST["email"])) {
     $emailErr = "Email is required";
   } else {
     $email = $_POST["email"];
     // check if e-mail address is well-formed
     if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
       $emailErr = "Invalid email format";
     }
   }   
   if (empty($_POST["password"])) {
     $passwordErr = "Password is required";
   } else {
     $password = $_POST["password"];
     // check if password only contains letters and whitespace
     if (!preg_match("/^[a-zA-Z ]*$/",$password)) {
       $passwordErr = "Only letters and white space allowed";
     }
   }  
    if (empty($_POST["sex"])) {
     $sexErr = "Member type is required";
   } else {
     $sex = $_POST["sex"];
   }
if (empty($_POST["day"])) {
     $dayErr = "Day of birth is required";
   } else {
     $day = $_POST["day"];
   }
if (empty($_POST["month"])) {
     $monthErr = "Month of birth is required";
   } else {
     $month = $_POST["month"];
   }
if (empty($_POST["year"])) {
     $yearErr = "Year of birth is required";
   } else {
     $year = $_POST["year"];
   }   
   $sql = "INSERT INTO users (username, email, password, sex, day, month, year)
VALUES ('$username', '$email', '$password', '$sex', '$day', '$month', '$year')";
if ($conn->query($sql) === TRUE) {    

    header("location:  profile.php");

} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

  } 
?> 

ive tried adding

flush_ob();

above it has read in forums and also the following

if (headers_sent()) {
    die("Redirect failed. Please click on this link: <a href=...>");
}

but nothing seems to be working can anyone suggest anything please :)

2
Contributors
1
Reply
11
Views
1 Year
Discussion Span
Last Post by ryantroop
0

So I assume you know why this happens, but if not (and for future readers), I will state it:

Any text output from PHP will also send along headers. Once this is done, headers cannot be modified or resent. This is just the way the PHP world works.

Now, to diagnose your problem...

I do not know what connection.php is doing, but that may have your problem in there if it outputs an error message. You also don't exit after your header redirect (which is, in my opinion, a bad thing), so something later on down the line may be triggering the output.

The only way to know is to put "exit();" at various points in the script above, and see if anything appears on the page. Start at the top (after the include), and then move it down one if check at a time. Please note: lets say you start at line 4 with exit(); and nothing appears, you will have to remove that exit() and then put another one further down. exit() essentially kills your script in place so if you forget to remove it, you will be a sad panda :(

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.