Hi! Could we please enable some services and cookies to improve your experience and our website?
No, thanks.
Okay!
Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code
CREATE DATABASE university;
USE university;
CREATE TABLE students (
id INT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
email VARCHAR(100) NOT NULL
);
SQL Server:
MySQL 8.0
MySQL 8.0 Sakila (ReadOnly)
MySQL 9.3.0
MariaDB 11.4
MariaDB 11.8
MariaDB 10
MariaDB 10 Sakila (ReadOnly)
SQLite 3
SQLite 3 Preloaded
PostgreSQL 10 Bookings (ReadOnly)
PostgreSQL 13
PostgreSQL 14
PostgreSQL 15
PostgreSQL 16
PostgreSQL 17 + PostGIS
PostgreSQL 17 + PostGIS WorkShop (ReadOnly)
MS SQL Server 2017
MS SQL Server 2019
MS SQL Server 2022
MS SQL Server 2022 AdventureWorks (ReadOnly)
Firebird 4.0
Firebird 4.0 (Employee)
RedDatabase 5.0
Oracle Database 19c (HR)
Oracle Database 21c
Oracle Database 23c Free
SOQOL
ClickHouse
Run SQL code
Save snippet
ER Diagram
<?php
// Step 1: Connect to the database
// Assuming $mysqli is predefined
// Step 2: Handle form submission to insert data
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$stmt = $mysqli->prepare("INSERT INTO students (first_name, last_name, email) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $first_name, $last_name, $email);
$stmt->execute();
$stmt->close();
}
// Step 3: Retrieve student data from the database
$result = $mysqli->query("SELECT id, first_name, last_name, email FROM students");
?>
<!DOCTYPE html>
<html>
<head>
<title>University Student Management</title>
</head>
<body>
<h1>Student Management</h1>
<!-- Step 4: Form to add new student -->
<form method="POST">
<label>First Name:</label>
<input type="text" name="first_name" required><br><br>
<label>Last Name:</label>
<input type="text" name="last_name" required><br><br>
<label>Email:</label>
<input type="email" name="email" required><br><br>
<button type="submit">Add Student</button>
</form>
<!-- Step 5: Display student data -->
<h2>Student List</h2>
<table border="1">
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
</tr>
<?php
// Display all student records
while ($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . htmlspecialchars($row['id']) . "</td>";
echo "<td>" . htmlspecialchars($row['first_name']) . "</td>";
echo "<td>" . htmlspecialchars($row['last_name']) . "</td>";
echo "<td>" . htmlspecialchars($row['email']) . "</td>";
echo "</tr>";
}
$result->free();
?>
</table>
</body>
</html>
PHP version :
PHP 7.4
PHP 8.0
PHP 8.1
PHP 8.2
PHP 8.3
PHP 8.4
Run PHP Code
Save snippet