I can’t go to another page with the header function

Asked

Viewed 1,276 times

1

I’m having trouble with the header(), I made a schedule to change the database data and when I click on the button to change it goes to a blank page then I put the header("Location: AdmAgenda.php"); so that you could return to the page already with updated data but is giving error:

The localhost page is not working

Excessive redirection by localhost Try to clear cookies.

What can I do to fix?

my code:

<?php 
$idSeg = filter_input(INPUT_GET, "id-segunda");
$seg = filter_input(INPUT_GET, "seg");

$idTerca = filter_input(INPUT_GET, "id-ter");
$tercaa = filter_input(INPUT_GET, "ter");


$idQuarta = filter_input(INPUT_GET, "id-quarta");
$quartaa = filter_input(INPUT_GET, "qua");


$idQuinta = filter_input(INPUT_GET, "id-quinta");
$quintaa = filter_input(INPUT_GET, "qui");

$idSexta = filter_input(INPUT_GET, "id-sexta");
$sextaa = filter_input(INPUT_GET, "sex");

$idSabado = filter_input(INPUT_GET, "id-sabado");
$sabadoo = filter_input(INPUT_GET, "sab");


include("conexao.php");


if($link) {
    $segg = mysqli_query($link, "UPDATE horas_segunda set horas_de_segunda='$seg' where id='$idSeg';");
    $terc = mysqli_query($link, "UPDATE horas_terca set horas_de_terca='$tercaa' where id='$idTerca';");
    $quarr = mysqli_query($link, "UPDATE horas_quarta set horas_de_quarta='$quartaa' where id='$idQuarta';");
    $quinn = mysqli_query($link, "UPDATE horas_quinta set horas_de_quinta='$quintaa' where id='$idQuinta';");
    $sexx = mysqli_query($link, "UPDATE horas_sexta set horas_de_sexta='$sextaa' where id='$idSexta';");
    $sabaa = mysqli_query($link, "UPDATE horas_sabado set horas_de_sabado='$sabadoo' where id='$idSabado';");
    header("Location: AdmAgenda.php");
}
else{
    die("Erro: " .mysqli_error($link));
}
?>
  • 2

    Is the redirect to the same page? Infinite loop?

  • this... would have to go back to the page

  • 1

    Put a if to prevent you from entering the condition that makes the changes and redirect.

  • Already managed to solve?

2 answers

1

I don’t agree with the way you are doing things, because only redirecting will not give the "Update successfully performed" information to the user, but if you change:

if($link) {

for:

if($link && isset($_GET["seg"])) { 

already solves the problem the way you want.

0

Despite not being best practice, it’s okay to redirect to the same page as long as you know what you’re doing and can write your logic in a way that never falls into an infinite redirect (the browser actually cancels the loop when it finds that there are many redirects to the same page, launching something like ERR_TOO_MANY_REDIRECTS). So it is not enough to stop navigation.

In your specific problem, you are just checking whether the variable $link (that from the context, it is understood that it is an open connection with the mysql) is not null, that is to say: at all times (if all is well).

To avoid this, instead of checking if there is a connection, make sure the url is sending the parameters to save. I suggest putting above your if($link) { something like that:

if ( $idSeg && $seg && $idTerca && $tercaa && $idQuarta && $quartaa &&
     $idQuinta && $quintaa && $idSexta && $sextaa && $idSabado && $sabadoo ) {

    if($link) {
        /* funções de update */
        header("Location: AdmAgenda.php");
    } else{
        die("Erro: " .mysqli_error($link));
    }

}

This would imply only updating the database and redirecting if all these parameters are present (I don’t know if this is the logic of your application, but it is for explanation purposes only, you adapt). A more clean is check one by one. Done this, when you redirect, the parameters will not be sent and consequently will not fall into this if.

  • So when I did the raw programming every time I changed the data and clicked on the button it would go to another blank page, then I thought if it was to fall into a blank page that falls on the Adm.php page again so it already automatically updates the page

  • And colleague I tried this way keep going to blank page what would be?

  • Good morning @Nathan, has over redirect error been fixed? the script AdmAgenda.php is what renders the form, right? when it does not fall into the 'headerpara redirecionar, ele continua o script normalmente, e vai executar tudo que está nele, depois desseif`.

  • I could not, not even with this code friend, the script that changes the bank and another, this AdmAgenda.php it would be the body from where I visualize and put the data to be able to change, when clicking the change button should give a redirect on the page to be able to see again (as if it were an F5).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.