php 使用 WordPress $wpdb 将数据插入到 WordPress 数据库中的表中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11205932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:58:27  来源:igfitidea点击:

Inserting data into a table in WordPress database using WordPress $wpdb

phpwordpressforms

提问by AmmyKami83

I am starting out plugin development and have followed the tutorials on the WordPress Codex sites. I am now stuck - I have a database called "wp_imlisteningto", where the wp_was inserted using:

我正在开始插件开发并遵循 WordPress Codex 网站上的教程。我现在卡住了 - 我有一个名为“wp_imlisteningto”的数据库,其中wp_插入了使用:

$table_name = $wpdb->prefix . "imlisteningto";

When the plugin is activated.

当插件被激活时。

The database itself has three columns, set up when the plugin is activated:

数据库本身有三列,在插件激活时设置:

$sql = "CREATE TABLE $table_name (
id mediumint(9) AUTO_INCREMENT,
album VARCHAR(50),
artist VARCHAR(50),
PRIMARY  KEY (id)
);";

I am trying to insert data (by creating a new row) into this database from a php form.

我正在尝试从 php 表单将数据(通过创建新行)插入到这个数据库中。

Within the WordPress admin, I create a new page which has the very simple form:

在 WordPress 管理员中,我创建了一个具有非常简单形式的新页面:

<form action="/wp-content/plugins/listeningto/formhtml.php" method="post">
Album: <input type="text" name="album" />
Artist: <input type="text" name="artist" />
<input type="submit">
</form>

Which as you can see calls formhtml.php, which is:

正如你所看到的 call formhtml.php,它是:

<?php
global $wpdb;

$wpdb->insert( $table_name, array( 'album' => $_POST['album'], 'artist' => $_POST['artist'] ), array( '$s', '$s' ) );
?>

When I submit the form, I get an Error 500.0when running the plugin in Worpdress on IIS7.0, and a "Page Not Found"when running on another web server which runs apache.

当我提交表单,我得到一个Error 500.0在Worpdress运行插件时IIS7.0,和"Page Not Found"另一台Web服务器运行在运行时apache

If I change formhtml.phpto:

如果我formhtml.php改为:

<?php

echo $_POST['album'];
echo $_POST['artist'];

?>

Works fine - I get the album and artist that I put in the form. Obviously something I'm doing wrong when inserting the data (in a new row) into the database.

工作正常 - 我得到了我放在表格中的专辑和艺术家。显然,在将数据(在新行中)插入数据库时​​我做错了。

Any thoughts as to what that might be?

关于那可能是什么的任何想法?

UPDATE

更新

Ok, so if I update formhtml.phpwith this:

好的,所以如果我formhtml.php用这个更新:

<?php
require_once('../../../wp-config.php');
$table_name = $wpdb->prefix . "imlisteningto";
$wpdb->insert( $table_name, array( 'album' => $_POST['album'], 'artist' => $_POST['artist'] ), array( '$s', '$s' ) );
?>

I no longer get an error message, but data still doesn't get put into the database.

我不再收到错误消息,但数据仍然没有放入数据库。

UPDATE 2

更新 2

This worked for me:

这对我有用:

<?php
require_once('../../../wp-config.php');
global $wpdb;
$table_name = $wpdb->prefix . "imlisteningto";
$wpdb->insert( $table_name, array( 'album' => $_POST['album'], 'artist' => $_POST['artist'] ) );
?>

as did this:

就像这样做:

<?php
require_once('../../../wp-load.php');
global $wpdb;
$table_name = $wpdb->prefix . "imlisteningto";
$wpdb->insert( $table_name, array( 'album' => $_POST['album'], 'artist' => $_POST['artist'] ) );
?>

So, for some reason $wpdbwas not working unless I required either wp-configor wp-load.php. If include wp-load.php, $wpdbgets values and all is well.

因此,出于某种原因$wpdb,除非我需要wp-configwp-load.php。如果包含wp-load.php,则$wpdb获取值并且一切正常。

回答by Pramod

including

包含

require_once('../../../wp-config.php');

worked for me

为我工作

回答by Ajay Patel

Try this..

尝试这个..

<?php
global $wpdb;

$wpdb->insert( $table_name, array( 'album' => "$_POST['album']", 'artist' => "$_POST['artist']" ) );
?>

Ex :

前任 :

<?php
global $wpdb;

$wpdb->insert($table_name , array('chart_name' => "Line Chart" ,'chart_type' => "trends",'status' => 0));

?>

回答by ultrageek

You've probably figured this out by now, but no one addressed it here. Your sample code has '$s' in the 3rd parameter (2nd array), but that should be '%s' because it's for value-formatting. The WP Codex says [http://codex.wordpress.org/Class_Reference/wpdb]that this format parameter for $wpdb->insert() is optional.

你现在可能已经明白了这一点,但没有人在这里解决它。您的示例代码在第三个参数(第二个数组)中有 '$s',但这应该是 '%s',因为它用于值格式化。WP Codex 说 [ http://codex.wordpress.org/Class_Reference/wpdb]$wpdb->insert() 的这个格式参数是可选的。

回答by Lodi Cronje

I think there are 2 mistakes in you sql string.

我认为您的 sql 字符串中有 2 个错误。

Think it should be the $table_namevariable should be concatenated

认为应该是$table_name变量应该被连接

$sql = "CREATE TABLE" . $table_name . "(
id mediumint(9) AUTO_INCREMENT,
album VARCHAR(50),
artist VARCHAR(50),
PRIMARY  KEY (id)
)";

and remove ;on the last line.

并删除;最后一行。