0
0
PHPprogramming~10 mins

Insert, update, delete operations in PHP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to insert a new record into the database.

PHP
<?php
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "INSERT INTO users (name, email) VALUES ('John', 'john@example.com')";
if ($conn->[1]($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $conn->error;
}
$conn->close();
?>
Drag options to blanks, or click blank then click option'
Asend
Bexecute
Crun
Dquery
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'execute' instead of 'query' causes errors.
Trying to use 'run' or 'send' which are not mysqli methods.
2fill in blank
medium

Complete the code to update the email of a user with id 5.

PHP
<?php
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "UPDATE users SET email = 'newemail@example.com' WHERE id = [1]";
if ($conn->query($sql) === TRUE) {
    echo "Record updated successfully";
} else {
    echo "Error updating record: " . $conn->error;
}
$conn->close();
?>
Drag options to blanks, or click blank then click option'
A10
B1
C5
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong id numbers like 10 or 1.
Leaving the id as 0 which might update no records.
3fill in blank
hard

Fix the error in the code to delete a user with id 3.

PHP
<?php
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "DELETE FROM users WHERE id = [1]";
if ($conn->query($sql) === TRUE) {
    echo "Record deleted successfully";
} else {
    echo "Error deleting record: " . $conn->error;
}
$conn->close();
?>
Drag options to blanks, or click blank then click option'
A3
Bid
C'3'
DNULL
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes around numeric id causing SQL errors.
Using 'id' as a string instead of a number.
Using NULL which deletes nothing.
4fill in blank
hard

Fill both blanks to insert a new user with name and email variables.

PHP
<?php
$name = 'Alice';
$email = 'alice@example.com';
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "INSERT INTO users ([1], [2]) VALUES ('$name', '$email')";
if ($conn->query($sql) === TRUE) {
    echo "New user added";
} else {
    echo "Error: " . $conn->error;
}
$conn->close();
?>
Drag options to blanks, or click blank then click option'
Aname
Bemail
Cusername
Dpassword
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong column names like 'username' or 'password'.
Mixing up the order of columns and values.
5fill in blank
hard

Fill all three blanks to update a user's name and email where id matches.

PHP
<?php
$id = 7;
$newName = 'Bob';
$newEmail = 'bob@example.com';
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "UPDATE users SET [1] = '$newName', [2] = '$newEmail' WHERE [3] = $id";
if ($conn->query($sql) === TRUE) {
    echo "User updated";
} else {
    echo "Error: " . $conn->error;
}
$conn->close();
?>
Drag options to blanks, or click blank then click option'
Aname
Bemail
Cid
Dusername
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong column names like 'username' instead of 'name'.
Using wrong WHERE clause column.