0

Im working on a reservation system deep dive and im stuck on the header function not redirecting my user to the 'index.php' page. Here's my PHP code.

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT * FROM customer WHERE transactionum = '$ticket' ";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        echo  "<strong>".$row["fname"]. " " . $row["lname"]."</strong><br>";
    }
} else {

header('location:index.php');

    }
?></li>
    <li class="list-group-item">Paid: <?php if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT * FROM customer WHERE transactionum = '$ticket' ";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        echo  "<strong>".$row["paid"]. "</strong><br>";
    }
} else {
    header('location:index.php');
}
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • You cannot use `header()` after you have echo'd anything to the browser – RiggsFolly Oct 02 '15 at 09:15
  • so how can i redirect them to the index.php? – Kim ocampo Oct 02 '15 at 09:16
  • Do it before sending anything to the browser, the send to the browser could be as simple as a blank line before your first ` – RiggsFolly Oct 02 '15 at 09:18
  • i have conditions there. if result > 0 {echo $row['paid'] } else {header('location:index.php); } – Kim ocampo Oct 02 '15 at 09:20
  • 2
    Yes but just below your first `header()` you output a `` which says to me you have already output at least a `DOCTYPE`, `` a `..` section **etc etc** Judging by what you do in the section of code you show, its pretty much _spagetti code_ Maybe you should spend a little time **refactoring** this script. – RiggsFolly Oct 02 '15 at 09:27
  • There is **no quick fix** for your problem. The only solution is to read and understand the link on CD001's comment. Then go back to your code and **totally refactor its flow** As you dont show all of your code, we cannot even help with that!!! – RiggsFolly Oct 02 '15 at 09:44

2 Answers2

-1

// Create connection

    $conn = mysqli_connect($servername, $username, $password, $dbname);
    // Check connection
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }

     $sql = "SELECT * FROM customer WHERE transactionum = '$ticket' ";
    $result = mysqli_query($conn, $sql);

    if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        $test1 =  "<strong>".$row["fname"]. " " . $row["lname"]."</strong><br>";
    }
    } else {

    header('location:index.php');

    }
    ?></li>
    <li class="list-group-item">Paid: <?php if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
    }

    $sql = "SELECT * FROM customer WHERE transactionum = '$ticket' ";
    $result = mysqli_query($conn, $sql);

    if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        $var1 =  "<strong>".$row["paid"]. "</strong><br>";
    }
    } else {
    header('location:index.php');
    }   
    echo $test1;
    echo $var1;
sandeepsure
  • 1,113
  • 1
  • 10
  • 17
  • This is not an answer. What have you changed apart from the formatting. A good answer has at leat some small amount of description about **what you have done** and **why** you have done it. – RiggsFolly Oct 02 '15 at 09:38
  • You may want to concat the string variables for more than 1 result. – manta Oct 02 '15 at 09:41
  • @RiggsFolly Please check the last two lines where i made the changes. – sandeepsure Oct 02 '15 at 14:50
  • Ok, but you still missed the actual issue. Output has been made to the browser well before this code is run as she is already in the `` section of her HTML as in this code she is outputting `` – RiggsFolly Oct 02 '15 at 14:54
  • @RiggsFolly Answer is partially correct because the first header come before `` and if that condition march then code will execute properly. Share your view on this. – sandeepsure Oct 02 '15 at 15:11
  • But if you see my other comment. [Up here](http://stackoverflow.com/questions/32904039/php-header-function-not-working-in-my-website/32904198?noredirect=1#comment53636805_32904039) that means they must have output a whole load of HTML before getting to the code they actually showed us. Basically the script is a nasty piece of _SPAGETTII_ There is no possible way we could ever fix it be looking at what they showed us – RiggsFolly Oct 02 '15 at 15:17
-1

Look this url http://php.net/manual/es/function.header.php Use capital Letter

header('Location: http://www.example.com/');

Try to use this form and make sure to your code 'else' execution.