0

I'm trying to make a registration form. This form needs to add data to the database when the submit is clicked. it's not giving any errors or messages. this is my code, I hope someone can help me.

<body>
 <form action="" method"post" class="form">
    <div class="form-group1">
    <label for="" >Username :</label>
    <input type="text" class="form-control" name="username" id="tb-username" placeholder="Username">
    </div>
    <div class="form-group2">
    <label for="" >Password :</label>
    <input type="password" class="form-control" name="password" id="tb-password" placeholder="Password">
    </div>
    <button type="submit" class="btn btn-primary" id="btn-submit" name="register">Submit</button>
    </form>

    <?php

    if(isset($_POST['register']))
    {
    if(isset($_POST['username'], $_POST['password']))
    {
        $username = $_POST['username'];
        $password = $_POST['password'];

        $link = mysqli_connect("localhost", "root", " ", "vbproject");

        if(mysqli_connect_errno())
        {
            printf("Connect failed: %s\n", mysqli_connect_errno());
            exit();
        }

        $sql = "INSERT INTO users (username, password)
                VALUES('$username', '$password')";

        mysqli_close($link);
    }
    }
     ?>

</body> 
Gufran Hasan
  • 8,910
  • 7
  • 38
  • 51
acesteef
  • 35
  • 1
  • 6
  • 4
    You do not prepare or execute your SQL. – Nigel Ren May 29 '18 at 06:51
  • 7
    Storing plain passwords, mmmmm – u_mulder May 29 '18 at 06:52
  • you are supposed to call the query in order to save the data into the database – Mahi Parmar May 29 '18 at 06:52
  • @MahiParmar im a beginner in php. what is the best way to call the query? – acesteef May 29 '18 at 06:55
  • i have already answered please check @acesteef – Mahi Parmar May 29 '18 at 06:56
  • @acesteef Just for security's sake. [Preparing SQL Statements 1](https://stackoverflow.com/questions/8263371/how-can-prepared-statements-protect-from-sql-injection-attacks) [Preparing SQL Statements 2](https://www.w3schools.com/php/php_mysql_prepared_statements.asp) [Hashing and verifying passwords](https://codeaddiction.net/articles/4/hash-and-verify-passwords-in-php---the-right-way) – Rick J May 29 '18 at 06:58
  • 1
    mysqli_query($con, $sql); // this Line Missing after Your SQL Query – Niraj patel May 29 '18 at 06:59
  • 2
    None of the answers (so far) address the point @u_mulder makes of storing plain passwords, which is so important these days for security. – Nigel Ren May 29 '18 at 07:01
  • @NigelRen I gave him some links in the comments.I don't think giving an answer just about the security issue's would be appropiate, since then you would go off topic.. – Rick J May 29 '18 at 07:08
  • 1
    @Rick_Jellema you don't need to give an answer which just addresses one point, but an answer should show the best way to solve a problem. – Nigel Ren May 29 '18 at 08:17
  • @NigelRen Gotta give a `true` to your comment. – Rick J May 29 '18 at 08:36

7 Answers7

2

You have an error here

<form action="" method"post" class="form">

replace this with

<form action="" method="post" class="form">

you missed the = sign causing your request as GET request and you are accessing as POST method

Ash-b
  • 705
  • 7
  • 11
0

Add this line before closing connection, it will execute your query which is what you are not doing

mysqli_query($link,$sql);
prit.patel
  • 330
  • 3
  • 12
0

you Need too:

 $sql = "INSERT INTO users (username, password)
        VALUES('$username', '$password')";
 $result = $link->query($sql);
 if($result === true){
  echo "Insert Success";
 }else{
  echo "Insert failed";
  }
0

Please change method"post" to method="post" in form tag.

 <body>

    <form action="" method="post" class="form">
    <div class="form-group1">
    <label for="" >Username :</label>
    <input type="text" class="form-control" name="username" id="tb-username" placeholder="Username">
    </div>
    <div class="form-group2">
    <label for="" >Password :</label>
    <input type="password" class="form-control" name="password" id="tb-password" placeholder="Password">
    </div>
    <button type="submit" class="btn btn-primary" id="btn-submit" name="register">Submit</button>
    </form>

    <?php

    if(isset($_POST['register']))
    {
    if(isset($_POST['username'], $_POST['password']))
    {
        $username = $_POST['username'];
        $password = $_POST['password'];

        $link = mysqli_connect("localhost", "root", " ", "vbproject");

        if(mysqli_connect_errno())
        {
            printf("Connect failed: %s\n", mysqli_connect_errno());
            exit();
        }

        $sql = "INSERT INTO users (username, password)
                VALUES('$username', '$password')";
        if(mysqli_query($link, $sql)){
         echo 'Inserted';
        }else{
          echo 'Not Inserted';
        }
        mysqli_close($link);
    }
    }
     ?>

    </body> 

Add these line in your code :

if(mysqli_query($link, $sql)){
         echo 'Inserted';
        }else{
          echo 'Not Inserted';
        }
Gufran Hasan
  • 8,910
  • 7
  • 38
  • 51
0

You did not run the mysql query. Please Find Below solution:

$sql = "INSERT INTO users (username, password)
            VALUES('$username', '$password')";

mysqli_query($con, $sql); // this Line Missing

mysqli_close($link);
Luca
  • 296
  • 2
  • 19
Niraj patel
  • 525
  • 4
  • 12
0

Just an observation your code

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

But $_POST['register'} has not been assigned any value and the if statement will fail suggest you add value="submit" here

<button type="submit" class="btn btn-primary" id="btn-submit" name="register">Submit</button>
-1

This is how the flow will go

<?php
$con=mysqli_connect("localhost", "root", " ", "vbproject");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

// Perform queries 
 $sql = "INSERT INTO users (username, password)
            VALUES('$username', '$password')";
mysqli_query($con, $sql);

mysqli_close($con);
?>
Mahi Parmar
  • 515
  • 3
  • 10
  • 31