Skip to content

Instantly share code, notes, and snippets.

@MonteLogic
Last active May 1, 2021 22:03
Show Gist options
  • Save MonteLogic/65d80c20a7fc57d7388972b894f72348 to your computer and use it in GitHub Desktop.
Save MonteLogic/65d80c20a7fc57d7388972b894f72348 to your computer and use it in GitHub Desktop.
Read and Write into Wordpress MySQL
This is a tutorial on how to
** Insert form data on Database and
** Display Data from DB in Wordpress
<!-- ********************************* -->
<!-- Table Schema in MySQL -->
// 1. Here is the table schema we are using.
CREATE TABLE `books` (
`id` int(11) NOT NULL,
`title` varchar(100) NOT NULL,
`author` varchar(100) NOT NULL
) ENGINE =InnoDB DEFAULT CHARSET=utf8;
<!-- ********************************* -->
2. Create a template file in your theme directory and insert the following code
----------- > copy code from another page to get the structure of a page in your theme.
Add the following on top of the page
/*
Template Name: Book Registration Form
*/
<!-- book_registration_template.php >>>>>> Registration Form Template -->
<form role="form" method="post">
<div class="form-group">
<input id="title" name="title" type="text" placeholder="Book Title" class="form-control input-sm" required="">
</div>
<div class="form-group">
<input id="author" name="author" type="text" placeholder="Primary Author" class="form-control input-sm" required="">
</div>
<div class="row justify-content-center">
<div class="col-xs col-sm-4 col-md-4">
<input type="submit" value="Submit" class="btn btn-info btn-block" name="submitbtn">
</div>
</div>
</form>
<!---- ************************************************************* --->
3. Insert the following code in functions.php
<!--- Save to DB code in functions.php -->
<?php
if(isset($_POST['submitbtn'])) {
$data = array(
'title' => $_POST['title'],
'author' => $_POST['author'],
);
$table_name = 'books';
$result = $wpdb->insert($table_name, $data, $format=NULL);
if ($result==1){
echo "<script> alert('Book Saved');</script>";
}else{
echo "<script>alert('Unable to Save');</script>";
}
}
?>
<!-- ********************************************************** -->
4. Create a page that uses this tempate
<!-- ********************************************************** -->
TO DISPLAY DB DATA
5. Create another template in your theme directory and insert the following code.
<!-- view_books_template.php >>> Display Data from DB -->
<?php
global $wpdb;
$result = $wpdb->get_results("select * from books");
?>
<table class="table table-striped">
<thead>
<tr>
<th>Book Title</th>
<th>Primary Author</th>
</tr>
</thead>
<tbody>
<?php
foreach ($result as $book) {?>
<tr>
<td><?php echo $book->title; ?> </td>
<td><?php echo $book->author; ?> </td>
</tr>
<?php } ?>
</tbody>
</table>
6. Create a page that uses view_books_template.php template
@MonteLogic
Copy link
Author

Source code for the following video: https://www.youtube.com/watch?v=nj2DKyQn6Gs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment