0% found this document useful (0 votes)
3 views5 pages

Database Connection

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Database Connection

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Got it 👍.

Since you’ve been working with HTML forms + PHP + MySQL (WAMP/XAMPP) in your previous
requests, I’ll give you a simple form + database connection example.

---

1. Database Connection File (db_connect.php)

<?php

$host = "localhost"; // Server name

$user = "root"; // Default username in WAMP/XAMPP

$pass = ""; // Default password is empty in WAMP/XAMPP

$db = "school_db"; // Your database name

// Create connection

$conn = new mysqli($host, $user, $pass, $db);

// Check connection

if ($conn->connect_error) {

die("Connection failed: " . $conn->connect_error);

?>

---
2. Example HTML Form (register.html)

<!DOCTYPE html>

<html>

<head>

<title>Register Student</title>

</head>

<body>

<h2>Register Student</h2>

<form action="register.php" method="POST">

<label>Full Name:</label>

<input type="text" name="fullname" required><br><br>

<label>Email:</label>

<input type="email" name="email" required><br><br>

<label>Class:</label>

<input type="text" name="class" required><br><br>

<input type="submit" value="Register">

</form>

</body>

</html>
---

3. Insert Data into Database (register.php)

<?php

include("db_connect.php"); // include the connection file

if ($_SERVER["REQUEST_METHOD"] == "POST") {

$fullname = $_POST['fullname'];

$email = $_POST['email'];

$class = $_POST['class'];

$sql = "INSERT INTO students (fullname, email, class)

VALUES ('$fullname', '$email', '$class')";

if ($conn->query($sql) === TRUE) {

echo "New student registered successfully!";

} else {

echo "Error: " . $sql . "<br>" . $conn->error;

$conn->close();

?>
---

4. Example Database Table

Run this in phpMyAdmin / MySQL to create the students table:

CREATE DATABASE school_db;

USE school_db;

CREATE TABLE students (

id INT AUTO_INCREMENT PRIMARY KEY,

fullname VARCHAR(100),

email VARCHAR(100),

class VARCHAR(50)

);

---

✅ Now when you fill the form and click Register, the student’s details will be saved in your MySQL
database.

---
Do you want me to also add secure password hashing (for login forms), or just keep it simple like above?

You might also like