All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

PHP Code For Inserting Data Into Database From Form

Last Updated : Mar 11, 2024

PHP Code For Inserting Data Into Database From Form

In this tutorial we will show you the solution of PHP code for inserting data into database from form, this type of concept is used in registration, login, in feedback forms and in surveys also.

Insertion of data into database is widely used concept now days. Now, let us understand how we can inserting data into database from form.

Step By Step Guide On PHP Code For Inserting Data Into Database From Form :-

Here, first we have to create a database and a php form also.

The fields which we are going to specified in form for database purposes must be available in database with same name because of case sensitiveness.

Now, let us see in brief this concept using forms.

// login.php
<?php
    $connect=mysqli_connect("localhost","root","","index") or die("Connectoin Failed");
    if(!empty($_REQUEST['button']))
    {
        $user=$_REQUEST['username'];
        $email=$_REQUEST['email'];
        $pass=$_REQUEST['password'];
        $cpass=$_REQUEST['cpassword'];
        $filename= $_FILES['file']['name'];
        $filepath=$_FILES['file'] ['tmp_name'];
        $imagename= explode(".",$filename);
        $ext=$imagename[1];
        $query = "show table status like 'rl_form'";
        $result = mysqli_query($connect,$query);
        $row=mysqli_fetch_assoc($result);
        $id=$row['Auto_increment'];
        $newfilename= $id.".".$ext;
        if($user=='' || $email== '' || $pass== '' || $cpass== '' || $filename == '')
        {
            echo "<script> alert('Please fill all fields.......')</script>";
        }
        else{
            $query="select * from rl_form where email= '$email'";
            $result=mysqli_query($connect,$query);
            $count = mysqli_num_rows($result);
            if($count>0)
            {
                echo "<script> alert(' Email id alreadty exist')</script>";
            }
            else
            {
                if($pass != $cpass)
                {
                    echo "<script> alert(' Please enter same password')</script>";
                }
                else
                {
                    $query=" insert into rl_form( user, email, pass, cpass, image) values('$user', '$email', '$pass' , '$cpass' , '$newfilename')";
                    if(mysqli_query($connect, $query))
                    {
                        move_uploaded_file($filepath,"uploadimages/".$newfilename);
                        echo " <script> alert('Record inserted successfully')
                        window.location.href= '../project/index.php';
                        </script>";
                    }
                    else{
                        echo " Error found ! come back after one year";
                    }
                }
            }
        }
    }
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>home page</title>
    <script src="js/jquery.js"></script>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="main">
        <div class="inner">
            <div class="welcome">
                Registration Form
            </div>
            <div class="registration_options">
                <form action="" method="post" enctype="multipart/form-data">
                    <div class="rleft_body">
                        <label for="">
                            Enter your name here
                        </label>
                        <br>
                        <label for="">
                            Enter your email here
                        </label>
                        <br>
                        <label for="">
                            Enter your password here
                        </label>
                        <br>
                        <label for="">
                            Enter password again here
                        </label>
                        <label for="">
                            Upload your image here
                        </label>
                    </div>
                    <div class="rright_body">
                        <input type="text" name="username" id="">
                        <br><br>
                        <input type="email" name="email" id="">
                        <br><br>
                        <input type="password" name="password" id="">
                        <br><br>
                        <input type="text" name="cpassword" id="">
                        <br><br>
                        <input type="file" name="file" id="" accept="images/*">
                    </div>
                    <div class="registration_submit">
                        <input type="submit" value="Save" id="save" name="button">
                    </div>
                </form>
            </div>
        </div>
    </div>
</body>
</html>
  1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As above now <head> tag is used to contain information about web page. In this tag a <title> tag is used which helps us to specify a webpage title. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  4. Here, then we create a body tag. All the content which we want to show on browser’s screen or display is always written inside this codes.
  5. Here, as you see that to show you how to insert data into database we use example of registration form.
  6. As you can see that we create some inputs fields here which send data to database on button click.
  7. The database used here is index and table name is rl_form. The database with table must be present in your database.
  8. Whereas required columns are user, email, pass, cpass, image.
  9. We use some validations here like form will not be submitted if any field is empty. Then we apply validations on email, passwords, etc. if all conditions are verified then data will be submitted to database.
  10. At last, the <body> and <html> tags are closed with </body> and </html> respectively.

Conclusion :-

At last in conclusion, here we can say that with the help of this article we are able to understand how to insert data into database from form using php.

I hope this tutorial on PHP code for inserting data into database from form helps you and the steps and method mentioned above are easy to follow and implement.