-2

my html + php code:

<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">

    <title>Hello, world!</title>
  </head>
  <body>
  <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
        <div class="container-fluid">
            <a class="navbar-brand" href="#">Submit</a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarSupportedContent">
            <ul class="navbar-nav me-auto mb-2 mb-lg-0">
                <li class="nav-item">
                <a class="nav-link active" aria-current="page" href="/phpTutorial/submitForm.php">Home</a>
                </li>
                <li class="nav-item">
                <a class="nav-link" href="#">Link</a>
                </li>
                <li class="nav-item dropdown">
                <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
                    Dropdown
                </a>
                <ul class="dropdown-menu" aria-labelledby="navbarDropdown">
                    <li><a class="dropdown-item" href="#">Action</a></li>
                    <li><a class="dropdown-item" href="#">Another action</a></li>
                    <li><hr class="dropdown-divider"></li>
                    <li><a class="dropdown-item" href="#">Something else here</a></li>
                </ul>
                </li>
                <li class="nav-item">
                <a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
                </li>
            </ul>
            <form class="d-flex">
                <input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
                <button class="btn btn-outline-success" type="submit">Search</button>
            </form>
            </div>
        </div>
    </nav>

    <?php
    
    if($_SERVER['REQUEST_METHOD']=='POST'){
        $name = $_POST['name'];
        $email = $_POST['email'];
        $pass = $_POST['pass'];
        $description = $_POST['description'];

        // creating connection
        $servername = "localhost";
        $username = "root";
        $password = "";
        $database = "contact";

        // connecting to database
        $conn = mysqli_connect($servername , $username , $password , $database);

        if(!$conn){
            die("Connection was not successfull due to:- " . mysqli_connect_error());
        }else{
            // connection was successfull and we need not to show this
            // submitting to the database
            $sql  = "INSERT INTO `contactus` ( `name`, `email`, `password`, `description`) VALUES ( '$name', '$email', '$pass', '$description')";

            $result = mysqli_query($conn , $sql);
            if($result){
                echo '<div class="alert alert-success alert-dismissible fade show" role="alert">
                        <strong>Success!</strong> Your entry has been submitted successfully!
                        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                            <span aria-hidden="true">×</span>
                        </button>
                    </div>';
            }else{
                echo '<div class="alert alert-danger alert-dismissible fade show" role="alert">
                        <strong>ERROR!</strong> Your entry was not  submitted due to some technical issue!
                        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                            <span aria-hidden="true">×</span>
                        </button>
                    </div>';
            }
        }
    }

    ?>


    <div class="container">
        <h3 class="mt-3">contactus for your concern</h3>
        <form method="POST" action="/phpTutorial/submitForm.php">
            <div class="mb-3">
                <label for="exampleInputEmail1" class="form-label">Full name</label>
                <input type="text" class="form-control" id="name" name="name" aria-describedby="emailHelp">
            </div>
            <div class="mb-3">
                <label for="exampleInputEmail1" class="form-label">Email address</label>
                <input type="email" class="form-control" id="email" name="email" aria-describedby="emailHelp">
                <div id="emailHelp" class="form-text">We'll never share your email with anyone else.</div>
            </div>
            <div class="mb-3">
                <label for="exampleInputPassword1" class="form-label">Password</label>
                <input type="password" name="pass" class="form-control" id="">
            </div>
            <div class="mb-3">
                <label for="desc" class="form-label">Enter your Concern</label>
                <div>
                    <textarea name="description" id="desc" cols="80" rows="8"></textarea>
                </div>
            </div>
            
            <button type="submit" class="btn btn-primary">Submit</button>
        </form>
    </div>

    <!-- Optional JavaScript; choose one of the two! -->

    <!-- Option 1: Bootstrap Bundle with Popper -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>

    <!-- Option 2: Separate Popper and Bootstrap JS -->
    <!--
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.min.js" integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF" crossorigin="anonymous"></script>
    -->
  </body>
</html>

I am trying a simple form submission through php in backend and i have installed xampp in which mysql is working as database. my database name is "contact" and the table i am using is "contactUs". i am trying to save the filled data of form in this table. all things are working good but when i am trying to enter 10 or more than 10 integers in password section it is not showing that password. instead it is showing "2147483647" as the password in the password column in database. i am not entering this but instead it is showing this. i don't know why it is happening my length of password column is 20 in database,

please explain the mistake in a beginner friendly way

James Z
  • 12,209
  • 10
  • 24
  • 44
  • 2
    **Warning!** Never store passwords in plain text! Only store password hashes generated with [password_hash() and use password_verify() to validate hashes against passwords](https://stackoverflow.com/questions/30279321/how-to-use-phps-password-hash-to-hash-and-verify-passwords). The password column in the database is also recommended to be 255 in length when using password_hash(). – M. Eriksson Jun 30 '21 at 06:01
  • 1
    **Warning!** You are currently _wide open_ for SQL injection attacks! Make sure that you use prepared statements with bound parameters instead of injecting user data directly into your queries like that! [Read how here](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). It's not just about safety, but if any of the fields contains certain characters, like a single quote `'`, your query will break. – M. Eriksson Jun 30 '21 at 06:05
  • 1
    Well, your code is really vulnerable for SQL Injection. Try to use Prepared Statement and also sanitize your inputs before processing it into the database...regarding the password thingy, its better to use 255 as length in database and you can limit the number of characters for password using regex checks. ALSO do not use password as plain text, always sanitize it first and then hash it using hashing algorithms https://www.php.net/manual/en/faq.passwords.php https://alexwebdevelop.com/php-password-hashing/ – vins Jun 30 '21 at 06:40

1 Answers1

0

this is a common beginner error many programmers encounter...the solution to this is simple, the "2147483647" value is a dumped value automatically generated.

Solutions:

  1. If you're using int(20), use bigint(20) instead.

    Query: ALTER TABLE contactUs MODIFY password BIGINT(20);

  2. You can also switch to varchar(100) if you want.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Harsh
  • 100
  • 1
  • 10
  • Using `bigint` is not a good idea since passwords should be hashed using password_hash() (which generates alphanumeric hashes) – M. Eriksson Jun 30 '21 at 07:53