VEDPHP
VEDPHP
<!DOCTYPE html>
<html>
<head>
<title>Database Connection</title>
50px;
.message {
padding: 20px;
border-radius: 8px;
display: inline-block;
font-size: 18px;
.success { background-
#155724;
.error {
background-color: #f8d7da;
color: #721c24;
}
</style>
</head>
2302020101518 1
Neel koshiya j.
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "testdb";
"</div>");
2302020101518 2
Neel koshiya j.
// Close connection
$conn->close();
?>
</body>
</html>
OUTPUT:
2302020101518 3
Neel koshiya j.
2302020101518 4
Neel koshiya j.
<?php
$servername = "localhost";
$username = "root";
$password = "";
// Create connection
// Check connection
if (!$conn)
// Create database
$ans=mysqli_query($conn,$sql);
if(!$ans)
else
2302020101518 5
Neel koshiya j.
mysqli_close($conn);
?>
$count = $check->fetch_assoc();
if ($count['total'] == 0) {
");
2302020101518 6
Neel koshiya j.
style='background-color:#f0f0f0;'>
<th>ID</th>
<th>Name</th>
<th>Age</th>
<th>Grade</th>
</tr>";
echo "<tr>
<td>{$row['id']}</td>
<td>{$row['name']}</td>
<td>{$row['age']}</td>
<td>{$row['grade']}</td>
</tr>";
echo "</table>";
// Close connection
$conn->close();
?>
<?php
if (isset($_GET['id'])) {
$id = $_GET['id'];
2302020101518 7
Neel koshiya j.
$name = $_POST['name'];
2302020101518 8
Neel koshiya j.
$age = $_POST['age'];
exit();
// Fetch data
$row = $result->fetch_assoc();
} else {
exit();
} else {
exit();
}
?>
2302020101518 9
Neel koshiya j.
<form method="post">
</form>
$id = $_GET['id'];
header("Location: students.php");
exit();
2302020101518 10
Neel koshiya j.
3. Build a simple web application that performs Create, Read, Update, and
Delete operations on a student table using AJAX for asynchronous
communication and JSON for data handling.
Connect.php:-
<?php
$server="localhost";
$username="root";
$password="";
$connect=mysqli_connect($server,$username,$password); if (!$connect) {
die("Connection fai".mysqli_connect_error());
}
} else {
mysqli_close($connect);
?>
Createtable.php:-
<?php
$server="localhost";
2302020101518 11
Neel koshiya j.
$username="root";
$password="";
fai".mysqli_connect_error());
$sql=”CREATE TABLE users(id int, name varchar(20), email varchar(20), phone int)”;
$query=mysqli_query($connect,$sql); if (!$query) {
Successfully";
mysqli_close($connect);
db.php:-
<?php
"root", "");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
?>
api.php:-
<?php
include 'db.php';
$action = $_GET['action'];
if ($action == 'read') {
2302020101518 12
Neel koshiya j.
if ($action == 'create') {
$data = json_decode(file_get_contents("php://input"));
$stmt = $pdo->prepare("INSERT INTO users (name, email, phone) VALUES (?, ?, ?)");
if ($action == 'update') {
$data = json_decode(file_get_contents("php://input"));
=> 'success']);
if ($action == 'delete') {
$data = json_decode(file_get_contents("php://input"));
?>
Index.php:-
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
2302020101518 13
Neel koshiya j.
<link
SgOJa3DmI69IUzQ2PVdRZhwQ+dy64/BUtbMJw1MZ8t5HZApcHrRKUc 4W0kG879m7"
crossorigin="anonymous">
</head>
<div class="container">
<body>
<form id="userForm">
</form>
<h3>Users List</h3>
<script> loadData();
function loadData() {
'<tr><th>Name</th><th>Email</th><th>Phone</th><th>Action</th>
</tr>';
<td>${user.email}</td>
<td>${user.phone}</td>
2302020101518 14
Neel koshiya j.
<td>
</tr>`;
});
$('#userTable').html(rows);
});
};
$('#userForm')[0].reset();
$('#id').val(''); loadData();
});
});
function editUser(user) {
$('#id').val(user.id);
$('#name').val(user.name);
$('#email').val(user.email);
$('#phone').val(user.phone);
2302020101518 15
Neel koshiya j.
});
</script>
</div>
</body>
</html>
2302020101518 16