提问者:小点点

为什么我的网站未能上传到我的数据库(使用MySQLI),然后没有重定向我?


我为这个问题绞尽脑汁已有一段时间了,但还没有发现问题所在。 我有一个表单(在index.php中),它在按下submit按钮后将用户输入上传到数据库。 正如预期的那样,在按下submit之后,我被重定向到add_records.php,但随后我被困在上面了,尽管包含了这一行

header("location: index.php")

如果我注释掉所有的mysqli代码,重定向就可以正常工作,因此我得出结论,错误与mysqli和上传到数据库有关(可能我错误地处理了地理数据)。 我已经做了所有常见的事情--检查错误日志,检查PHPMyAdmin上的任何错误,但都没有显示错误! 我展示了下面的代码来帮助排除故障,所有敏感信息都替换为'#'(注意-我已经检查了数据库密码等几次,它们都是正确的)。

index.php:

<?php
// Initialize the session
session_start();

// Check if the user is logged in, if not then redirect him to login page
if(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true){
    header("location: ##/records/login.php");
    exit;
}

echo file_get_contents("##/code/header.php");
?>
<div class="page-header">
    <h1>Hi, <b><?php echo htmlspecialchars($_SESSION["username"]); ?></b>. Welcome to our site.</h1>
</div>
<form action="##/records/add_records.php" method="POST">
    <input type="text" name="species" placeholder="Species">
    <br>
    <input type="date" name="date" placeholder="Date">
    <br>
    <input type="time" name="time" placeholder="Time">
    <br>
    <input type="location" name="location" placeholder="Location Name">
    <br>
    <input type="text" name="lat/long" placeholder="Lat Long">
    <br>
    <input type="number" name="count" placeholder="Count">
    <br>
    <input type="text" name="county" placeholder="County (default Norfolk)">
    <br>
    <input type="text" name="country" placeholder="Country (default United Kingdom)">
    <br>
    <input type="text" name="notes" placeholder="Notes">
    <br>
    <button type="submit" name="submit">Submit</button>
</form>

<p>
    <a href="##/records/logout.php" class="btn btn-danger">Sign Out of Your Account</a>
</p>
<?php echo file_get_contents("##/code/footer.php"); ?>

add_records.php:

<?php include_once '##/records/config_bird_records.php';

    $species = mysqli_real_escape_string($conn, $_POST['species']);
    $date = mysqli_real_escape_string($conn, $_POST['date']);
    $time = mysqli_real_escape_string($conn, $_POST['time']);
    $location = mysqli_real_escape_string($conn, $_POST['location']);
    $latlong = mysqli_real_escape_string($conn, $_POST['latlong']);
    $count = mysqli_real_escape_string($conn, $_POST['count']);
    $county = mysqli_real_escape_string($conn, $_POST['county']);
    $country = mysqli_real_escape_string($conn, $_POST['country']);
    $notes = mysqli_real_escape_string($conn, $_POST['notes']);

    $sql = "INSERT INTO bird_db (Species, Date, Time, Location, Lat/Long, Count, County, Country, Notes) VALUES ('$species', $date, $time, '$location', geography::STGeomFromText('POINT($latlong)', 4326), $count, '$county', '$country', '$notes')";

    if(mysqli_query($conn, $sql)){
        echo "Records added successfully.";
    } else{
        echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
    }

    mysqli_close($conn);

    header("location: ##/records/index.php")
?>

config_bird_records.php:

<?php
/* Database credentials.*/
define('DB_SERVER', '##');
define('DB_USERNAME', '##');
define('DB_PASSWORD', '##');
define('DB_NAME', '##');

/* Attempt to connect to MySQL database */
$conn = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);

// Check connection
if($conn === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}
?>

谢谢你的帮助,路易斯


共1个答案

匿名用户

在向浏览器发送任何输出之前,需要调用header方法。

您的代码包括在成功和错误情况下的输出,这意味着重定向永远不会工作。 如果删除,回显“Records added successful.”;,它应该可以工作。