-1

I have two files

  1. functions.php
<?php

include 'config.php';

function signup(){

    if (isset($_POST['submit'])) {

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

        if($password == $cpassword) {

            $hash = md5($password);

            $insert = "INSERT INTO `users`(`user_name`, `email`, `password`) VALUES ('$uname','$email','$hash')";

            $result = mysqli_query($con, $insert);

            if ($result) {
                echo '<script>alert("Your account has been successfully created.")</script>';
            }
        }
        else {
            echo '<script>alert("Passwords do not match!")</script>';
        }
    }
}
?>
  1. signup.php
<?php
    include 'functions.php';
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <!-- ===== Iconscout CSS ===== -->
    <link rel="stylesheet" href="https://unicons.iconscout.com/release/v4.0.0/css/line.css">

    <!-- ===== CSS ===== -->
    <link rel="stylesheet" href="css/credential.css">

    <title>Sing Up</title>
</head>
<body>
    <div class="container">
        <div class="forms">
            <div class="form signup">
                <span class="title">Sign Up</span>

                <form method="POST" action="functions.php">
                    <div class="input-field">
                        <input type="text" name="uname" placeholder="Enter your full name" required>
                        <i class="uil uil-user"></i>
                    </div>
                    <div class="input-field">
                        <input type="email" name="email" placeholder="Enter your email" required>
                        <i class="uil uil-envelope icon"></i>
                    </div>
                    <div class="input-field">
                        <input type="password" class="password" name="password" placeholder="Create a password" required>
                        <i class="uil uil-lock icon"></i>
                    </div>
                    <div class="input-field">
                        <input type="password" class="password" name="cpassword" placeholder="Confirm a password" required>
                        <i class="uil uil-lock icon"></i>
                        <i class="uil uil-eye-slash showHidePw"></i>
                    </div>

                    <div class="checkbox-text">
                        <div class="checkbox-content">
                            <input type="checkbox" id="termCon">
                            <label for="termCon" class="text">I accepted all <a href="#">Terms and Conditions</a>, <a href="#">Privacy Policy</a> and <a href="#">Cookie Policy</a></label>
                        </div>
                    </div>

                    <div class="input-field button">
                        <input type="submit" value="Sign Up" name="submit">
                    </div>
                </form>

                <div class="login-signup">
                    <span class="text">Already have an account?
                        <a href="#" class="text login-link">Login Now</a>
                    </span>
                </div>
            </div>
            <div class="form login">
                <span class="title">Login</span>

                <form action="#">
                    <div class="input-field">
                        <input type="email" placeholder="Enter your email" required>
                        <i class="uil uil-envelope icon"></i>
                    </div>
                    <div class="input-field">
                        <input type="password" class="password" placeholder="Enter your password" required>
                        <i class="uil uil-lock icon"></i>
                        <i class="uil uil-eye-slash showHidePw"></i>
                    </div>

                    <div class="checkbox-text">
                        <div class="checkbox-content">
                            <input type="checkbox" id="logCheck">
                            <label for="logCheck" class="text">Remember me</label>
                        </div>

                        <a href="#" class="text">Forgot password?</a>
                    </div>

                    <div class="input-field button">
                        <input type="submit" value="Login" name="submit">
                    </div>
                </form>

                <div class="login-signup">
                    <span class="text">Don't have an account?
                        <a href="#" class="text signup-link">Signup Now</a>
                    </span>
                </div>
            </div>

        </div>
    </div>

    <script src="js/credential.js"></script>

</body>
</html>

I want something like this... when I click on <input type="submit" of signup the signup() function from functions.php should work. But I don't know how to do it.

If I remove function signup(){} from functions.php and try without function then in url signup.php is replaced by functions.php and page is blank and no data is inserted in mysql localhost.

In 'config.php' file

<?php
    $con = mysqli_connect("localhost","root","","get-viewed");
?>

Database name, Table name and field name are perfect I have double checked it.

Vora Arshit
  • 40
  • 1
  • 8
  • 1
    **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/32391315) – Dharman Sep 09 '22 at 10:00
  • 1
    **Never store passwords in clear text or using MD5/SHA1!** Only store password hashes created using PHP's [`password_hash()`](https://php.net/manual/en/function.password-hash.php), which you can then verify using [`password_verify()`](https://php.net/manual/en/function.password-verify.php). Take a look at this post: [How to use password_hash](https://stackoverflow.com/q/30279321/1839439) and learn more about [bcrypt & password hashing in PHP](https://stackoverflow.com/a/6337021/1839439) – Dharman Sep 09 '22 at 10:01
  • Never configure your web app to login to the database as `root`. Root can do whatever it likes, so on top of the SQL injection vulnerabilities this just leaves your database an open book for hackers. Instead create a separate user account specifically for this application which has only the permissions it actually _needs_ in order to work properly. Don't even use the root account as a shortcut during development or testing, because you need to test your account permissions as well - otherwise when you go live you might have unexpected errors relating to the user account setup. – ADyson Sep 09 '22 at 10:13
  • `page is blank and no data is inserted in mysql localhost`...that's likely to be because your form sends a GET request, but your code is checking for POST data. Put `method="POST"` in your form's attributes and try again. And also, learn to debug, because if you'd actually examined your data variables and the path your code was taking, you might have noticed this already. https://www.atatus.com/blog/debugging-in-php/ has a simple guide to debugging with PHP. – ADyson Sep 09 '22 at 10:15

2 Answers2

1

The form action correctly point to function.php and the webserver execute it.

The result is blank because nothing in function.php get executed.

you defined function signup() but you don't call it

add signup(); as last code line, just before php closing tag ?>

Note 1: you can extract the code from the signup function, since it does not add any advantage.

Note 2: if the php closing tag is the last code line in the file (no html follow) you should omit, it is a good practice to avoid unwanted output.

This is a must once you start to use frameworks, otherwise header errors will popup

Roberto Braga
  • 569
  • 3
  • 8
  • thanks bro it worked. But how can I run signup() function from functions.php to signup.php while I click Sign up button? – Vora Arshit Sep 09 '22 at 10:19
0

Thanks for helping me I have solved my question. I updated functions.php

<?php

include 'config.php';

function signup() {

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

        if($password == $cpassword) {

            $hash = password_hash($password, PASSWORD_DEFAULT);

            $insert = "INSERT INTO `users`(`user_name`, `email`, `password`) VALUES ('$uname','$email','$hash')";

            $result = mysqli_query($con, $insert);

            if ($result) {
                echo '<script>alert("Your account has been successfully created.")</script>';
            }
        }
        else {
            echo '<script>alert("Passwords do not match!");location.replace("signup.php");</script>';
        }
    }

function login(){

    if (isset($_POST['login'])) {
        echo '<script>alert("login")</script>';
    }
}

if (isset($_POST['signup'])) {
    signup();
}
else {
    login();
}

Now it is working perfectly as I wanted.

Vora Arshit
  • 40
  • 1
  • 8