How can the explode function be effectively used to parse a string in PHP for database insertion?

When inserting data into a database in PHP, sometimes you may need to parse a string to extract individual values before insertion. The explode function can be effectively used to split a string into an array of values based on a delimiter. These values can then be inserted into the database individually.

// Sample string to be parsed
$string = "John,Doe,30";

// Explode the string based on the comma delimiter
$values = explode(",", $string);

// Insert the values into the database
$query = "INSERT INTO users (first_name, last_name, age) VALUES ('$values[0]', '$values[1]', '$values[2]')";
mysqli_query($connection, $query);