<?php
$servername
= "localhost"; // Change this to your server name
$username
= "root"; // Change this to your database username
$password
= ""; // Change this to your database password
$dbname
= "dbStudents";
//
Create a connection
$conn
= new mysqli($servername, $username, $password);
//
Check connection
if
($conn->connect_error) {
die("Connection failed: " .
$conn->connect_error);
}
//
Create database
$sql
= "CREATE DATABASE IF NOT EXISTS $dbname";
if
($conn->query($sql) === TRUE) {
echo "Database created successfully<br>";
}
else {
echo "Error creating database: "
. $conn->error . "<br>";
}
//
Select the database
$conn->select_db($dbname);
//
Create Student table
$sql
= "CREATE TABLE IF NOT EXISTS Student (
id INT AUTO_INCREMENT PRIMARY KEY,
roll_number INT NOT NULL,
student_name VARCHAR(255) NOT NULL
)";
if
($conn->query($sql) === TRUE) {
echo "Table created
successfully<br>";
}
else {
echo "Error creating table: " .
$conn->error . "<br>";
}
//
Accept roll number and student name from user
if
(isset($_POST['submit'])) {
$rollNumber = $_POST['roll_number'];
$studentName = $_POST['student_name'];
// Insert data into Student table
$sql = "INSERT INTO Student
(roll_number, student_name) VALUES ('$rollNumber', '$studentName')";
if ($conn->query($sql) === TRUE) {
echo "Data inserted
successfully<br>";
} else {
echo "Error inserting data: "
. $conn->error . "<br>";
}
}
//
Close the connection
$conn->close();
?>
<!DOCTYPE
html>
<html>
<head>
<title>Insert Student
Data</title>
</head>
<body>
<form method="post"
action="">
Roll Number: <input
type="text" name="roll_number"><br>
Student Name: <input
type="text" name="student_name"><br>
<input type="submit"
name="submit" value="Insert Data">
</form>
</body>
</html>
No comments:
Post a Comment