Page 349 - Computer Software Application TP - Volume 1
P. 349
COMPUTER SOFTWARE APPLICATION - CITS
<body>
<h2>Insert Data into MySQL Database</h2>
<form method=”post” action=”insert.php”>
<label for=”username”>Username:</label><br>
<input type=”text” id=”username” name=”username” required><br><br>
<label for=”email”>Email:</label><br>
<input type=”email” id=”email” name=”email” required><br><br>
<input type=”submit” value=”Submit”>
</form>
</body>
</html>
3 Save the program in C:\Apache24\htdocs in a folder as index.html
ii Create a PHP script (insert.php) to connect to the database and handle the insertion process.
1 Open the text editor
2 Write the following codes
<html >
<head>
<title> </title>
</head>
<body>
<?php
// Database connection parameters
$servername = “localhost”; // Change this if the MySQL server is on a different host
$username = “your_username”; // Change this to MySQL username
$password = “your_password”; // Change this to MySQL password
$database = “example_db”; // Change this to database name
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error)
{
die(“Connection failed: “ . $conn->connect_error);
}
// Check if the form is submitted
if ($_SERVER[“REQUEST_METHOD”] == “POST”)
{
// Get form data
username = $_POST[“username”];
334
CITS : IT & ITES - Computer Software Application - Exercise 61