-1

I am facing simple error while making simple register to insert into mysql database with username and password.

Notice: Undefined variable: username in C:\xampp\htdocs\x\index.php on line 42

Notice: Undefined variable: password in C:\xampp\htdocs\x\index.php on line 42 Registerd Successfully

<!DOCTYPE html>
<html>
 <head>
  <title>THIS IS MY FIRST LOGIN PAGE</title>
  <link rel="stylesheet" type="text/css" href="style.css"></link>
  <link rel="stylesheet" type="text/css" href="">
 </head>

 <body>
  <div class="container">
   <img src="men.png" alt="missing">
   <form method="post" >
    <div class="font-input">
     <input type="text" name="name" id="name"  placeholder="Enter Name">
    </div class="font-input">
    <div>
     <input type="password" name="pass" id="pass"  placeholder="Enter Password">
    </div>
    <div>
     <input type="button" name="login" value="Login">
    </div>
   </form>
  </div>
 </body>
</html>
  <?php

$conn = mysqli_connect('localhost','root','','y');

if(!$conn)
{
    die('Connection failed!'.mysqli_error($conn));
}

if (isset($_POST['name'], $_POST['pass']))
{
        $username = $_POST['name'];
        $password = $_POST['pass'];
} 
 $sql = "INSERT INTO hack(myusername,mypassword) VALUES('$username', '$password')";



if(mysqli_query($conn,$sql))
{
    echo "Registerd Successfully";
}
else
{
    echo mysqli_error($conn);
}

?>

1 Answers1

0

Try this : We check if username and password are sent with the form, if so, we insert them into our database if there's no error.
Code :

if((isset($_POST['name'])) && (isset($_POST['pass'])))
{
    $username = $_POST['name'];
    $password = $_POST['pass'];
    $sql = "INSERT INTO hack(myusername, mypassword) VALUES('$username', '$password')";
    if(mysqli_query($conn,$sql))
    {
    echo "Registerd Successfully";
    }
    else
    {
    echo mysqli_error($conn);
    }
} 
AraByte
  • 154
  • 9