Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'execute' instead of 'query' causes errors.
Trying to use 'run' or 'send' which are not mysqli methods.
✗ Incorrect
The correct method to run an SQL query in mysqli is 'query'.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong id numbers like 10 or 1.
Leaving the id as 0 which might update no records.
✗ Incorrect
The user id to update is 5, so the WHERE clause must use 5.
3fill in blank
hardFix 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'
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.
✗ Incorrect
The id value should be a number without quotes in the SQL statement.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong column names like 'username' or 'password'.
Mixing up the order of columns and values.
✗ Incorrect
The columns to insert into are 'name' and 'email' matching the variables.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong column names like 'username' instead of 'name'.
Using wrong WHERE clause column.
✗ Incorrect
The columns to update are 'name' and 'email', and the WHERE clause uses 'id'.