Uncategorized

How to insert PHP form data into a MySQL database

In this blog post, you will learn how to insert PHP form data into a MySQL database. | In the world of web development, learning to handle form data and store it securely is a crucial first step. With PHP and MySQL, beginners can easily build basic web applications that collect user input and save it to a database.

 

How to insert PHP form data into a MySQL database

Setting Up:

Ensure that XAMPP is installed on your computer. This tool is essential for establishing a local server environment for testing purposes. If you have not installed it yet, please do so.

Understanding the Code:

The provided PHP script is designed to handle form submissions. It starts by checking if the form has been submitted ($_POST[‘submit’]). If so, it retrieves the values entered by the user for ‘name’ and ‘city’.

Database Connection:

Before executing any database operations, the script requires a database connection. This is achieved by including the “config.php” file, which presumably contains the necessary credentials for connecting to the MySQL database.

Inserting Data into the Database:

Once the form data is retrieved, the script constructs an SQL query to insert the data into a table named ‘users’. The query includes placeholders (‘$name’, ‘$city’) for the values obtained from the form.

 

Folder Structure

http://localhost/web/

    • config.php
    • index.php

 

1. Create Database name demo

After creating the database, locate the SQL Tab and execute the provided SQL query.

CREATE TABLE `users` (
  `id` int(11) NOT NULL,
  `name` text NOT NULL,
  `city` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

 

2. Create file config.php

Please create a file on your computer and paste all the provided code into it.

<?php
/* Database credentials. Assuming you are running MySQL
server with default setting (user 'root' with no password) */

define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_NAME', 'demo');
 
/* Attempt to connect to MySQL database */
$link = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
 
// Check connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}
?>

 

3. Create file index.php

Please create a file on your computer and paste all the provided code into it.

<?php
/* Database connection */
require "config.php";

if (isset($_POST['submit'])) {

        // Get form data
        $name = $_POST['name'];
        $city = $_POST['city'];
        
    if($name!="" && $city!=""){
        // SQL to insert data into table
        $query = "INSERT INTO users (name, city) VALUES ('$name', '$city')";
        $run = mysqli_query($link, $query);

                if ($run === TRUE) {
                    echo "Data Inserted";
                } else {
                    echo "Error: ";
                }
    }else{
        echo "All Fields Required!";
    }
}
?>


<!DOCTYPE html>
<html>
<head>
    <title>Data Insert</title>
</head>
<body>

<h2>PHP Form Data Insert</h2>

<form method="post" action="">
    Name: <input type="text" name="name"><br><br>
    City: <input type="text" name="city"><br><br>
    <input type="submit" name="submit" value="Submit">
</form>

</body>
</html>

 

Conclusion:

With this simple PHP script, beginners can grasp the fundamentals of collecting and storing user data in a MySQL database. By understanding each component of the code, you’re one step closer to building dynamic web applications that interact with databases. Keep practicing and exploring new possibilities in the vast world of web development! Happy coding!