0

Hello guys I am having a problem with my code. I have a simple ecommerce site and I built this with no framework. The site is runnning without errors in my local PC. But when I moved this in the other PC.

This is the process of my wishlist. After submitting the form it will redirect to itself. The process is simply removing the item to the cart.

 //remove item from cart
if(isset($_GET['remove_id'])) {

    if(isset($_SESSION['member'])) {

        fn_delete_item($_GET['remove_id'], $_SESSION['member']);

    } else {

        foreach ($_SESSION['cart'] as $key => $subarray){ 

            if ($subarray['product_id'] == $_GET['remove_id']){ 
                unset($_SESSION['cart'][$key]); 
                break; 
            } 

        }

    }

    header('Location: ?page=yoga_product&product='.$_GET['remove_id'].'&product_id='.$_GET['remove_id']);

} 

//remove all items from cart

if(isset($_GET['removeall'])){

    if(isset($_SESSION['member'])) {

        fn_delete_all_item($_SESSION['member']);
        unset($_SESSION['cart']);  

    } else {

        unset($_SESSION['cart']);  

    }

    header('Location: ?page=yoga_product&product='.$_GET['removeall'].'&product_id='.$_GET['removeall']);

}

...... 


echo "<div style='overflow-y: scroll; height:200px;'>";

echo "<table border='0' style='font-size: 12px; width: 100%' cellpadding='5'>";
    echo "<tr>";
        echo "<td style='background-color: white; color: black; text-align: center'>Product ID</td>";
        echo "<td style='background-color: white; color: black; text-align: center'>Name</td>";
        //echo "<td style='background-color: white; color: black; text-align: center'>Price</td>";
        echo "<td style='background-color: white; color: black; text-align: center'>Sale Price</td>";
        echo "<td style='background-color: white; color: black; text-align: center'>Quantity</td>";
        echo "<td style='background-color: white; color: black; text-align: center'>Total</td>";
        echo "<td style='background-color: white; color: black; text-align: center'><a href='?page=yoga_product&product=".$_GET['product_id']."&removeall=".$_GET['product_id']."' class='hover'>Remove All</a></td>";
    echo "</tr>";

    $total = 0;

    if(isset($_SESSION['member'])) {

        $cart_data = fn_get_cart($_SESSION['member']);

        $total_cart = fn_count_cart($_SESSION['member']);

        while ($row = mysql_fetch_assoc($cart_data, MYSQL_ASSOC) ){

            $link = '?page=yoga_product&product='.$row['product_id'].'&product_id='.$row['product_id'];
            $update = '?page=yoga_product&product='.$row['product_id'].'&update_id='.$row['product_id'];
            $remove = '?page=yoga_product&product='.$row['product_id'].'&remove_id='.$row['product_id']; //this will be the generated URL for removing.

            $compute_total = $row['qty'] * $row['sale_price'];

            echo "<tr>";
                echo "<td style='text-align: center; background-color: gray; color: black'>".$row['product_id']."</td>";
                echo "<td style='text-align: left; background-color: gray; color: black'><a href='$link' style='color: blue; '>".$row['product_name']."</a></td>";
                //echo "<td style='text-align: right; background-color: gray; color: black; font-family: courier'>".number_format($row['price'],2)."</td>";
                echo "<td style='text-align: right; background-color: gray; color: black; font-family: courier'>".number_format($row['sale_price'],2)."</td>";
                echo "<td style='text-align: center; background-color: gray; color: black'>".$row['qty']."</td>";
                echo "<td style='text-align: right; background-color: gray; color: black; font-family: courier'>".number_format($compute_total,2)."</td>";
                echo "<td style='text-align: center; background-color: gray; color: black'>
                        <a href='$remove' class='hover'>Remove this?</a>
                      </td>";
            echo "</tr>";

            $total += ($row['sale_price'] * $row['qty']);

        }

        echo "</table>";

        echo "</div>";

        echo "<br />";

        echo "<div align='right'>";
            echo "<label>Total Items: ".$total_cart."</label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
            echo "<label>Subtotal Amount: </label><input type='text' name='subtotal' value='".number_format($total,2)."' readonly='readonly' style='text-align: right; font-family: courier'>";
            echo "<input type='submit' value='Place Item' disabled='disabled' />";
        echo "</div>";

    }

it will remove the item but I got an error.

Warning: Cannot modify header information - headers already sent by (output started at E:\xampp\htdocs\ahm\pages\main\menu.php:50) in E:\xampp\htdocs\ahm\pages\yoga\clothing_shop.php on line 179

I don't know how I got this error. In my local there's error like this. But when I moved this in other PC. I got this. I am accessing my page in this way. 10.x.x.x./ahm

Please help me guys. Thanks.

Jerielle
  • 7,144
  • 29
  • 98
  • 164
  • 1
    Well it seems that your headers are failing you've already sent some output. It started at menu.php line 50, and you tried to send more headers at clothing_shop.php line 179. You can't send headers after you've sent content, simple as that. – cincodenada Apr 25 '14 at 03:01
  • hmm... But why do I get these errors after I moved the site? In my local I don't encounter this error. – Jerielle Apr 25 '14 at 03:04
  • Probably differing error display settings. My guess is your local setup is suppressing some errors that the new location isn't, so the new location is showing the errors on your page. You'll still want to look at the lines mentioned and see what's generating the output. – cincodenada Apr 25 '14 at 03:29

2 Answers2

1

before you acess anything from $_SESSION, you need to use session_start() and do it before you print anything in your page ans you cant send headers after that too

try using before you acess anything from $_SESSION, you need to use session_start() and do it before you print anything in your page

you can also try using try / catch to discover exactly what is going wrong in your code

farmcoder
  • 33
  • 7
1

This error occurs whenever ANY kind of output is sent before you call the header() function. ( or use $_SESSION ) You can replace your header function with exit() and view the source of your page. If there is any sort of text there, you need to restructure your code so that text does not get outputted before.

echo "hello";

header("location: ...blah blah");

This results in: Warning: Cannot modify header information - headers already sent by (output started at E:\xampp\htdocs\ahm\pages\main\menu.php:50) in E:\xampp\htdocs\ahm\pages\yoga\clothing_shop.php on line 179

Understanding what the header() function really is with greatly help you in the future. Simply, it tells the requesting server what kind of data to expect. If any sort of data is sent before that notice, the server complains.

Ryan Smith
  • 704
  • 2
  • 6
  • 13
  • Thanks but what do you mean by this. "You can replace your header function with exit() and view the source of your page." ? – Jerielle Apr 25 '14 at 03:08
  • This is really just a debugging technique. Think about it. `exit()` stops all execution of the code. So, if you had any output before a `header()` or `$_SESSION`, you would be able to see that by commenting out your `header()` or `$_SESSION` and putting in an `exit()`. You can right click on the page and "view source" to see if there's any output. – Ryan Smith Apr 25 '14 at 03:12
  • ah ok I got it. Sorry for slow understanding. thanks – Jerielle Apr 25 '14 at 03:13