All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

PHP Insert Data Into MySQL From Form

Last Updated : Mar 11, 2024

PHP Insert Data Into MySQL From Form

In this article we will show you the solution of PHP insert data into MySQL from form, MySQL is a well-liked relational database management system that is open-source, and PHP is a well-known programming language for web development.

Inserting data from an HTML form into a MySQL database using PHP is a typical web development activity.

In this procedure, a user-input HTML form is created, and after that, PHP is used to retrieve the data from the form and enter it into the MySQL database.

So let's talk about the PHP insert data into MySQL from form notion.

Step By Step Guide On PHP Insert Data Into MySQL From Form :-

// Establish a connection to the MySQL database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database_name";
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
// Check if the form has been submitted
    // Retrieve the form data
    $name = $_POST["name"];
    $email = $_POST["email"];
    $message = $_POST["message"];
    // Prepare and execute the SQL statement to insert the data into the database
    $sql = "INSERT INTO messages (name, email, message) VALUES ('$name', '$email', '$message')";
    if (mysqli_query($conn, $sql)) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . mysqli_error($conn);
// Close the MySQL database connection
  1. As you can see, we wrote the PHP code to put data into MySQL using a form.
  2. Initially, we connect to the MySQL database by entering the server name, user name, password, & database name.
  3. To create the connection, we employ the mysqli connect() method.
  4. We determine whether the connection was effective.
  5. If not, we stop the script and display an error.
  6. The $_SERVER["REQUEST METHOD"] variable is used to determine if the form has been submitted.
  7. A form submission has occurred if it equals "POST."
  8. Using the $_POST variable, we get the form data and save it in variables.
  9. Assumed in this example, the form contains three input fields with the names "name," "email," and "message."
  10. To enter the form data into the database, we create a SQL statement.
  11. For the purposes of this example, let's assume that database table "messages" includes three columns: "name," "email," and "message."
  12. In order to execute the SQL command, we use the mysqli query() method.
  13. We determine whether the SQL command was successfully performed.
  14. We show a success message if it was.
  15. In that case, we show an error notice.
  16. Lastly, we use the mysqli close() method to terminate the connection to the MySQL database.

Conclusion :-

As a result, we have successfully learnt how to use PHP to insert data into a MySQL database from a form.

We have discovered that one frequent chore in web development is writing PHP code to insert data from a form into a MySQL database.

The code utilises the functions mysqli connect() and mysqli query() to connect to the MySQL database and enter the form data into the database using a SQL statement, respectively.

I hope this article on PHP insert data into MySQL from form helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪