简体   繁体   English

在WordPress页面中从MySQL插入和选择数据

[英]Insert and select data from MySQL in WordPress Page

I am designing a WordPress website, I am thorough with the design part, Now the next phase is the database related things. 我正在设计一个WordPress网站,我对设计部分进行了深入研究,现在下一阶段是与数据库相关的事情。

1. Insert data into database.
2. Select the data and display it in the page.
3. Where i need to create a php page if i dont want to install widget.? where i need the place the code?

I have googled some plugin and I got php code widget plugin and inserted into one of my pages as a widget 我用谷歌搜索了一些插件,然后得到了php code widget插件,并以小部件的形式插入了我的页面之一

I tried installing insert_php plugin but its not working so I am continuing with php_code_widget 我尝试安装insert_php插件,但无法正常工作,因此我继续使用php_code_widget

Pages-> All Pages-> Home Page -> Add Row -> Add Widget -> Php Code Widget.

Now in my MySQL Database I have a simple table called Rituals having three columns 现在在我的MySQL数据库中,我有一个名为Rituals的简单表,该表具有三列

 Ritual ID-> Int -> Auto Increament.
 Ritual_Name-> varchar
 Ritual_Active-> varchar.

Now I need to insert the Ritual name to the database, And with some reference I got this code and I have put it in the php code widget window. 现在,我需要将仪式名称插入数据库中,并通过一些参考获得了此代码,并将其放在php代码小部件窗口中。

<?php
 require_once('../../../wp-load.php');
 function insertuser(){

 if(isset($_POST['submit']){
 global $wpdb;
 $rname=$_POST['rname'];
 $ractive=$_POST['ractive'];
 $table_name = $wpdb->prefix . "mahathiwp";
  $wpdb->insert($table_name, array ('Ritual_Name' => $rname, 'Ritual_Active' =>   $ractive) ); 
 }
 ?>
  <form action="" method="post">
  Ritual Name: <input type="text" name="rname" /><br><br>
  Ritual Active: <input type="text" name="ractive" /><br><br> 
 <input type="submit" name="submit"/>
 </form>

 <?php

 }

insertuser();
?>

Data is not getting inserted.

Can you suggest the proper and faster way to insert the data into the database and also to retrieve the data and show it in my WordPress page. 您能建议一种将数据插入数据库中以及检索数据并将其显示在我的WordPress页面中的正确且快速的方法。 Any help appreciated. 任何帮助表示赞赏。

You have to customize your theme, 您必须自定义主题,

Add code to make an action while form is submitted. 添加代码以在提交表单时执行操作。

functions.php functions.php

function childtheme_style_andscripts(){
    //wp_enqueue_style('parent-style', get_template_directory_uri() . '/style.css' );
    wp_enqueue_script('ajax-function',  get_stylesheet_directory_uri() . '/js/ajaxfunction.js', array('jquery'), '1.0', true );
    wp_localize_script( 'ajax-function', 'usersubmitform', array(
        'url'=> admin_url('admin-ajax.php'),
        'security'=> wp_create_nonce('our-nonce')
    ) );
}

add_action('wp_enqueue_scripts','childtheme_style_andscripts');


function form_action_function(){
    require_once(dirname( __FILE__ ).'/../../../wp-load.php');
    $data = $_POST['data'];
    global $wpdb;
    if( !check_ajax_referer('our-nonce', 'security' ) ){

        wp_send_json_error('security failed');

        return;

    }
    //var_dump($data);
    $rname=$data['rname'];
    $ractive=$data['ractive'];

    $table_name = "rituals";
    $wpdb->insert($table_name, array ('rname' => $rname, 'ractive' => $ractive) ); 

    $wpdb->show_errors();
    $wpdb->print_error();
    echo 'From Submitted Successfully';

    die();
}
add_action('wp_ajax_nopriv_form_action_function','form_action_function');
add_action('wp_ajax_form_action_function','form_action_function');

Custom Page Template 自定义页面模板

<?php
/**
    Template Name: Form For User
 */

get_header(); ?>

<div id="main-content" class="main-content">

<?php
    if ( is_front_page() && twentyfourteen_has_featured_posts() ) {
        // Include the featured content template.
        get_template_part( 'featured-content' );
    }
?>
    <div id="primary" class="content-area">
        <div id="content" class="site-content" role="main">
            <h1 class="headingform">User Form</h1>
            <div class="msg"></div>
            <form  class="userform">
                Ritual Name: <input type="text" id="rname" name="rname" /><br><br>
                Ritual Active: <input type="text" id="ractive" name="ractive" /><br><br> 
                <input  id="usersubmit"type="submit" Value="Submit" />
            </form>

        </div><!-- #content -->
    </div><!-- #primary -->
    <?php get_sidebar( 'content' ); ?>
</div><!-- #main-content -->

<?php
get_sidebar();
get_footer();

ajax-admin.js ajax-admin.js

Here I have use ajax that why this file is created.put this file into you theme js folder. 在这里我使用ajax来说明为什么要创建此文件。将此文件放入主题js文件夹中。

jQuery(document).ready(function($){

    var submitButton = document.getElementById('usersubmit');

    var ajaxFunctionformprocess = function(fromdata, action){
        $.ajax({
            type:'post',
            url: usersubmitform.url,
            data:{
                action:action,
                data:fromdata,
                security:usersubmitform.security,



            },
            success:function(reponse){
                $('div.msg').html(reponse);
            },
            error:function(response){
                alert(response);
            }

        });



    }

    submitButton.addEventListener('click', function(event){
        event.preventDefault();
        var fromdata = {
            'rname':document.getElementById('rname').value,
            'ractive':document.getElementById('ractive').value,
        };
        ajaxFunctionformprocess(fromdata, 'form_action_function');  

        });




});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM