-2

I'm new to php and I've been battling all day trying to find a solution to this error on my remote server, it works fine locally on my computer

Warning: Cannot modify header information - headers already sent by (output started at /home3/ezzuah/public_html/onecedishop.com/page.php:3) in /home3/folder_path/page.php on line 62

php

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

<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($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;
}
}

$colname_rs_users = "-1";
if (isset($_SESSION['MM_Username'])) {
  $colname_rs_users = $_SESSION['MM_Username'];
}
mysql_select_db($database_conn, $conn);
$query_rs_users = sprintf("SELECT * FROM regis WHERE username = %s", GetSQLValueString($colname_rs_users, "text"));
$rs_users = mysql_query($query_rs_users, $conn) or die(mysql_error());
$row_rs_users = mysql_fetch_assoc($rs_users);
$totalRows_rs_users = mysql_num_rows($rs_users);
?>

<?php


// Create connection
$conn= new mysqli($hostname_conn, $username_conn, $password_conn,$database_conn);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$user=$row_rs_users['username'];
$sql = "UPDATE regis SET credit_unit = credit_unit - 1  WHERE username='$user'";
if ($conn->query($sql) === TRUE) { 
} else {
    echo "Error updating record: " . $conn->error;
}
header("Location:bid_ok_m.php"); 
$conn->close();
?> 


<?php
mysql_free_result($rs_users);
?>

When i preview my page i get this error i read on the web and i was told there shouldn't be any spaces before the start and end of the script which i did but still getting the error

i_user
  • 511
  • 1
  • 4
  • 21
  • You can't modify headers (with your line `header("Location:bid_ok_m.php");`) after output has been sent. – Ben Feb 03 '16 at 11:14

2 Answers2

1

There are multiple instances of you closing the PHP tags, leaving whitespace, then starting PHP again.

For example:

    $totalRows_rs_users = mysql_num_rows($rs_users);
?>
    //<-- THERE IS WHITESPACE HERE!
<?php

Remove all the whitespace, or just delete the close/open of PHP tags, to prevent output being sent before headers.

Ben
  • 8,894
  • 7
  • 44
  • 80
  • remove all the php tags? then i guess the script wouldn't work? can you give me a sample with my script? or do you mean all the scripts should be wrapped into one open and close tag? – i_user Feb 03 '16 at 11:21
  • Remove *all* the `` tags **except** the opening tag (the ` – Ben Feb 03 '16 at 11:22
  • Putting it all together as one makes the update script not to work. – i_user Feb 03 '16 at 11:30
  • @i_user No I can't - if there is whitespace, the `header()` function *should not work at all*. I don't know how it's working locally! – Ben Feb 03 '16 at 11:31
0

It because you are opening and closing php tags.

It has spaces between them which is considered as output on browser.

If you want redirection to work, you need to have only one <?php opening tag.

It is recommended that you should not close ?> tag.

Pupil
  • 23,834
  • 6
  • 44
  • 66