使用WordPress $ wpdb将数据插入WordPress数据库中的表中 [英] Inserting data into a table in WordPress database using WordPress $wpdb

查看:715
本文介绍了使用WordPress $ wpdb将数据插入WordPress数据库中的表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开始插件开发,并已按照WordPress Codex网站上的教程进行操作.我现在陷入困境-我有一个名为"wp_imlisteningto"的数据库,在其中使用以下命令插入了wp_:

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:

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

激活插件时.

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

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)
);";

我正在尝试通过php表单将数据(通过创建新行)插入此数据库中.

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

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

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

<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>

您可以看到呼叫formhtml.php的地方,即:

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

<?php
global $wpdb;

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

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

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

如果我将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.

有什么想法吗?

更新

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

Ok, so if I update formhtml.php with this:

<?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.

更新2

这对我有用:

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

是这样的:

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

因此,出于某些原因,除非我需要wp-configwp-load.php,否则$wpdb无法正常工作.如果包含wp-load.php,则$wpdb获取值,一切都很好.

So, for some reason $wpdb was not working unless I required either wp-config or wp-load.php. If include wp-load.php, $wpdb gets values and all is well.

推荐答案

包括

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

为我工作

这篇关于使用WordPress $ wpdb将数据插入WordPress数据库中的表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆