-1

So I am getting this error:

Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/add_user.php:50) in /Applications/MAMP/htdocs/add_user.php on line 55

The code on line 55 is:

55 header(sprintf("Location: %s", $insertGoTo));

script is starting with:

 <?php require_once('Connections/connHotel.php'); ?>
 <?php
 function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "userform")) {
  $insertSQL = sprintf("INSERT INTO clients (title, firstName, lastName, address1, address2, town, province, country, postCode, telephone, email) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['title'], "text"),
                       GetSQLValueString($_POST['firstName'], "text"),
                       GetSQLValueString($_POST['lastName'], "text"),
                       GetSQLValueString($_POST['address1'], "text"),
                       GetSQLValueString($_POST['address2'], "text"),
                       GetSQLValueString($_POST['town'], "text"),
                       GetSQLValueString($_POST['province'], "text"),
                       GetSQLValueString($_POST['country'], "text"),
                       GetSQLValueString($_POST['postcode'], "text"),
                       GetSQLValueString($_POST['telephone'], "text"),
                       GetSQLValueString($_POST['email'], "text"));

mysqli_select_db($connHotel, $database_connHotel);
  $Result1 = mysqli_query($connHotel, $insertSQL) or die(mysqli_error($connHotel));

  $insertGoTo = "booking_details.php?email=$email";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }

I don't understand what the warning means.

alex
  • 2,464
  • 23
  • 32
aimaha
  • 1
  • 3
  • If you post the preceding code of the script, you can get a more reliable answer. – alex Jul 23 '14 at 16:36
  • possible duplicate of [How to fix "Headers already sent" error in PHP](http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php) – meda Jul 23 '14 at 16:38
  • Ok, this is what i've got. – aimaha Jul 23 '14 at 16:38
  • Make sure `` is the first line with no preceding space or line break. There could be an mySQL error, which could be sent first. Depends on your server configuration. – alex Jul 23 '14 at 16:41
  • There isn't anything before that alex, and it is the first line. – aimaha Jul 23 '14 at 16:43
  • There's a notice as well: Notice: Undefined variable: email in /Applications/MAMP/htdocs/add_user.php on line 50 but that's the only other error or etc. – aimaha Jul 23 '14 at 16:45
  • Remove all issues that cause notices. That notice will be sent before the header, which causes the header redirect to fail. And merge the two PHP section. There is a line break in-between both, resulting in the same error. – alex Jul 23 '14 at 16:51
  • I'm having trouble finding why email is undefined. – aimaha Jul 23 '14 at 16:56
  • Variable `$email` is not defined. Seems you miss an `$email = $_POST['email'];`. – alex Jul 23 '14 at 17:10
  • It's mentioned in the GetSQLValueString like the others though. – aimaha Jul 23 '14 at 17:23
  • Yes, but the other POST variable values aren't accessed a second time. So far, there is a POST array key `email` but no variable `$email`. You might add `$email = $_POST['email'];` before `$email` is read for the first time. – alex Jul 23 '14 at 17:33
  • Thank you Alex! I added that variable declaration before the insert and it worked. – aimaha Jul 23 '14 at 18:27

1 Answers1

0

Header must be sent before all other output. Make sure you don't send any other characters before that. Even your PHP opening sequence <?php must not have any leading characters.

alex
  • 2,464
  • 23
  • 32