Click here to Skip to main content
14,691,047 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The error is located here:

else {
            $_SESSION['status'] = "New Admin entry NOT successful or cancelled";
            header ('Location: register.php');
        }


This is the full code I have.

<?php
session_start();

$connection=mysqli_connect("locahost","root","","adminpanel");
if(isset($_POST['registerbtn']))
{
    $fullname=$_POST['fullname'];
    $office=$_POST['office'];
    $school=$_POST['school'];
    $email=$_POST['email'];
    $username=$_POST['username'];
    $password=$_POST['password'];
    $cpassword=$_POST['confirmpassword'];

    if($password === $cpassword) {
        $query = "INSERT INTO register (fullname,office,school,email,username,password) VALUES ('$fullname','$office','$school','$email','$username,'$password')";
        $query_run = mysqli_query($connection, $query);
    
        if($query_run); {
            $_SESSION['success'] = "New Site Admin added";
            header ('Location: register.php');
        }
        else {
            $_SESSION['status'] = "New Admin entry NOT successful or cancelled";
            header ('Location: register.php');
        }
    }
    else {
        $_SESSION['status'] = "Password and Confirm Password Do Not Match";
        header ('Location: register.php');
        }
}

?>


What I have tried:

I have tried to figure out the correct pairing of the opening and closing of every code but I guess everything is okay.
Posted
Updated 8-Aug-20 21:48pm
v2

The problem you have noticed is because of this:
if($query_run); {
Remove the semicolon, and it should go away.

BUT ... that is a trivial problem compared the one tones you haven't noticed yet.

1) Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
DROP TABLE MyTable;
A perfectly valid "delete the table" command
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

2) Never store passwords in clear text - it is a major security risk. You need to hash the salted password and store the hash value instead of storing the password itself. When you come to check the password next time, you salt the user input, then hash that and compare the two hashes. If they differ, it's the wrong password.

And remember: if this is web based and you have any European Union users then GDPR applies and that means you need to handle passwords as sensitive data and store them in a safe and secure manner. Text is neither of those and the fines can be .... um ... outstanding. In December 2018 a German company received a relatively low fine of €20,000 for just that.

Fix both of those through your whole app as a serious priority!
   
Comments
JeffLiteral 9-Aug-20 2:55am
   
Thank you so much for a very vital solution and information you have shared. Thank you a lot!
OriginalGriff 9-Aug-20 2:56am
   
You're welcome!
May be because of the semicolon there:
if($query_run); {


$query = "INSERT INTO register (fullname,office,school,email,username,password) VALUES ('$fullname','$office','$school','$email','$username,'$password')";


Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
   
v2
Comments
JeffLiteral 9-Aug-20 2:55am
   
Thank you so much for a very vital solution and information you have shared. Thank you a lot!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900