Which SQL statement is used to insert a new row into a table?
Analysis & Theory
The correct SQL command to insert a row is `INSERT INTO`.
Which PHP function is used to execute the SQL INSERT statement?
Analysis & Theory
`mysqli_query()` is used to execute SQL commands like `INSERT INTO`.
What does this SQL statement do?
```
INSERT INTO users (username, email) VALUES ('John', 'john@example.com');
```
C
Inserts a new user with username John and email john@example.com
Analysis & Theory
It inserts a new record into the `users` table with the given username and email.
How do you insert multiple rows in a single SQL statement?
A
Using multiple INSERT statements
B
Using one INSERT INTO with multiple VALUES sets
C
Using INSERT INTO...SELECT
Analysis & Theory
You can insert multiple rows using `INSERT INTO table (columns) VALUES (row1), (row2), (row3);`.
What is the output of this code if the query is successful?
```
$sql = "INSERT INTO users (name) VALUES ('Alice')";
if (mysqli_query($conn, $sql)) {
echo "New record created";
} else {
echo "Error";
}
```
Analysis & Theory
If the insert is successful, the output will be `New record created`.
Which function is used to get the ID of the last inserted row in PHP MySQLi?
Analysis & Theory
`mysqli_insert_id()` returns the ID of the last inserted row.
What happens if you omit a NOT NULL column in an INSERT statement?
C
It assigns 0 automatically
Analysis & Theory
If a NOT NULL column is not provided a value, the insert fails with an error.
Which is the correct SQL to insert multiple users?
`users (name)` table.
A
INSERT INTO users name VALUES ('John'), ('Mike');
B
INSERT users (name) VALUES ('John'), ('Mike');
C
INSERT INTO users (name) VALUES ('John'), ('Mike');
D
INSERT users name ('John'), ('Mike');
Analysis & Theory
The correct syntax is `INSERT INTO users (name) VALUES ('John'), ('Mike');`.
What is the best practice to prevent SQL injection when inserting data?
A
Using string concatenation
B
Using prepared statements
Analysis & Theory
Prepared statements are the safest way to prevent SQL injection.
Why would you use a multiple-row insert instead of multiple single inserts?
B
It reduces database server overhead and improves performance
C
It creates backups automatically
Analysis & Theory
Multiple-row inserts are more efficient because they reduce the number of round trips to the database.