将数据插入WordPress插件中的新表

时间:2014-07-16 04:15:25

标签: php mysql wordpress wordpress-plugin

我正在开发一个插件,我创建了一个带有插件安装的新表,表已成功创建。

现在我想在这个新表中添加数据。我在" lsp_manage_foo.php"中创建了一个表单。并在" lsp_manage_process.php"中写入插入查询。当我点击提交按钮时出现错误:

Not Found

The requested URL /lgs_pro/wp-admin/lsp_manage_process.php was not found on this server.
Apache/2.2.15 (CentOS) Server at 192.168.2.2 Port 80

并在地址栏中链接如下:

http://192.168.2.2/lgs_pro/wp-admin/lsp_manage_process.php

问题是什么?

这是我的代码:

lsp_manage_foo.php:

<form action="lsp_manage_process.php" method="post" name="lsp_add_foo">
    <table width="100%" border="0" cellspacing="4" cellpadding="0">
        <tr>
            <td width="25%">
                  <label>
                       foo Name
                  </label>
            </td>
            <td width="58%">
                 <input type="text" name="lsp_add_foo" id="lsp_add_foo" value="">
            </td>
            <td width="10%" align="right">
                 <input type="submit" name="lsp_save_foo" value="Add Foo">
            </td>
         </tr>
     </table>
</form>

lsp_manage_process.php:

<?php
    global $wpdb;
    $foo_add = $wpdb->prefix."lsp_foo";

    $lsp_foo_name = stripslashes(strip_tags($_POST['lsp_add_foo']));

    $foo_shortcode = str_replace(" ", "_", $lsp_foo_name);
    $foo_shortcode = strtolower($foo_shortcode);

    $foo_data = array(
        'foo_name'       =>  $lsp_foo_name,
        'foo_shortcode'  =>  $foo_shortcode
    );

    $foo_insert = $wpdb->insert($foo_add,$foo_data);

    header("Location: lsp_manage_foo.php");
?>

1 个答案:

答案 0 :(得分:1)

好的,我自己找到了这个问题的解决方案

在“lsp_manage_foo.php”文件中

<form action="<?php echo plugin_dir_url(__FILE__) ?>lsp_foo/lsp_manage_process.php" method="post" name="lsp_add_foo">
    <table width="100%" border="0" cellspacing="4" cellpadding="0">
        <tr>
            <td width="25%">
                  <label>
                       foo Name
                  </label>
            </td>
            <td width="58%">
                 <input type="text" name="lsp_add_foo" id="lsp_add_foo" value="">
            </td>
            <td width="10%" align="right">
                 <input type="submit" name="lsp_save_foo" value="Add Foo">
            </td>
         </tr>
     </table>
</form>

在“lsp_manage_process.php”中

<?php
    require_once( str_replace('//','/',dirname(__FILE__).'/') .'../../../../wp-config.php');
    global $wpdb;
    $foo_add = $wpdb->prefix."lsp_foo";

    $lsp_foo_name = stripslashes(strip_tags($_POST['lsp_add_foo']));

    $foo_shortcode = str_replace(" ", "_", $lsp_foo_name);
    $foo_shortcode = strtolower($foo_shortcode);

    $foo_data = array(
        'foo_name'       =>  $lsp_foo_name,
        'foo_shortcode'  =>  $foo_shortcode
    );

    $foo_insert = $wpdb->insert($foo_add,$foo_data);

    header("Location: Location: ".site_url()."/wp-admin/admin.php?page=manage_foos");
?>

我用这行

require_once( str_replace('//','/',dirname(__FILE__).'/') .'../../../../wp-config.php');

因为我的$ wpdb无法在这个“lsp_manage_process.php”中工作,所以我需要“config.php”并且我的$ wpdb工作正常。