0

我正在尝试从 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');

我怎么解决这个问题?请帮我

4

1 回答 1

1

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

WordPress Codex 页面Register_Taxonomy()包含“保留条款”列表。


此外,您标签上的action属性缺少您的 URL。form这在当前浏览器中处理得很好,但在某些旧浏览器中可能会导致意外行为,并且不保证将来可以正常工作。

更好的做法是完全删除此属性,如果您不打算使用它,因为规范强烈建议作者不要将其留空

actionformactioncontent 属性(如果指定必须具有一个有效的非空 URL 值,该 URL 可能被空格包围。

(这个信息重新action属性感谢@mercator这个答案

于 2013-04-13T14:11:57.237 回答