douqiu1604 2012-12-07 06:42
浏览 56

我想在插件的WP中发布数据库

I am using a form to add data in table using plugin in wordpress but when i enters submitt button it shows nothing and no data add in table where i doing mistake?? plz anybody help me. My code for insert data is here...

  <form id="form1" name="form1" method="post" action="">
  Name
  <input type="text" name="name" /> </br>
  Website 
    <input type="text" name="website" /> </br>
    Description
    <input type="text" name="description" /> </br>
    <input type="submit" value="Submit Form" />
     </form>
    <?php
    global $wpdb;
    $wpdb->query($structure);
    $wpdb->query="insert into ".PRO_TABLE_PREFIX."tutorial ( name, website, description )
    values('{$_POST['name']}','{$_POST['website']}','{$_POST['description']}')";
    if (is_object($wpdb) && is_a($wpdb, 'wpdb')) {
    if (!$wpdb->insert('.PRO_TABLE_PREFIX.tutorial',
    array(
                            'name'=>$_POST[name]
                            ,'website'=>$_POST[website]
                            ,'description'=>$_POST[description]
                ))) exit; 
                } 
else {echo 'Form Submitted';}
?>
  • 写回答

1条回答 默认 最新

  • dqm4675 2012-12-07 07:17
    关注

    A blank page usually means an internal 50x error, normally caused by PHP or your web hosting software (which is likely Apache).

    $wpdb->query="insert into ".PRO_TABLE_PREFIX."tutorial ( name, website, description )
        values('{$_POST['name']}','{$_POST['website']}','{$_POST['description']}')";
    

    $wpdb->query= is not valid. The code should read :

     $wpdb->query("insert into ".PRO_TABLE_PREFIX."tutorial ( name, website, description )
    values('{$_POST['name']}','{$_POST['website']}','{$_POST['description']}')");
    

    because $wpdb->query is a function, not a variable.

    Update: (More in-depth)

    First, let me start by linking to the wpdb documentation. For your purpose, you'll want to do this:

    $table = $wpdb->prefix."my_table";
    

    Note: When typing in the table name, don't include the "wp_" prefix. The "wp_" prefix can be changed by a number of things, but it will always be stored in $wpdb->prefix, so always use this instead of typing the default prefix.

    global $wpdb;
    
    $wpdb->insert($table,array(
        "name" => mysql_real_escape_string($_POST['name']),
        "website" => mysql_real_escape_string($_POST['website']),
        "description" => mysql_real_escape_string($_POST['description'])
    ));
    

    That will enter the record into your database. mysql_real_escape_string is important to protect yourself against MYSQL Injection. That's about all there is to it.

    Update 2: (response to next comment)

    Then you need to have PHP check to see if the form is submitted. You could simply add if(isset($_POST)){}, but I personally don't like doing that because if another form submitted to this page via post, the database would still update.

    <?php if(!isset($_POST[PLUGIN_PREFIX.'submit'])){ 
    
        global $wpdb;
        $table = $wpdb->prefix.PLUGIN_PREFIX."my_table";
    
        // $wpdb->insert will return true or false based on if the query was successful.
        $success = $wpdb->insert($table,array(
            "name" => mysql_real_escape_string($_POST[PLUGIN_PREFIX.'name']),
            "website" => mysql_real_escape_string($_POST[PLUGIN_PREFIX.'website']),
            "description" => mysql_real_escape_string($_POST[PLUGIN_PREFIX.'description'])
        ));
    
        if($success){
            $display = '<div class="'.PLUGIN_PREFIX.'submit_success">
                Your data has been saved.
            </div>';
        }else{
            $display = '<div class="'.PLUGIN_PREFIX;.'submit_fail">
                Your data failed to save. Please try again.
            </div>';
        }
    }
    
    $display .= '<form id="form1" name="form1" method="post" action="">
        <label for="name">Name</label>
        <input id="name" type="text" name="'.PLUGIN_PREFIX.'name" /> </br>
    
        <label for="website">Website</label> 
        <input id="website" type="text" name="'.PLUGIN_PREFIX.'website" /> </br>
    
        <label for="description">Description</label>
        <input id="description" type="text" name="'.PLUGIN_PREFIX.'description" /> </br>
    
        <input type="hidden" name="'.PLUGIN_PREFIX.'submit" />
        <input type="submit" value="Submit Form" />
    </form>';
    
    return $display;
    

    A few things I added that seem to be part of good plugin development:

    • One of the first lines of the first file in your plugin should have a line similar to define('PLUGIN_PREFIX', 'plugin_name_');. Use the prefix before anything that may conflict with other plugins.
    • <label> tags are very important. if the label's "for" matches an input's "id", clicking the label will select that input.
    • Returning a variable holding your HTML is the proper way to display plugins. If you have other content on that page above the plugin and you just echo the form, the form will appear above everything else, regardless of the position of the shortcode.
    评论

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计