Update Record
CRUD TYPE PROJECT
-----------------------------------
query($delete_query);
// Redirect to the same page to refresh the records
echo "";
}
// Check if update button is clicked
if (isset($_POST['update_records'])) {
// Retrieve form data for update
$update_id = intval($_POST['update_id']); // Convert to integer
$update_name = sanitize_text_field($_POST['update_name']);
$update_age = intval($_POST['update_age']); // Convert to integer
$update_degree = sanitize_text_field($_POST['update_degree']);
// Update data in the database
$table_name = 'user_details_crud';
$wpdb->update(
$table_name,
array(
'name' => $update_name,
'age' => $update_age,
'degree' => $update_degree,
),
array('id' => $update_id),
array('%s', '%d', '%s'),
array('%d')
);
// Redirect to the same page to refresh the records
echo "";
}
// Set default sorting column and direction
$sort_column = isset($_GET['sort_column']) ? $_GET['sort_column'] : 'id';
$sort_order = isset($_GET['sort_order']) ? $_GET['sort_order'] : 'DESC';
// Set default search term
$search_term = isset($_GET['search']) ? $_GET['search'] : '';
// Construct the SQL query
$query = "SELECT * FROM user_details_crud";
// Add search functionality
if (!empty($search_term)) {
$query .= " WHERE name LIKE '%{$search_term}%' OR age LIKE '%{$search_term}%' OR degree LIKE '%{$search_term}%' OR entry_time LIKE '%{$search_term}%'";
}
// Add sorting functionality
$query .= " ORDER BY $sort_column $sort_order";
// Retrieve records from the database
$results = $wpdb->get_results($query);
// Display the records in a table with delete checkboxes, sorting links, and update forms
echo "";
echo "";
// JavaScript to handle the update form visibility
echo "";
// Update form
echo "";
echo "
Update Record
";
echo "";
echo "";
}
// Call the function to display records, handle sorting, deletions, and updates
displayRecords();
?>