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