注册插件时没有错误,但仍然没有更新wordpress表

时间:2015-06-02 14:59:23

标签: php database wordpress plugins wpdb

这是我的插件,它让我疯了......它在激活时创建了一个新表,但之后不会插入或更新数据,页面上没有错误..请帮助!

<?php


            wp_register_style( 'andreiplugin', plugin_dir_url( FILE ) . 'pluginAndrei.css' );
            register_activation_hook(__FILE__, 'andreiPlugin_table');

我在这里制作表格

function andreiPlugin_table() {
            require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
            global $wpdb;
            global $table_name;

            $table_name = $wpdb->prefix."myusers";
            if($wpdb->get_var("SHOW TABLES LIKE '.$table_name'") != $table_name) {

                $sql = 'CREATE TABLE '.$table_name.'(
                        id INTEGER NOT NULL, 
                        name VARCHAR(30) NOT NULL,
                        email VARCHAR(30),
                        gender VARCHAR(50),
                        reg_date TIMESTAMP,
                        PRIMARY KEY  (id))';


                    dbDelta($sql);

                    add_option("andreiPlugin_version", "1.0");

                    }




            }

表单功能:

            function userForm(){


            $name = $_POST['username'];
            $email = $_POST['email'];
            $gender = $_POST['gender'];
            $insert = 1;
            global $wpdb;

            $result_db_table = $wpdb->prefix . "myusers";
            $query_data = $wpdb->get_results($wpdb->prepare("
                        SELECT *
                        FROM " .$result_db_table, OBJECT )); 



            while ($query_data) {

                    if($name == $query_data['name']){

                        $insert = 0;

                        $wpdb->update( 
                            '$table_name', 
                            array( 
                                'name' => '$name',
                                'email' => '$email',    
                                'gender' => '$gender'    
                                ));
                    }


                }

                if($insert==1){

                    $wpdb->insert( 
                '$result_db_table', 
                array( 
                    'name' => '$name', 
                    'email' => '$email',
                    'gender' => '$gender'
                ));

                    }

            }


            ?>

我还在插件中包含了html ..

            <html lang="en">



            <head>



                <meta http-equiv="content-type" content="text/html; charset=utf-8">

                <title>formsubmit</title>

并在javascript中进行表单验证

                <script type="text/javascript">
                    function check_info(){
                        var username = document.getElementById('username').value;
                        var email = document.getElementById('email').value;

                        var male = document.getElementById('auto_renew_male').checked;
                        var female = document.getElementById('auto_renew_female').checked;
                        if (male==false&&female==false) {

                        alert('Please specify your gender');
                            return false;
                        }
                        if (username==""|| email=="") {

                            alert('Please fill in all fields')
                            return false;
                        }
                        else{

                            return true;
                        }
                    }

                </script>

            </head>



            <body>
                <div id="wrapper"> <!--style="float:left; margin-left: 300px;"-->

而且......形式本身

            <form action="" method="post" onsubmit="return check_info();">

                <p>Full Name:</p><input type="text" name="username" id="username"/>
                <p>Email:</p><input type="text" name="email" id="email"/>
                <p>What is your gender</p>
                Male<input type="radio" name="gender" value="male" id="auto_renew_male"><br/>
                Female<input type="radio" name="gender" value="female" id="auto_renew_female"><br/>
                        <input type="submit" value="login" name="submitButton"/>
            </form>

                </div>

            </body>



            </html>

最后在这里我执行userForm函数..我试过简单地在if()之后用userForm()调用它;并且仍然没有效果......它也回应了#34;谢谢......&#34;按下按钮后

            <?php


                if(isset($_POST['submitButton'])){
                    register_activation_hook(__FILE__, 'userForm');


                    echo "Thank you for your submission!";
                }


            ?>

0 个答案:

没有答案