How can PHP be used to handle data insertion and error handling in a more efficient manner when interacting with Oracle databases?

When interacting with Oracle databases in PHP, it is important to handle data insertion and error handling efficiently to ensure data integrity and smooth operation. One way to achieve this is by using prepared statements for data insertion to prevent SQL injection attacks and using try-catch blocks for error handling to gracefully handle exceptions.

<?php
// Establish a connection to the Oracle database
$connection = oci_connect('username', 'password', 'localhost/XE');

// Prepare a SQL statement with placeholders for data insertion
$sql = 'INSERT INTO table_name (column1, column2) VALUES (:value1, :value2)';
$statement = oci_parse($connection, $sql);

// Bind the values to the placeholders
oci_bind_by_name($statement, ':value1', $value1);
oci_bind_by_name($statement, ':value2', $value2);

// Execute the statement
if (oci_execute($statement)) {
    echo 'Data inserted successfully';
} else {
    echo 'Error inserting data';
}

// Close the connection
oci_close($connection);
?>