Luis Abraham
Luis Abraham

Reputation: 33

PHP Localhost redirected you too many times

I try to upload data to a database and then deploy them, the problem is that I see

Localhost redirected you too many times

I delete a line of code where I try to call the file where I make the connection with my database and works but can´t make the SELECT * FROM PRUEBA.DBO.AREAS

index.php

<html>
<body>
    <div class="container mt-2 mb-4 p-2 shadow bg-white">
        <form action="crudQuery.php" method="POST">
            <div class="form-row justify-content-center">
                <div class="col-auto">
                    <input type="text" required name="area" class="form-control" id="area" placeholder="Ingrese Area">
                </div>
                <div class="col-auto">
                    <button type="submit" name="save" class="btn btn-info">Agregar Area</button>
                </div>
            </div>
        </form> 
    </div>
    <?php //require_once("crudQuery.php");?>
    <div class="container">
        <?php if(isset($_SESSION['msg'])): ?>
            <div class="<?= $_SESSION['alert'];?>">
                <?= $_SESSION['msg']; ?>
            </div>
        <?php endif; ?>
        <table class="table">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Area</th>
                    <th>Action</th>

                </tr>
            </thead>
            <tbody>
                <form action="crudQuery.php" method="POST">
                    <?php
                    #Show DB
                    $sQuery = "SELECT * FROM prueba.dbo.AREAS";
                    $sResult = sqlsrv_query($conn,$sQuery) or sqlsrv_errors();
                    ?>
                </form>
            </tbody>
        </table>
    </div>  
</body>
</html>

To be precise, the commented line is the one that fails.

crudQuery.php

<?php
////////////////// CONEXION TO DB //////////////////

$serverName = 'SNMXLAP187\SQLEXPRESS';
$connectionInfo = array( "Database"=>"prueba");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if(isset($_POST['save'])){
    if(!empty($_POST['area'])){
        $area = $_POST['area'];
        $iQuery = "INSERT INTO prueba.dbo.AREAS(AREA)
        values('$area')";
        echo $iQuery;
        $sqlRes=sqlsrv_query($conn,$iQuery);
        if(sqlsrv_execute($sqlRes)){
             $_SESSION['msg'] = "Nueva Area Agregada";
             $_SESSION['alert'] = "alert alert-success";
        }
    }
}
else{
    #alert msg
     $_SESSION['msg'] = "No se pudo agregar el Area";
     $_SESSION['alert'] = "alert alert-warning";
}
header("location: index.php");

?>

Upvotes: 1

Views: 199

Answers (3)

kokom
kokom

Reputation: 121

I'm sorry this is my first answer and I believe this isn't the best answer you'd like to use but please let me try up this...

Try to change following code located on crudQuery.php

header("location: index.php");

to a complete url of your folder where the index.php file were exist. Perhaps like following code (or anything else, as your location of index.php):

header("location: //localhost/index.php");

I hope it will work for you.

Upvotes: 1

showdev
showdev

Reputation: 29178

It looks like crudQuery.php is unconditionally included in index.php and unconditionally redirects to index.php. So there's an infinite redirect.

It seems that you access the database even when you are not using crudQuery.php. So I suggest putting the database connection in a separate file that's included in both index.php and crudQuery.php. That way, you don't need to require crudQuery.php from index.php in order to make a database connection.

db.php

// connect to database
$serverName = 'SNMXLAP187\SQLEXPRESS';
$connectionInfo = array( "Database"=>"prueba");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

index.php

// connect to database
require_once 'db.php';

// fetch stuff from database
...

// display form           
?>
<form action="crudQuery.php" method="POST">
    ...
</form>

crudQuery.php

// connect to database
require_once 'db.php';

if(!empty($_POST['save'])){
    // get posted value
    // insert into database
    // set session values
    ...
}

// redirect
header("location: index.php");

Alternatively, you can potentially do everything from one file:

index.php

// connect to database
$serverName = 'SNMXLAP187\SQLEXPRESS';
$connectionInfo = array( "Database"=>"prueba");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

// process submission
if(!empty($_POST['save'])){

    // get posted value
    // insert into database
    // set session values
    ...

    // redirect
    header("location: index.php");

}

// define msg and alert
$msg = !empty($_SESSION['msg'])
    ? $_SESSION['msg']
    : "No se pudo agregar el Area";

$alert = !empty($SESSION['alert'])
    $_SESSION['alert']
    : "alert alert-warning";

// fetch stuff from database
...

// display form    
?>
<form action="" method="POST">
    ...
</form>

Upvotes: 1

Serghei Leonenco
Serghei Leonenco

Reputation: 3507

The problem is in declaration of header("location: index.php");. You have to move it in if statement where you check if there is a post request.

if(isset($_POST['save'])){
    if(!empty($_POST['area'])){
        $area = $_POST['area'];
        $iQuery = "INSERT INTO prueba.dbo.AREAS(AREA)
        values('$area')";
        echo $iQuery;
        $sqlRes=sqlsrv_query($conn,$iQuery);
        if(sqlsrv_execute($sqlRes)){
             $_SESSION['msg'] = "Nueva Area Agregada";
             $_SESSION['alert'] = "alert alert-success";
             header("location: index.php");
        }
    }
}
else{
    #alert msg
     $_SESSION['msg'] = "No se pudo agregar el Area";
     $_SESSION['alert'] = "alert alert-warning";
}

Because you have to redirect ONLY when it was POST request

Upvotes: 0

Related Questions