Link to home
Start Free TrialLog in
Avatar of dereksheahan
dereksheahan

asked on

Record Update Problem

Hi all,
Could anyone tell me how to sort out this error: Warning: Cannot modify header information - headers already sent by (output started at /home/reachate/public_html/Connections/GSM_backup.php:4) in /home/reachate/public_html/Remote/nokiaremotereturnlive.php on line 57
Not sure what to do with it?
Here's the page and the connection file.
Thanks so much,
D

<?php require_once('../Connections/GSM_backup.php'); ?>
<?php


// Deconcatenate the varaiables in the 'os0' post field...
$customVar = $_REQUEST['custom'];
$arrayVar = explode("---",$customVar);
$IMEI = $arrayVar[0];
$username = $arrayVar[1];
$email = $arrayVar[2];


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"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO nokia_removal_table (nokia_username, nokia_email_add, nokia_IMEI_nr) VALUES (%s, %s, %s)",
                       GetSQLValueString($_POST['nokia_username'], "text"),
                       GetSQLValueString($_POST['nokia_email_add'], "text"),
                       GetSQLValueString($_POST['nokia_IMEI_nr'], "int"));

  mysql_select_db($database_GSM_mobile, $dbh);
  $Result1 = mysql_query($insertSQL, $dbh) or die(mysql_error());

  $insertGoTo = "../mainpage.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}
$name = $_POST['txt_name'] ;
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<form id="nokia_return" name="nokia_return" method="post" action="">
 
</form>

<form method="post" name="form1" action="<?php echo $editFormAction; ?>">
  <table align="center">
    <tr valign="baseline">
      <td nowrap align="right">Username:</td>
      <td><input name="nokia_username" type="text" value="<?php echo $username; ?>" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">Email Address:</td>
      <td><input type="text" name="nokia_email_add" value="<?php echo $email; ?>" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">IMEI Number</td>
      <td><input type="text" name="nokia_IMEI_nr" value="<?php echo $IMEI; ?>" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">&nbsp;</td>
      <td><input type="submit" value="Insert record"></td>
    </tr>
  </table>
  <input type="hidden" name="MM_insert" value="form1">
</form>

<p>&nbsp;</p>
</body>
</html>


And here's the connection (it works fine for other pages)
<?php
$dbh=mysql_connect ("localhost", "reachate_mainuse", "xxx") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("reachate_corkgsmdb");
?>
Avatar of hiteshgupta1
hiteshgupta1

This error comes when something is sent to the browser before you session started. Check to see if you have any empty lines at the top of your code
Avatar of dereksheahan

ASKER

Hi hiteshgupta1,
I can't seem to understand what it is I need to change in order to make it work. I've checked the code and have no spaces at the top of the page. I'm not using session variables like the other example. I am however passing variables to the page and taking them out of the POST_array. Is it the $InsertGoTo Statement that is the problem?
Thanks,
D
What we ment is something like "echo", "print", "sprint", "sprintf" etc..

like the line 44 u have in your code:
$insertSQL = sprintf("INSERT INTO nokia_removal_table (nokia_username, nokia_email_add, nokia_IMEI_nr) VALUES (%s, %s, %s)",

if it isn't the "sprintf" its something inside the GSM_backup.php that doing that, some echo or print...
This line might be the problem because it is trying to change the header

header(sprintf("Location: %s", $insertGoTo));
SOLUTION
Avatar of Khanh Doan
Khanh Doan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Your code is using the header function, which won't work because some output has already been sent to the browser. PHP is telling you that the output started in home/reachate/public_html/Connections/GSM_backup.php on line 4. You should look there for your problem.
post GSM_backup.php for us.
<?php
$dbh=mysql_connect ("localhost", "reachate_mainuse", "xxx") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("reachate_corkgsmdb");
?>

the problem is probably here, right after the ?> which means your have something like a newline or space.

delete everything before the ?> and type:
<?php
$dbh=mysql_connect ("localhost", "reachate_mainuse", "xxx") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("reachate_corkgsmdb"); ?>

making sure NOTHING comes after the ?>, no newline, try openening a new file and copying it there and saving it again in case a stray control character has somehow been inserted.
>> dereksheahan,
is ur problem solved??
haven't heard nething from u ?

Hi guys,
Sorry was on holiday there for a week, thats why I haven't been in contact :) Unfortunatley none of the above solves the problem. maUru, I looked at my connection file and the last line already had the php close statement on it.
I'm sure it is this part which is causing the problem:
$insertGoTo = "../mainpage.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo)); // this line in particular

I just need to know how to change this so that the page is still redirected to mainpage.php on update.
Thanks so much,
D
Hi bonmat86,
I've tried changing the page to this but it still gives me the same error:

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "nokia_return")) {
$nokia_username = addslashes($_POST['nokia_username']);
$nokia_email_add = addslashes($_POST['nokia_email_add']);
$nokia_IMEI_nr = addslashes($_POST['nokia_IMEI_nr']);

$insertSQL = "INSERT INTO nokia_removal_table (nokia_username, nokia_email_add, nokia_IMEI_nr) VALUES (' . $nokia_username  . ', ' . $nokia_email_add . ', ' . $nokia_IMEI_nr . ')";

  mysql_select_db($database_GSM_mobile, $dbh);
  $Result1 = mysql_query($insertSQL, $dbh) or die(mysql_error());

  $insertGoTo = "../mainpage.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}
Sorry ignore my last comment. When I try the above script in my last post it doesn't generate an error but reloads the same page and doesn't update the DB. Any suggestions as to what I might be doing wrong?
Thanks,
D
first of all, MM_insert (is probably not set which is why you are getting reloaded to the same page

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "nokia_return")) {

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

this is NOT your problem, line 4 of gsmbackup is.

try and use hard coded the gsm backup, dont include it:

<?
$dbh=mysql_connect ("localhost", "reachate_mainuse", "xxx") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("reachate_corkgsmdb");

// Deconcatenate the varaiables in the 'os0' post field...
$customVar = $_REQUEST['custom'];
....
....
....
....
Hi maUru,
Yes I have the MM_insert statement there. Thanks, I've also hard coded the connection but I think that was working now anyways.

Here's the code I have. It gives no error but doesn't update the DB at all. It also still returns to the same page.
Thanks,
D

<?php $dbh=mysql_connect ("localhost", "corkgsm_admin", "xxx") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("corkgsm_corkgsmdb"); ?>
<?php


// Deconcatenate the varaiables in the 'os0' post field...
$customVar = $_REQUEST['custom'];
$arrayVar = explode("---",$customVar);
$IMEI = $arrayVar[0];
$username = $arrayVar[1];
$email = $arrayVar[2];


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"] == "nokia_return")) {
$nokia_username = addslashes($_POST['nokia_username']);
$nokia_email_add = addslashes($_POST['nokia_email_add']);
$nokia_IMEI_nr = addslashes($_POST['nokia_IMEI_nr']);

$insertSQL = "INSERT INTO nokia_removal_table (nokia_username, nokia_email_add, nokia_IMEI_nr) VALUES (' . $nokia_username  . ', ' . $nokia_email_add . ', ' . $nokia_IMEI_nr . ')";

  mysql_select_db($database_GSM_mobile, $dbh);
  $Result1 = mysql_query($insertSQL, $dbh) or die(mysql_error());

  $insertGoTo = "../mainpage.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}
$name = $_POST['txt_name'] ; ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>


<form  id="nokia_return" method="post" name="nokia_return" action="<?php echo $editFormAction; ?>">
  <table align="center">
    <tr valign="baseline">
      <td nowrap align="right">Username:</td>
      <td><input name="nokia_username" type="text" value="<?php echo $username; ?>" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">Email Address:</td>
      <td><input type="text" name="nokia_email_add" value="<?php echo $email; ?>" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">IMEI Number</td>
      <td><input type="text" name="nokia_IMEI_nr" value="<?php echo $IMEI; ?>" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">&nbsp;</td>
      <td><input type="submit" value="Insert record"></td>
    </tr>
  </table>
  <input type="hidden" name="MM_insert" value="form1">
</form>

<p>&nbsp;</p>
</body>
</html>





ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Great Thanks maUru, should have seen that one!
I'll split the points because I was using bonmat86's layout.
Cheers,
D
sounds good to me ;p