How can the PHP script be modified to display an error message if the insertion is not successful?
To display an error message if the insertion is not successful, we can use the mysqli_error() function to check for any errors that occur during the insertion process. If an error is detected, we can output a custom error message to the user.
// Attempt to insert data into the database
$insert_query = "INSERT INTO table_name (column1, column2) VALUES ('$value1', '$value2')";
$result = mysqli_query($connection, $insert_query);
// Check if the insertion was successful
if ($result) {
echo "Data inserted successfully!";
} else {
echo "Error: " . mysqli_error($connection);
}
Keywords
Related Questions
- Are there any potential pitfalls or drawbacks to using a full-fledged IDE for PHP development?
- How can regular expressions be utilized to handle multiple line breaks in text manipulation tasks in PHP?
- Are there specific guidelines for efficiently using arrays in PHP to store and retrieve image file names?