简体   繁体   English

PHP Localhost重定向您太​​多次

[英]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 Localhost重定向您太​​多次

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 我删除了一行代码,在其中尝试调用与数据库建立连接并可以正常工作的文件,但无法进行SELECT * FROM PRUEBA.DBO.AREAS

index.php 的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 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");

?> ?>

It looks like crudQuery.php is unconditionally included in index.php and unconditionally redirects to index.php . 看起来crudQuery.php无条件地包含在index.php并且无条件地重定向到index.php So there's an infinite redirect. 因此,存在无限重定向。

It seems that you access the database even when you are not using crudQuery.php . 似乎即使不使用crudQuery.php ,也可以访问数据库。 So I suggest putting the database connection in a separate file that's included in both index.php and crudQuery.php . 因此,我建议将数据库连接放在一个单独的文件中,该文件包含在index.phpcrudQuery.php That way, you don't need to require crudQuery.php from index.php in order to make a database connection. 这样,您无需从index.php要求crudQuery.php即可建立数据库连接。

db.php db.php中

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

index.php 的index.php

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

// fetch stuff from database
...

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

crudQuery.php 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 的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>

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 尝试更改位于crudQuery.php上的以下代码

header("location: index.php");

to a complete url of your folder where the index.php file were exist. 到存在index.php文件的文件夹的完整URL。 Perhaps like following code (or anything else, as your location of index.php): 可能像下面的代码(或其他任何内容,如index.php的位置):

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

I hope it will work for you. 希望它对您有用。

The problem is in declaration of header("location: index.php"); 问题出在header("location: index.php");声明中header("location: index.php"); . You have to move it in if statement where you check if there is a post request. 您必须将其移入if语句中,以检查是否有post请求。

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 因为您只有在POST请求时才必须重定向

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM