[go: up one dir, main page]

0% found this document useful (0 votes)
26 views2 pages

P 16 PHP

The document contains a PHP program that updates and deletes records in a student database. It establishes a connection to the database, updates a student's age, and deletes a record based on roll number. The program also includes error handling for both update and delete operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views2 pages

P 16 PHP

The document contains a PHP program that updates and deletes records in a student database. It establishes a connection to the database, updates a student's age, and deletes a record based on roll number. The program also includes error handling for both update and delete operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Practical No.

16

Write a program to update and delete records from student database

Code :-

<?php }
$servername = "localhost"; $students = [
$username = "username"; [1, 'Riddhi Sardesai', 19, 'A'],
$password = "1234"; [2, 'Sabina Mujawar', 19, 'A'],
$dbname = "students"; [3, 'Prachi Dhere', 19, 'A'],
$conn = new mysqli($servername, $username, [4, 'Nikita Khot', 19, 'A'],
$password, $dbname); [5, 'Sakshi Kamble', 19, 'A'],
if ($conn->connect_error) { [6, 'Divya Patil', 19, 'A'],
die("Connection failed: " . $conn->connect_error); [7, 'Shreya Gangdhar', 19, 'A'],
} [8, 'Shweta Chavan Patil', 19, 'A']
$update_stmt = $conn->prepare("UPDATE stud SET ];
age = ? WHERE name = ?"); $stmt = $conn->prepare("INSERT INTO stud (roll_no,
$update_stmt->bind_param("is", $new_age, name, age, grade) VALUES (?, ?, ?, ?)");
$name_to_update); $stmt->bind_param("isis", $roll_no, $name, $age,
$new_age = 19; $grade);
$name_to_update = 'Nikita Khot'; foreach ($students as $student) {
if ($update_stmt->execute()) { $roll_no = $student[0];
echo "Age updated for $name_to_update to $name = $student[1];
19.<br>"; $age = $student[2];
} else { $grade = $student[3];
echo "Error updating record for if ($stmt->execute()) {
$name_to_update: " . $update_stmt->error . "<br>"; echo "Record for $name inserted
} successfully.<br>";
$delete_stmt = $conn->prepare("DELETE FROM stud } else {
WHERE roll_no = ?"); echo "Error inserting record for $name: " .
$delete_stmt->bind_param("i", $roll_no_to_delete); $stmt->error . "<br>";
$roll_no_to_delete = 9; }
if ($delete_stmt->execute()) { }
echo "Record with roll_no $roll_no_to_delete $stmt->close();
deleted successfully.<br>"; $update_stmt->close();
} else { $delete_stmt->close();
echo "Error deleting record with roll_no $conn->close();
$roll_no_to_delete: " . $delete_stmt->error . "<br>"; ?>
Output :-

You might also like