PHP 本地主机重定向您太多次



我尝试将数据上传到数据库然后部署它们,问题是我看到

本地主机重定向您太多次

删除了一行代码,我尝试调用与数据库建立连接的文件并工作,但无法从 PRUEBA 中选择 *。DBO。地区

索引.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>

准确地说,注释的行是失败的行。

克鲁德查询.php

<?php
////////////////// CONEXION TO DB //////////////////
$serverName = 'SNMXLAP187SQLEXPRESS';
$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");

?>

看起来crudQuery.php无条件包含在index.php中,并无条件地重定向到index.php。所以有一个无限的重定向。

即使您不使用crudQuery.php,您似乎也可以访问数据库。所以我建议将数据库连接放在一个单独的文件中,该文件包含在index.phpcrudQuery.php中。这样,您无需从index.php获取crudQuery.php即可建立数据库连接。

数据库.php

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

索引.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");

或者,您可以从一个文件执行所有操作:

索引.php

// connect to database
$serverName = 'SNMXLAP187SQLEXPRESS';
$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>

抱歉这是我的第一个答案,我相信这不是你想使用的最佳答案,但请让我试试这个......

尝试更改位于 crudQuery 上的以下代码.php

header("location: index.php");

到存在索引.php文件的文件夹的完整 URL。也许就像以下代码(或其他任何内容,作为索引的位置.php(:

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

我希望它能为你工作。

问题出在声明header("location: index.php"); .您必须将其移动到 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";
}

因为您只需要在 POST 请求时

重定向

最新更新