Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从前端wordpress向表中插入数据_Php_Mysql_Wordpress - Fatal编程技术网

Php 从前端wordpress向表中插入数据

Php 从前端wordpress向表中插入数据,php,mysql,wordpress,Php,Mysql,Wordpress,我试图在wordpress中从前端和后端向自定义表插入数据。 下面是我的代码,如果我从后端插入数据,它会工作,但是如果我尝试从前端插入数据,它会给我错误404 <?php /* Plugin Name: Custom Form Description: Custom Plugin Author: Bijay Luitel */ // Create the table if not exixts ?> <style> p { display:block; }

我试图在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');

p{
显示:块;
}
h3{
高度:20px;
填充:10px 5px;
}

我认为您遇到的问题与使用“保留”post变量名有关,即
'name'

包含“保留条款”列表


此外,您的
表单
标签上缺少您的URL。这在当前浏览器中处理正常,但在一些较旧的浏览器中可能会导致意外行为,并且不能保证将来工作

更好的做法是,如果您不打算使用该属性,则完全删除该属性,因为:

action
formaction
内容属性(如果指定)必须具有一个值,该值为


(由于@mercator with,此信息是
操作
属性)

sql注入警告您可以在执行$insertMe之前回显$insertMe,以确保它包含您期望的内容,然后从那里返回。@Sepster我尝试过,但当我提交表单时,其显示错误404页,但如果是从管理员提交表单,数据插入成功Tanks@sepster,成功:),它是“保留”post变量“name”