PHP MySQL Insert Data is a process of adding new records or data into a MySQL database using PHP programming language. It is one of the most common and important operations in web development as it allows users to store and retrieve data from a database.
Inserting data into a MySQL database using PHP is a simple process that involves connecting to the database, creating a SQL query, and executing the query. The data to be inserted is usually obtained from a form or user input.
Before inserting data into a MySQL database using PHP, you need to establish a connection to the database. This can be done using the mysqli_connect() function in PHP. The function takes four parameters: the hostname, username, password, and database name.
// Database configuration
$host = "localhost";
$username = "root";
$password = "";
$database = "mydatabase";
// Create connection
$conn = mysqli_connect($host, $username, $password, $database);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
Once the connection to the MySQL database is established, you can insert data into the database using the mysqli_query() function in PHP. The function takes two parameters: the connection object and the SQL query.
// Insert data into table
$sql = "INSERT INTO users (name, email, phone) VALUES ('John Doe', 'johndoe@example.com', '1234567890')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "
" . mysqli_error($conn);
}
The above code inserts a new record into the "users" table with the name, email, and phone number of the user. If the query is executed successfully, the message "New record created successfully" is displayed. Otherwise, an error message is displayed.
Inserting data into a MySQL database using PHP is often done through a form. The form collects data from the user and sends it to the PHP script for processing. The data is then inserted into the database using the mysqli_query() function.
<form method="post" action="insert.php">
<label for="name">Name:</label>
<input type="text" name="name" id="name"><br>
<label for="email">Email:</label>
<input type="email" name="email" id="email"><br>
<label for="phone">Phone:</label>
<input type="text" name="phone" id="phone"><br>
<input type="submit" value="Submit">
</form>
The above code creates a simple form with fields for name, email, and phone number. The form sends the data to the "insert.php" script for processing.
// Get form data
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
// Insert data into table
$sql = "INSERT INTO users (name, email, phone) VALUES ('$name', '$email', '$phone')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "
" . mysqli_error($conn);
}
The above code retrieves the data from the form using the $_POST superglobal variable and inserts it into the "users" table using the mysqli_query() function.
PHP MySQL Insert Data is a simple and important process in web development. It allows users to store and retrieve data from a MySQL database using PHP programming language. By following the above examples, you can easily insert data into a MySQL database using PHP.