简体   繁体   English

PHP无法重新声明先前定义的函数?

[英]PHP Cannot redeclare function, previously defined?

I'm very new to PHP and I've come across an issue I can't solve. 我是PHP的新手,遇到了一个我无法解决的问题。 I am trying to create a page that adds a new row to a database. 我正在尝试创建一个向数据库添加新行的页面。 I keep getting an error saying I cannot redeclare the checkIfAdminExists function that's defined in currentSessionData.php (prob gonna rename) 我不断收到错误消息,说我无法重新声明currentSessionData.php中定义的checkIfAdminExists函数(可能会重命名)

I suspect the issue has something to do with the fact that I'm setting the header location to the current page (create_admin.php) to process form data through POST, and then when the page is loading it's trying to redeclare the function. 我怀疑问题与以下事实有关:我将标题位置设置为当前页面(create_admin.php),以通过POST处理表单数据,然后在页面加载时尝试重新声明该函数。 I've tried including the file inside 我尝试将文件包含在内

if (!function_exists('checkIfAdminExists')), 如果(!function_exists('checkIfAdminExists')),

but I still get the same error. 但我仍然遇到相同的错误。 What am I doing wrong? 我究竟做错了什么? Is there a better way to approach handling form data with a function? 有没有更好的方法来处理带有函数的表单数据?

create_admin.php create_admin.php

<?php
require_once('currentSessionData.php');

if (isset($_POST['newUsername']) && isset($_POST['newPassword'])) {
    if (checkIfAdminExists($_POST['username'], $_POST['password'])) {
        // admin account already exists
        echo '<script language="javascript">';
        echo 'alert("This admin account already exists");';
        echo '</script>';
        header('Location: create_admin.php');
    }
    else {
        //create new admin account in database
        $username = $_POST['newUsername'];
        $password = $_POST['newPassword'];

        $username = mysql_real_escape_string($username);
        $username = mysql_real_escape_string($password);
        $sqlQuery = "INSERT INTO table_test (username, password)
        VALUES ('$username', '$password')";

    }
}
?>

currentSessionData.php currentSessionData.php

<?php

function checkIfAdminExists($username, $password) {
require_once("db_connection.php");
$sql = "SELECT personid, username, password FROM table_test";
$result = $dbcon->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        if ($row["username"] == $username && $row["password"] == 
$password) {
            return true;
        }
    }
}
else {
    return false;
}
$dbcon->close();
}

?>

As per your comment, you are placing the function_exists check in the wrong place, change your code to the following: 根据您的评论,您将function_exists检查放在错误的位置,将代码更改为以下内容:

if (!function_exists('checkIfAdminExists')) 
{
    function checkIfAdminExists($username, $password) {
        require_once("db_connection.php");

        $sql = "SELECT personid, username, password FROM table_test";
        $result = $dbcon->query($sql);

        if ($result->num_rows > 0) {
            while($row = $result->fetch_assoc()) {
                if ($row["username"] == $username && $row["password"] == $password) {
                    return true;
                }
            }
        } else {
            return false;
        }

        $dbcon->close();
    }
}

Now, with the above code, the function will only be defined if it doesn't already exist. 现在,使用上面的代码,仅当该函数不存在时才对其进行定义。

Note: Please stop using mysql_* as it has been officially deprecated and removed in PHP 7. It would be wise to start learning mysqli_* or PDO and to make use of prepared statements . 注意:请停止使用mysql_*因为它已在PHP 7中正式弃用并删除。明智的做法是开始学习mysqli_*PDO并使用准备好的语句

Update #1 更新#1

You are setting the username variable twice: 您要两次设置用户名变量:

$username = mysql_real_escape_string($username);
$username = mysql_real_escape_string($password);

It should be: 它应该是:

$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);

Update #2 更新#2

As per the documentation of mysql_real_escape_string , it takes an optional second parameter: 根据mysql_real_escape_string的文档,它带有一个可选的第二个参数:

The MySQL connection. MySQL连接。 If the link identifier is not specified, the last link opened by mysql_connect() is assumed. 如果未指定链接标识符,则假定使用mysql_connect()打开的最后一个链接。 If no such link is found, it will try to create one as if mysql_connect() had been called with no arguments. 如果找不到这样的链接,它将尝试创建一个好像没有参数的mysql_connect()一样的链接。 If no connection is found or established, an E_WARNING level error is generated. 如果未找到或建立连接,则生成E_WARNING级错误。

With the above in mind, it means that your connection has not been established. 考虑到上述情况,这意味着尚未建立连接。 So the next logical question is: Where is your connection being established in create_admin.php ? 因此,下一个逻辑问题是:在create_admin.php何处建立连接?

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

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