Friends, you must know one thing that in any website, whether it is WordPress or HP, there is one thing which is very important, that is our operation, which means inserting any data in the database, fetching data from it, updating it, deleting it, are called operations.
We are dealing with crud database operations so we need to have database table to store be able to store data.
Create Menu First for insert operation i need path till submit pages
function my_admin_menu() {
add_menu_page('Menu Title', 'All Customers',
'manage_options', 'all-customers',
'my_admin_page_contents', 'dashicons-schedule', 3
);
add_submenu_page('all-customers', 'Add Customers', 'Add Customers',
'manage_options', 'add-customers',
'my_sub_menu_admin_page_contents');
add_submenu_page('all-customerspk', 'Submit Customers', 'Submit Customers',
'manage_options', 'submit-customers',
'my_sub_menu_admin_page_submit');
add_submenu_page('all-customerspk', 'Edit Customers', 'Edit Customers',
'manage_options', 'edit-customers',
'my_sub_menu_admin_page_edit');
add_submenu_page('all-customerspk', 'Update Customers', 'Update Customers',
'manage_options', 'update-customers',
'my_sub_menu_admin_page_update');
add_submenu_page('all-customerspk', 'Delete Customers', 'Delete Customers',
'manage_options', 'delete-customers',
'my_sub_menu_admin_page_delete');
}
add_action('admin_menu', 'my_admin_menu');
function my_admin_page_contents() {
include('all_customers.php');
}
function my_sub_menu_admin_page_contents() {
include('form.php');
}
function my_sub_menu_admin_page_submit() {
include('submit.php');
}
function my_sub_menu_admin_page_edit() {
include('edit.php');
}
function my_sub_menu_admin_page_update() {
include('update.php');
}
function my_sub_menu_admin_page_delete() {
include('delete.php');
}
form.php
<div class="container">
<div class="card" style="padding:10px; background:white">
<form action="admin.php?page=submit-customers" class="" method="post">
<div class="mb-3 mt-3">
<label for="uname" class="form-label">Name:</label>
<input type="text" class="form-control" id="unames" placeholder="Enter name" name="name" required>
</div>
<div class="mb-3">
<label for="pwd" class="form-label">Mobile No:</label>
<input type="number" class="form-control" id="number" placeholder="Enter your number" name="mobile" required>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
submit.php
<?php
global $wpdb;
$name = $_POST['name'];
$mobile = $_POST['mobile'];
$table_name = 'wp_customers';
$q = $wpdb->insert(
$table_name,
array(
'name' => $name,
'phone' => $mobile,
)
);
if($q){ ?>
<script> window.location.href ="http://localhost/wordpress/wp-admin/admin.php?page=all-customers";</script>
<?php }
all_customers.php
<div class="container table-responsive">
<table class="table table-striped">
<thead>
<th>S.No</th>
<th>Name</th>
<th>Phone</th>
<th>Action</th>
</thead>
<?php
global $wpdb;
$result = $wpdb->get_results("SELECT * FROM wp_customers");
foreach ($result as $key => $print) {
echo "<tbody><tr>
<td width='25%'>$key</td>
<td width='25%'>$print->name</td>
<td width='25%'>$print->phone</td>
<td>
<a href='admin.php?page=edit-customers&&id=$print->id' class='btn btn-success'>Edit</a>
<a href='admin.php?page=delete-customers&&id=$print->id' class='btn btn-danger'>Delete</a>
</td>
</tr></tbody>"; } ?>
</table>
</div>