0

Here is my code:

<?php
     
$url = $_GET["id"];

$servername = "XXXX";
$username = "XXXX";
$password = "XXXX";
$dbname = "XXX";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
    
    $sql = "DELETE FROM Table WHERE id='".$url."'";
    
    if ($conn->query($sql) === TRUE) {
        echo 'Row deleted successfully';
    } else {
        echo "Error deleting record: " . $conn->error;
    }
    
    $conn->close();
    
    header('Location:index.php');
        exit;
    ?>

But header redirect is not working... Here is the error log:

Strict Standards: header(): It is not safe to rely on the system's timezone settings. You are required to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/New_York' for 'EST/-5.0/no DST' instead in /homepages/5/d394578306/htdocs/XXXXX/XXXXXX/delete.php on line 24

Warning: Cannot modify header information - headers already sent by (output started at /homepages/5/d394578306/htdocs/XXXXX/XXXXXX/delete.php:1) in /homepages/5/d394578306/htdocs/XXXXXX/XXXXXX/delete.php on line 24

Community
  • 1
  • 1
Pedram
  • 15,766
  • 10
  • 44
  • 73
  • 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) – Martijn Pieters Feb 07 '15 at 10:04
  • It appears to me that the timezone warning of the header causes the output, which in turn makes the function itself not work. Trying doing this: `@header('Location:index.php')`. That will suppress error output for that particular function call. – arik Feb 07 '15 at 10:23

3 Answers3

1
    <?php

Delete the whitespace before the PHP tag - that counts as output and will stop the header() call from working.

You're also doing some echo calls, both of which will also stop the redirect from working as echo will output content/headers to the browser.

Dan Smith
  • 5,685
  • 32
  • 33
1

It appears to me that the timezone warning of the header causes the output, which in turn makes the function itself not work. Try doing this:

@header('Location:index.php')

The @ will suppress error output for that particular function call.

Also, as Bulk correctly pointed out, you should disable your echo calls in lines 17 and 19, or move them below the header method call.

arik
  • 28,170
  • 36
  • 100
  • 156
  • @header also not working, i even removed echos but still not working. – Pedram Feb 07 '15 at 11:15
  • Are you calling this file directly, or is there some other file calling this one using `include` or `require`? – arik Feb 07 '15 at 11:22
  • In that case, try adding `ob_start()` in the very first line. That will disable all subsequent output. – arik Feb 07 '15 at 11:45
0

Based on the timezone error, try this:

<?php
date_default_timezone_set("--HERE YOUR TIMEZONE---");
$url = $_GET["id"];

$servername = "XXXX";
$username = "XXXX";
$password = "XXXX";
$dbname = "XXX";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

    $sql = "DELETE FROM Table WHERE id='".$url."'";

    if ($conn->query($sql) === TRUE) {
        echo 'Row deleted successfully';
    } else {
        echo "Error deleting record: " . $conn->error;
    }

    $conn->close();

    header('Location:index.php');
        exit;
    ?>

Now replace the "--HERE YOUR TIMEZONE---".

Here you can find the correct timezone for your country: http://php.net/manual/en/timezones.php

D3F
  • 156
  • 1
  • 9
  • file name is delete.php – Pedram Feb 07 '15 at 11:14
  • OK, first of all, why did you placed the header on top of your script? When you do that the rest of the file will not be executed. Second, the header function will only work when you have no errors or output in your script. – D3F Feb 07 '15 at 11:18
  • thanks for reply. well i moved header to bottom but still not working. about error, now i have not any error. i have same code in save.php for insert data in sql , it working well, but delete.php not working. – Pedram Feb 07 '15 at 11:30
  • nothing changed, still not working. i put this code in http://writecodeonline.com/php/ it works perfectly but not working in my delete.php file. sound crazy... – Pedram Feb 07 '15 at 12:24
  • Very strange, i think i can´t help you further more. – D3F Feb 07 '15 at 12:30