0
0
PHPprogramming~20 mins

Insert, update, delete operations in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of Insert, Update, Delete
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this PHP insert operation?

Consider the following PHP code that inserts a new user into a database. What will be the output?

PHP
<?php
$pdo = new PDO('sqlite::memory:');
$pdo->exec("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT);");
$stmt = $pdo->prepare("INSERT INTO users (name) VALUES (:name)");
$result = $stmt->execute([':name' => 'Alice']);
echo $result ? 'Insert successful' : 'Insert failed';
?>
AFatal error: Uncaught exception
BInsert successful
CInsert failed
DSyntax error
Attempts:
2 left
💡 Hint

Check if the execute() method returns true on success.

Predict Output
intermediate
2:00remaining
What is the output after this update operation?

This PHP code updates a user's name in the database. What will be the output?

PHP
<?php
$pdo = new PDO('sqlite::memory:');
$pdo->exec("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT);");
$pdo->exec("INSERT INTO users (name) VALUES ('Bob');");
$stmt = $pdo->prepare("UPDATE users SET name = :name WHERE id = :id");
$success = $stmt->execute([':name' => 'Robert', ':id' => 1]);
echo $success ? $stmt->rowCount() : 0;
?>
ASyntax error
B0
C1
Dfalse
Attempts:
2 left
💡 Hint

rowCount() returns the number of rows affected by the update.

Predict Output
advanced
2:00remaining
What error does this PHP delete operation raise?

Look at this PHP code that tries to delete a user. What error will it produce?

PHP
<?php
$pdo = new PDO('sqlite::memory:');
$pdo->exec("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT);");
$stmt = $pdo->prepare("DELETE FROM users WHERE id = ?");
$stmt->execute();
?>
APDOException: SQLSTATE[HY093]: Invalid parameter number
BNo error, deletes all rows
CSyntax error
DFatal error: Missing argument
Attempts:
2 left
💡 Hint

Check if the execute() method is called with the required parameter.

🧠 Conceptual
advanced
2:00remaining
Which option correctly updates multiple rows in PHP?

You want to update the status of all users older than 30 to 'active'. Which PHP code snippet does this correctly?

A$stmt = $pdo->prepare("UPDATE users SET status = 'active' WHERE age > 30"); $stmt->execute();
B$stmt = $pdo->prepare("UPDATE users SET status = :status WHERE age > :age"); $stmt->execute(['status' => 'active']);
C$stmt = $pdo->prepare("UPDATE users SET status = 'active' WHERE age > 30"); $stmt->execute(['active', 30]);
D$stmt = $pdo->prepare("UPDATE users SET status = :status WHERE age > :age"); $stmt->execute(['active', 30]);
Attempts:
2 left
💡 Hint

Check if the parameters match the placeholders in the query.

Predict Output
expert
3:00remaining
What is the output of this PHP code with transactions?

This PHP code uses a transaction to insert and then delete a user. What will be the output?

PHP
<?php
$pdo = new PDO('sqlite::memory:');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->exec("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT);");
try {
  $pdo->beginTransaction();
  $pdo->exec("INSERT INTO users (name) VALUES ('Charlie');");
  $pdo->exec("DELETE FROM users WHERE name = 'Charlie';");
  $pdo->commit();
  $count = $pdo->query("SELECT COUNT(*) FROM users;")->fetchColumn();
  echo $count;
} catch (Exception $e) {
  $pdo->rollBack();
  echo 'Failed';
}
?>
ASyntax error
B1
CFailed
D0
Attempts:
2 left
💡 Hint

Think about what happens when you insert and then delete the same row inside a transaction.