从前端wordpress向表中插入数据

时间:2013-04-13 13:25:45

标签: php mysql wordpress wordpress-plugin

我正在尝试从前端和wordpress中的后端向自定义表插入数据。 下面是我的代码,如果我从后端插入数据它的工作,但如果我尝试从前端插入它给我错误404。

 <?php
/*
Plugin Name: Custom Form
Description: Custom Plugin
Author: Bijay Luitel


*/

// Create the table if not exixts
?>
<style>
p {
    display:block;
}
h3 {
    height:20px;
    padding:10px 5px;
}

</style>
<?php
//Short Codes
add_shortcode('form_bands','form_bands');
function form_bands(){
    global $wpdb;
    $this_page = $_SERVER['REQUEST_URI'];
    $query1 = "SELECT * FROM grade";
    $result1 = $wpdb->get_results($query1);
    $query2 = "SELECT * FROM branch";
    $result2 = $wpdb->get_results($query2);

    if($_POST['action']==1 && $_POST['name'] != '' ){
     $page_one_table = 'band';
     $name =$_POST['name'];
     $mailingAddress = $_POST['address'];
     $city = $_POST['city'];
     $state = $_POST['state'];
     $zip = $_POST['zip'];
     $email = $_POST['email'];
     $url = $_POST['url'];
     $telephone = $_POST['telephone'];
     $gradeId = $_POST['grade'];
     $branchId = $_POST['branch'];
     $insertMe="INSERT INTO band ('Name', 'MailingAddress', 'City', 'State', 'Zip', 'Email', 'URL', 'Telephone', 'GradeID', 'BranchID') VALUES('$name', '$mailingAddress', '$city', '$state', '$zip', '$email', '$url', '$telephone', '$gradeId', '$branchId')";
     $insert_page_one = $wpdb->query($insertMe);
     //$insert_page_one = $wpdb->insert($page_one_table, $page_one_inputs);

     $form_id = $wpdb->insert_id;
         if($insert_page_one)
         {
            echo '<div id="successMsg" class="updated below-h2"><p>Operation Successful</p></div>';  
         }
         else{ 
         echo '<div id="successMsg" class="updated below-h2"><p>Error ! Recheck and tryagain.</p></div>';   
         }

     }
     elseif ($_POST['action']==1 && $_POST['name'] == ''){
         echo '<div id="successMsg" class="updated below-h2"><p>Error ! Recheck and tryagain.</p></div>';   
     }
?>

<h2>Bands</h2>
<div class="postbox">
    <form action="" method="post">
        <div class="inside">
        <table class="form-table">
            <tr>
                <th>Name :</th>
                <td><input type="text" name="name" /></td>
            </tr>
            <tr>
                <th>Address :</th>
                <td><input type="text" name="address" /></td>
            </tr>
            <tr>
                <th>City :</th>
                <td><input type="text" name="city" /></td>
            </tr>
            <tr>
                <th>State :</th>
                <td><input type="text" name="state" /></td>
            </tr>
            <tr>
                <th>Zip :</th>
                <td><input type="text" name="zip" /></td>
            </tr>
            <tr>
                <th>Telephone :</th>
                <td><input type="text" name="telephone" /></td>
            </tr>
            <tr>
                <th>Email :</th>
                <td><input type="text" name="email" /></td>
            </tr>
            <tr>
                <th>Url :</th>
                <td><input type="text" name="url" /></td>
            </tr>
            <tr>
                <th>Grade :</th>
                <td><select name="grade">
                        <?php foreach($result1 as $row){
                        $value = $row->GradeID;
                        echo '<option value="'.$value.'">';
                        echo $row->Grade;   
                        echo "</option>";
                    }?>
                    </select></td>
            </tr>
            <tr>
                <th>Branch :</th>
                <td><select name="branch">
                        <?php foreach($result2 as $row){
                        $value = $row->BranchID;
                        echo '<option value="'.$value.'">';
                        echo $row->Name;    
                        echo "</option>";
                    }?>
                    </select></td>
            </tr>
        </table>
        <p class="submit">
            <input type="submit" name="add_form" class="button-primary" value="Submit" />
        </p>
        <input type="hidden" name="action" value="1" />
    </form>
</div>
</div>
<?php
 }
function myForm () 
{
     add_menu_page('Forms', 'Forms', '','forms', ''); 
     add_submenu_page("forms", "Bands", "Bands", 0, "Bands", "form_bands"); 
}
add_action('admin_menu','myForm');

我该如何解决这个问题?请帮帮我

1 个答案:

答案 0 :(得分:1)

我预计您遇到的问题与您使用'name'的“保留”帖子变量名称有关。

The WordPress Codex page for Register_Taxonomy()包含“保留条款”列表。


此外,form标记上的action attribute内容遗失了您的网址。这在当前的浏览器中处理正常,但在某些旧版浏览器中可能会导致意外行为,并且不能保证将来可以使用。

更好的做法是完全删除此属性,如果您不打算使用它,因为the spec strongly discourages authors from leaving it empty

  

actionformaction内容属性,如果指定必须的值为valid non-empty URL potentially surrounded by spaces

(此信息取决于action属性,感谢@mercator和this answer