Home / Comments / Untitled Snippet
Duplicate Snippet

Embed Snippet on Your Site

Untitled Snippet

Code Preview
php
<?php
<?php
// Function to insert data into oldagehome table
function insert_into_oldagehome($first_name, $last_name, $email, $mobileno, $message) {
    global $wpdb; // Access WordPress database object
    // Table name
    $table_name = $wpdb->prefix . 'oldagehome'; // Add prefix to table name
    // Prepare the data for insertion
    $data = array(
        'first_name' => sanitize_text_field($first_name),
        'last_name'  => sanitize_text_field($last_name),
        'email'      => sanitize_email($email),
        'mobileno'   => sanitize_text_field($mobileno),
        'message'    => sanitize_textarea_field($message),
    );
    // Insert the data into the table
    $inserted = $wpdb->insert($table_name, $data);
    // Check if the data was inserted successfully
    if ($inserted) {
        return "Data inserted successfully!";
    } else {
        return "Error: Data insertion failed!";
    }
}
// Example usage
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $first_name = isset($_POST['first_name']) ? $_POST['first_name'] : '';
    $last_name = isset($_POST['last_name']) ? $_POST['last_name'] : '';
    $email = isset($_POST['email']) ? $_POST['email'] : '';
    $mobileno = isset($_POST['mobileno']) ? $_POST['mobileno'] : '';
    $message = isset($_POST['message']) ? $_POST['message'] : '';
    // Call the function to insert data
    echo insert_into_oldagehome($first_name, $last_name, $email, $mobileno, $message);
}
?>

Comments

Add a Comment