Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello, please look at my code:

<?php include "db.php" ?>
<?php 

    if(isset($_POST['create'])){
        $username = $_POST['username'];
        $email = $_POST['email'];
        $password = $_POST['password'];
    }

    $query = "INSERT INTO customers(username, email, password) VALUES('$username', '$email', '$password')";

    $result = mysqli_query($connection, $query);
    
    if($result){
        echo "User created for ". $username . "Successfully";
    } else {
        echo "Sorry, we could not register you. Please try again.";
    }


?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Create Users for Customrs</title>
    <link href="css/bootstrap.css" rel="stylesheet" type="text/css">
</head>
<body>
   <div class="container">
      <h1 class="text-center text-info">Create Users for Customers</h1>
       <form action="create.php" method="post">
           <div class="form-group">
               <input type="text" name="username" placeholder="Username" class="form-control">
           </div>
           <div class="form-group">
               <input type="text" name="email" placeholder="Email" class="form-control">
           </div>
           <div class="form-group">
               <input type="password" name="password" placeholder="Password" class="form-control">
           </div>
           <div class="form-group">
               <input type="submit" name="create" value="Create User" class="btn btn-success form-control">
           </div>
       </form>
       
   </div>
    
</body>
</html>




--------

I get an error as:
Notice: Undefined variable: username in /Applications/XAMPP/xamppfiles/htdocs/login_application/create.php on line 10

Notice: Undefined variable: email in /Applications/XAMPP/xamppfiles/htdocs/login_application/create.php on line 10

Notice: Undefined variable: password in /Applications/XAMPP/xamppfiles/htdocs/login_application/create.php on line 10


What I have tried:

I have check the value of $query and confirmed the variables to echo it, i was able to echo the variables but when inserting it to database i get that error.
Posted
Updated 16-Aug-19 3:56am
Comments
Saleh Mohammad Aria 16-Aug-19 7:44am    
I can insert data to database, but by only loading this page first time, i see this error. Otherwise if i enter the data into fields it will work fine.

I am worried about this error and couldnt find the reason

Quote:
Undefined variable: username

have you noticed that the variables are created only if $_POST['create'] exist, it is non sense to used them if you are not sure they exist.
PHP
if(isset($_POST['create'])){
    $username = $_POST['username'];
    $email = $_POST['email'];
    $password = $_POST['password'];
}


PHP
$query = "INSERT INTO customers(username, email, password) VALUES('$username', '$email', '$password')";

Not a solution to your question, but another problem you have.
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[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
-----
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

phpdbg | php debugger[^]
Debugging techniques for PHP programmers[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
v2
Comments
raddevus 16-Aug-19 9:42am    
Very good points. Was attempting to provide basic "test" solution with string concatenation.
Saleh Mohammad Aria 16-Aug-19 9:48am    
Yes it exists, even i can echo the username entered by user, but i still have the error of undefined variable for $username and the rest of the fields.

if(isset($_POST["create"])){
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];

}
Hey guys!
I found the solution for this, hope it helps anyone with this kinda error in future.

In situations like this that we get undefined variable error when everything is correct we have to initialize the variable to get rid of the error.

<?php
// Initialize variables to null.

$username = '';
$email = '';
$password = '';

if(isset($_POST["create"])){

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

   }



Now it works fine!
 
Share this answer
 
Comments
Richard Deeming 16-Aug-19 14:19pm    
Except you're still vulnerable to SQL Injection[^], so be prepared to have your database stolen, vandalized, or deleted.

You're also storing passwords in plain text, which is another serious vulnerability in your code. Use the functions provided by PHP to store them properly:
PHP: password_hash[^]
PHP: password_verify[^]

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