简体   繁体   English

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

[英]Insert data into new table in WordPress plugin

I am working on a plugin and I create new table with plugin installation, table created successfully. 我正在使用插件,并且通过插件安装创建了新表,该表创建成功。

Now I want to add data in this new table. 现在,我想在此新表中添加数据。 I create a form in "lsp_manage_foo.php" and write insert query in "lsp_manage_process.php". 我在“ lsp_manage_foo.php”中创建一个表单,并在“ lsp_manage_process.php”中编写插入查询。 When I click on submit button I got the error: 当我单击提交按钮时,出现错误:

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

and in address bar the link is like that: 在地址栏中的链接是这样的:

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

What is the issue? 有什么问题

Here is my code: 这是我的代码:

lsp_manage_foo.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: 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");
?>

Ok i find the solution of this issue by myself 好的,我自己找到了解决此问题的方法

In "lsp_manage_foo.php" file 在“ 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>

And in "lsp_manage_process.php" 并在“ 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");
?>

I use this line 我用这条线

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

because my $wpdb not working in this "lsp_manage_process.php", so i require "config.php" and my $wpdb works perfectly. 因为我的$ wpdb在此“ lsp_manage_process.php”中不起作用,所以我需要“ config.php”,并且我的$ wpdb可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM