1

I'm very new to PHP and I've come across an issue I can't solve. 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)

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. I've tried including the file inside

if (!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

<?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

<?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();
}

?>
acc1999
  • 25
  • 5
  • What line is the error thrown and what is the code on that line? – Script47 Jun 05 '18 at 03:32
  • Where are you using `function_exists`? – Script47 Jun 05 '18 at 03:33
  • 1
    Stop using `mysql_*`, make use of `mysqli_*` or `PDO` *with* prepared statements. – Script47 Jun 05 '18 at 03:34
  • Fatal error: Cannot redeclare checkIfAdminExists() (previously declared in /home/intertid/public_html/currentSessionData.php:3) in /home/intertid/public_html/currentSessionData.php on line 3 – acc1999 Jun 05 '18 at 03:35
  • *I keep getting an error saying I cannot redeclare the checkIfAdminExists function that's defined in currentSessionData.php* - Did you try renaming the function? – Romeo Sierra Jun 05 '18 at 03:35
  • This code isn't using function_exists, I just surrounded the code on line 2 of create_admin.php with an if (!function_exists('checkIfAdminExists')) and it still doesn't work – acc1999 Jun 05 '18 at 03:36
  • You are meant to put the `function_exists` around the declaration of the function. – Script47 Jun 05 '18 at 03:38
  • Yes, i've tried renaming it. No luck – acc1999 Jun 05 '18 at 03:38
  • Thanks @Script47, that fixed one issue. Unfortunately, now I'm seeing Warning: mysql_real_escape_string(): Access denied for user ''@'localhost' (using password: NO) in /home/intertid/public_html/create_admin.php on line 28 – acc1999 Jun 05 '18 at 03:42

1 Answers1

0

As per your comment, you are placing the function_exists check in the wrong place, change your code to the following:

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.

Update #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

As per the documentation of mysql_real_escape_string, it takes an optional second parameter:

The MySQL connection. If the link identifier is not specified, the last link opened by mysql_connect() is assumed. If no such link is found, it will try to create one as if mysql_connect() had been called with no arguments. If no connection is found or established, an E_WARNING level error is generated.

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?

Script47
  • 14,230
  • 4
  • 45
  • 66
  • That's what I've just fixed, but now I'm seeing Warning: mysql_real_escape_string(): Access denied for user ''@'localhost' (using password: NO) in /home/intertid/public_html/create_admin.php on line 28 – acc1999 Jun 05 '18 at 03:43
  • Make sure your connection details / credentials are valid. – Script47 Jun 05 '18 at 03:44
  • They are. It's telling me the problem is on $username = mysql_real_escape_string($username); which doesn't make sense to me since I thought that was just performing some function on a variable and not dealing with the connection – acc1999 Jun 05 '18 at 03:45
  • Do you have a connection variable called `$username` by any chance? – Script47 Jun 05 '18 at 03:46
  • I do not. $username just stores the username entered through the form through POST. – acc1999 Jun 05 '18 at 03:49
  • wow! I didn't even notice that. I just fixed it and uploaded the new file to the server and I'm seeing the same error after I try to fill out the form. It's still complaining about line 28 – acc1999 Jun 05 '18 at 03:57
  • What is line 28 now? – Script47 Jun 05 '18 at 03:59
  • $username = mysql_real_escape_string($username); the next line is the same, but for password – acc1999 Jun 05 '18 at 04:05
  • That error is showing because it cannot find the connection, please see my update. – Script47 Jun 05 '18 at 04:08
  • I just noticed that a few minutes ago while reviewing my code, I created variables but never tried connecting to the database. I added this code after the $username and $password lines we discussed in create_admin.php include "db_connection.php"; $sqlQuery = "INSERT INTO table_test (username, password) VALUES ('$username', '$password')"; $dbcon->query($sqlQuery); $dbcon->close(); the db_connection file declares $dbcon with mysqli_connect() and passes in my credentials for the database. I use that file with my main login as well, it works fine there – acc1999 Jun 05 '18 at 04:16
  • '*$dbcon with mysqli_connect()*' - You are either using `mysql_*` or `mysqli_*`, which one is it? – Script47 Jun 05 '18 at 04:18
  • It appears one was using mysqli and the other mysql, that's the result of 2 different tutorials. You said mysql was deprecated, right? This is my first time with anything SQL. I updated all code files to use mysqli and now I get no errors when I submit the form, but no new row is created in the database. – acc1999 Jun 05 '18 at 04:24
  • Use `mysqli_error` to debug your `mysqli` code. Secondly when using `mysqli` AFAIK, you must use the link in the functions if you are using procedural code. – Script47 Jun 05 '18 at 04:26
  • I just updated that to mysqli_real_escape_string($dbcon, $username); and still no luck. I will debug and read up on some of this stuff so I'm up-to-speed with what I'm coding. Thanks for your help – acc1999 Jun 05 '18 at 04:29