mardi 26 février 2019

PHP user register page will not add values to MySQL table

I have a login and register page on my website along with a mysql table called 'user' which stores the username, password, and email. Here is the php included in my register page:

<?php
        if(isset($_POST['submit_btn']))
        {

            $username = $_POST['username'];
            $password = $_POST['password'];
            $cpassword = $_POST['password'];
            $email = $_POST['email'];

            if($password==$cpassword)
            {
                $query= "select * from user WHERE username='$username'";
                $query_run= mysqli_query($con,$query) or die(mysql_error());

                if(mysqli_num_rows($query_run)>0)
                {
                        // there is already a user with the same username
                        echo '<script type="text/javascript"> alert("User already exists, please try a different username") </script>';
                }
                else
                {
                    $query= "insert into user values('$username','$password','$email')";
                    $query_run = mysqli_query($con,$query) or die(mysql_error());

                    if($query_run)
                    {
                            echo '<script type=text/javascript"> alert("User Registered, return to login page to login") </script>';
                    }
                    else
                    {
                        echo '<script type="text/javascript"> alert("Error!") </script>';
                    }
                }

            }
            else{
            echo '<script type="text/javascript"> alert("Passwords do not match") </script>';   
            }
        }
    ?>

Pressing the sign up button will redirect me to the login page but doesn't add the fields to the database. As you can see, I've inserted (mysql_error()); at any point things could go wrong, yet a never receive an error code. Strangely enough, if a manually add a user to my database and then try to create a new user through the register page using the same credentials, I will trigger the error saying that user has already been registered. This obviously means I have some kind of connectivity with the DB, I'm just not able to add to it. I have already checked the MySQL user privileges and the user should be allowed to write to the database. I'm assuming I have a problem with my php, but I suppose it could potentially be a MySQL issue as well. If anyone could assist me I would greatly appreciate it.

Edit: Here's my statement linking the page to my config.php file:

<?php
    session_start();
    require_once('dbconfig/config.php');
?>




Aucun commentaire:

Enregistrer un commentaire