Wordpress Tutorials
- Wordpress
- Directory and file structure.
- Wordpress configuration
- Htaccess Configuration
- WP-CONTENT user playground
- Increase file size loading in phpmyadmin on localhost.
- Fatal error: Allowed memory size of x bytes
- wordpress Dashboard
- How to set local host domain or Virtual host
- Header, index and footer page.
- How to create wordpress Themes
- Html to wordpress themes
- Child Themes
- Increase Upload Max Filesize
- wp_enqueue_style & script
- How to make plugin
- Create Plugin
- Insert data into table using plugin.
- Show data from database
- Hook & Filter
- Template Or Custom page template
- Custom Login template
- How to show post on diffrent page.
- Custom widgit
- How to use contact form 7 plugin.
- Wordpress Database
- How to use Duplicator plugin
- How to fix Fatal Error: Maximum Execution Time Exceeded in WordPress
- How to debuggin mode on in wordpress
- Auto update plugin and themes
- How to Transfer WordPress
- How to change a WordPress site URL
- Top 10 issues in wordpress
- wordpress website link
Wordpress setting
Wordpress Function
wordpress top 10 problems
Important Links
How to insert data into table using plugin.
create your plugin. under plugin directory
After that Activate your plugin.
finally add your shortcut on your page.
[elh-db-insert]
File name : applicationform.php
<?php
/**
* Plugin Name: my-application-plugin
* Plugin URI: http://itechtuto.com
* Description: This plugin show login page details.
* Version: 1.0.0
* Author: Mahtab Habib
* Author URI: http://itechtuto.ocm
* License: GPL2
*/
function elh_insert_into_db() {
global $wpdb;
// creates my_table in database if not exists
$table = $wpdb->prefix . "my_table";
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE IF NOT EXISTS $table (
`id` mediumint(9) NOT NULL AUTO_INCREMENT,
`name` text NOT NULL,
UNIQUE (`id`)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
// starts output buffering
ob_start();
?>
<form action="#v_form" method="post" id="v_form">
<label for="visitor_name"><h3>Hello there! What is your name?</h3></label>
<input type="text" name="visitor_name" id="visitor_name" />
<input type="submit" name="submit_form" value="submit" />
</form>
<?php
$html = ob_get_clean();
// does the inserting, in case the form is filled and submitted
if ( isset( $_POST["submit_form"] ) && $_POST["visitor_name"] != "" ) {
$table = $wpdb->prefix."my_table";
$name = strip_tags($_POST["visitor_name"], "");
$wpdb->insert(
$table,
array(
'name' => $name
)
);
$html = "<p>Your name <strong>$name</strong> was successfully recorded. Thanks!!</p>";
}
// if the form is submitted but the name is empty
if ( isset( $_POST["submit_form"] ) && $_POST["visitor_name"] == "" )
$html .= "<p>You need to fill the required fields.</p>";
// outputs everything
return $html;
}
// adds a shortcode you can use: [insert-into-db]
add_shortcode('elh-db-insert', 'elh_insert_into_db');
?>
// http://blog.samelh.com/2015/08/13/create-mysql-database-table-wordpress-insert-data-wpdb-html-form/
File name : applicationform.php
<?php
/**
* Plugin Name: my-application-plugin
* Plugin URI: http://itechtuto.com
* Description: This plugin show login page details.
* Version: 1.0.0
* Author: Mahtab Habib
* Author URI: http://itechtuto.ocm
* License: GPL2
*/
function elh_insert_into_db() {
global $wpdb;
// creates my_table in database if not exists
$table = $wpdb->prefix . "my_table";
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE IF NOT EXISTS $table (
`id` mediumint(9) NOT NULL AUTO_INCREMENT,
`name` text NOT NULL,
`address` text NOT NULL,
`mobile` text NOT NULL,
UNIQUE (`id`)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
// starts output buffering
ob_start();
?>
<form action="#v_form" method="post" id="v_form">
<label for="visitor_name"><h3>Name?</h3></label>
<input type="text" name="visitor_name" id="visitor_name" />
<label for="visitor_address"><h3>Address</h3></label>
<input type="text" name="visitor_address" id="visitor_address" />
<label for="visitor_mobile"><h3>Mobile</h3></label>
<input type="text" name="visitor_mobile" id="visitor_mobile" />
<input type="submit" name="submit_form" value="submit" />
</form>
<?php
$html = ob_get_clean();
// does the inserting, in case the form is filled and submitted
if ( isset( $_POST["submit_form"] ) && $_POST["visitor_name"] != "" && $_POST["visitor_address"] != "" && $_POST["visitor_mobile"] != "") {
$table = $wpdb->prefix."my_table";
$name = strip_tags($_POST["visitor_name"], "");
$address = strip_tags($_POST["visitor_address"], "");
$mobile = strip_tags($_POST["visitor_mobile"], "");
$wpdb->insert(
$table,
array(
'name' => $name,
'address' => $address,
'mobile' => $mobile
)
);
$html = "<p>Your name <strong>$name</strong> address <strong>$address</strong> and mobile <strong>$mobile</strong>was successfully recorded. Thanks!!</p>";
}
// if the form is submitted but the name is empty
if ( isset( $_POST["submit_form"] ) && $_POST["visitor_name"] == "" && $_POST["visitor_address"] != "" && $_POST["visitor_mobile"] != "")
$html .= "<p>You need to fill the required fields.</p>";
// outputs everything
return $html;
}
// adds a shortcode you can use: [insert-into-db]
add_shortcode('elh-db-insert', 'elh_insert_into_db');
?>
Another way :
global $wpdb;
$name = 'Toby';
$gender = 'boy';
$category1 = 1;
$category2 = 0;
$wpdb->insert('wp_names',
array(
'name'=>$name,
'gender'=>$gender,
'category1'=>$category1,
'category2'=>$category2
),
array(
'%s',
'%s',
'%d',
'%d'
)
);
The first two strings (%s) and the second two are numbers (%d). You could also have a floating point number (%f).
How to fetch data from database table in wordpress.
create my-showdata-plugin
File name : showdata.php
<?php
/**
* Plugin Name: my-showdata-plugin
* Plugin URI: http://itechtuto.com
* Description: This plugin show login page details.
* Version: 1.0.0
* Author: Mahtab Habib
* Author URI: http://itechtuto.ocm
* License: GPL2
*/
function show_data_db() {
?>
<table border="1">
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<?php
global $wpdb;
$result = $wpdb->get_results ( "SELECT * FROM wp_my_table" );
foreach ( $result as $print ) {
?>
<tr>
<td><?php echo $print->id;?></td>
<td><?php echo $print->name;?></td>
</tr>
<?php
}
}
// adds a shortcode you can use: [showtabledata]
add_shortcode('showtabledata', 'show_data_db');
?>
Another way.
global $wpdb;
/* wpdb class should not be called directly.global $wpdb variable is an instantiation of the class already set up to talk to the WordPress database */
$result = $wpdb->get_results( "SELECT * FROM wp_my_table"); /*mulitple row results can be pulled from the database with get_results function and outputs an object which is stored in $result */
//echo "<pre>"; print_r($result); echo "</pre>";
/* If you require you may print and view the contents of $result object */
echo "ID"." "."Name"."<br><br>";
foreach($result as $row)
{
echo $row->id." ".$row->name."<br>";
}
/* Print the contents of $result looping through each row returned in the result */