How to send data to my database from html and css Contact Us Form

0 votes

Aug 4, 2020 in Database by Sign
• 120 points
17,548 views

2 answers to this question.

0 votes

Hi, @Sign

You need to follow few steps regarding your query:

1. Firstly, install a virtual server on your computer (eg Xampp, Wamp).

  • Xampp is a free and open-source cross-platform web server solution stack package developed by Apache Friends, consisting mainly of the Apache HTTP Server, MySQL database, and interpreters for scripts written in the PHP and Perl programming languages. 

2. Next, we will require an editor where the HTML code has to be written. You can use any editor (such as Notepad++, Adobe Dreamweaver, NetBeans, etc). Here we will use Notepad ++.

3. Open the Notepad++ text editor and write the HTML code for designing the HTML Sign Up page. We can use various HTML tags to design the page. You can include the fields according to your convenience.

Have a view of the code written in notepad++,

<html>    

    <head>    

        <title>Registration Form</title>    

    </head>    

    <body>    

        <link href = "registration.css" type = "text/css" rel = "stylesheet" />    

        <h2>Sign Up</h2>    

        <form name = "form1" action="modified.php" method = "post" enctype = "multipart/form-data" >    

            <div class = "container">    

                <div class = "form_group">    

                    <label>First Name:</label>    

                    <input type = "text" name = "fname" value = "" required/>    

                </div>    

                <div class = "form_group">    

                    <label>Middle Name:</label>    

                    <input type = "text" name = "mname" value = "" required />    

                </div>    

                <div class = "form_group">    

                    <label>Last Name:</label>    

                    <input type = "text" name = "lname" value = "" required/>    

                </div>    

                <div class = "form_group">    

                    <label>Password:</label>    

                    <input type = "password" name = "pwd" value = "" required/>    

                </div>    

            </div>    

        </form>    

    </body>    

</html>    

You can refer to this https://www.c-sharpcorner.com/UploadFile/52bd60/create-an-html-form-and-insert-data-into-database162/ for more information.

answered Aug 4, 2020 by Rashmi

edited Aug 4, 2020
0 votes

Hello @Sign,

It is simple to create contact form to send email or any other information  and storing in mysql database.

Using INSERT INTO statement you can insert new data into a database table.

See the example below:

Output:

Step 1: Connection with Database

This is dbConn.php file which is used to connect the database.

This dbConn.php file will use everywhere, where you want to perform a task with the database.

Here it is connected with the local database and check database connected or not.

dbConn.php

<?php

$db = mysqli_connect("localhost","root","","testDB");

if(!$db)
{
    die("Connection failed: " . mysqli_connect_error());
}

?>

Step 2: Creating HTML form and inserting data

Here's a simple HTML form which is index.php file. It has two text input and a submit button.

The INSERT INTO statement is used to add new data into the database. This insert query will execute passing through to the PHP mysqli_query() function to insert new data.

And also created a simple HTML form which has two input text and a submit button which is used to take data from users.

In the index.php file the dbConn.php file includes for connecting with the database and insert new data. The form has two field fullname and age which are get data from users and store into the database.

In this example the PHP code and the HTML form coded in the single page. The PHP script embedded with the HTML page.

index.php

<!DOCTYPE html>
<html>
<head>
  <title>Add Records in Database</title>
</head>
<body>

<?php
include "dbConn.php"; // Using database connection file here

if(isset($_POST['submit']))
{		
    $fullname = $_POST['fullname'];
    $age = $_POST['age'];

    $insert = mysqli_query($db,"INSERT INTO `tblemp`(`fullname`, `age`) VALUES ('$fullname','$age')");

    if(!$insert)
    {
        echo mysqli_error();
    }
    else
    {
        echo "Records added successfully.";
    }
}

mysqli_close($db); // Close connection
?>

<h3>Fill the Form</h3>

<form method="POST">
  Full Name : <input type="text" name="fullname" placeholder="Enter Full Name" Required>
  <br/>
  Age : <input type="text" name="age" placeholder="Enter Age" Required>
  <br/>
  <input type="submit" name="submit" value="Submit">
</form>

</body>
</html>

Write PHP code as a separate file

Step 1: Creating HTML Form

First of all creating HTML form which is index.php file. This form has two text boxes and a submit button for inserting data.

This form also has an action attribute which contains insert.php. This will perform when clicking on submit button.

index.php

<!DOCTYPE html>
<html>
<head>
  <title>Add Records in Database</title>
</head>
<body>

<form action="insert.php" method="POST">
  Full Name : <input type="text" name="fullname" placeholder="Enter Full Name" Required>
  <br/>
  Age : <input type="text" name="age" placeholder="Enter Age" Required>
  <br/>
  <input type="submit" name="submit" value="Submit">
</form>

</body>
</html>

Step 2: Connection with Database

This is dbConn.php file which connects the database.

dbConn.php

<?php

$db = mysqli_connect("localhost","root","","testDB");

if(!$db)
{
    die("Connection failed: " . mysqli_connect_error());
}

?>

Step 3: Write PHP code for insert data

This is insert.php file. Here is written PHP code for inserting data when clicking on submit button.

insert.php

<?php
include "dbConn.php"; // Using database connection file here

if(isset($_POST['submit']))
{		
    $fullname = $_POST['fullname'];
    $age = $_POST['age'];

    $insert = mysqli_query($db,"INSERT INTO `tblemp`(`fullname`, `age`) VALUES ('$fullname','$age')");

    if(!$insert)
    {
        echo mysqli_error();
    }
    else
    {
        echo "Records added successfully.";
    }
}

mysqli_close($db); // Close connection
?>

Hope it helps!!
Thank you!

answered Aug 4, 2020 by Niroj
• 82,720 points
I think I have a lot to learn from edureka mostly the PHP. I have no typical idea. Am only good at ASP.NET

Related Questions In Database

+2 votes
1 answer

How to export data from MySql to a CSV file?

If you are using MySql workbench then ...READ MORE

answered Jan 4, 2019 in Database by Priyaj
• 58,080 points
407 views
0 votes
1 answer

How will I get all the rows from my table where name starts with 's' and ending with 'I'?

Hi Santanu, I understand your problem, You can try ...READ MORE

answered May 28, 2019 in Database by sampriti
• 1,120 points
514 views
0 votes
1 answer

How to load data of .csv file in MySQL Database Table?

At first, put the dataset in the ...READ MORE

answered Jul 5, 2019 in Database by Reshma
708 views
0 votes
1 answer

I have to build a school website How can i connect databases to the instance (ec2) and what is the best AMI for my webserver INAWS ???

Hi@Trinu, I don't think your database is depends ...READ MORE

answered May 4, 2020 in Database by MD
• 95,360 points
288 views
0 votes
1 answer

How to drop all tables from a database with one SQL query?

Use the INFORMATION_SCHEMA.TABLES view to get the ...READ MORE

answered Feb 4 in Database by Neha
• 8,560 points
117 views
0 votes
0 answers

How to change column datatype in SQL database without losing data?

I have a SQL Server database, and ...READ MORE

Feb 25 in Database by Vaani
• 7,020 points
52 views
0 votes
2 answers

Define a SQL query? What is the difference between SELECT and UPDATE Query? How do you use SQL in SAS?

HI.. SQL is Structured Query Language, which is ...READ MORE

answered Aug 8, 2020 in PHP by anonymous
4,495 views
0 votes
1 answer
0 votes
1 answer

How to access PHP var from external javascript file?

Hello @kartik, You don't really access it, you ...READ MORE

answered Jul 6, 2020 in Java-Script by Niroj
• 82,720 points
4,351 views
0 votes
1 answer

How do I escape a single quote in SQL Server?

Hello @kartik, Single quotes are escaped by doubling ...READ MORE

answered Jul 21, 2020 in PHP by Niroj
• 82,720 points
4,261 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP