Web Technologies - Old Questions
8. Write an example of server-side script to update data in the database.
5 marks
|
Asked in 2072
PHP script to update data in database
Consider a Student table with attribute id, firstname, lastname and email.
Now, update the record with id=2 in the "Student" table:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "UPDATE Student SET lastname='Poudel' WHERE id=2";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
?>