我正在开始插件开发,并遵循了WordPress Codex网站上的教程。我现在被困住了 - 我有一个名为"wp_imlisteningto"的数据库,其中wp_
是使用以下方法插入的:
$table_name = $wpdb->prefix . "imlisteningto";
当插件被激活时。
数据库本身有三列,在激活插件时设置:
$sql = "CREATE TABLE $table_name (
id mediumint(9) AUTO_INCREMENT,
album VARCHAR(50),
artist VARCHAR(50),
PRIMARY KEY (id)
);";
我正在尝试将数据(通过创建新行)从 php 表单插入此数据库。
在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>
如您所见,它调用formhtml.php
,即:
<?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"
。
如果我将formhtml.php
更改为:
<?php
echo $_POST['album'];
echo $_POST['artist'];
?>
工作正常 - 我得到了我放入表格中的专辑和艺术家。显然,我在将数据(在新行中)插入数据库时做错了什么。
关于那可能是什么的任何想法?
更新
好的,所以如果我用这个更新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' ) );
?>
我不再收到错误消息,但数据仍然没有放入数据库。
更新 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-config
或wp-load.php
,否则$wpdb
不起作用。如果包含wp-load.php
,$wpdb
将获取值,一切都很好。
包括
require_once('../../../wp-config.php');
为我工作
试试这个。
<?php
global $wpdb;
$wpdb->insert( $table_name, array( 'album' => "$_POST['album']", 'artist' => "$_POST['artist']" ) );
?>
前任:
<?php
global $wpdb;
$wpdb->insert($table_name , array('chart_name' => "Line Chart" ,'chart_type' => "trends",'status' => 0));
?>
你现在可能已经想通了,但这里没有人解决这个问题。示例代码在第三个参数(第二个数组)中有"$s",但这应该是"%s",因为它用于值格式设置。WP Codex 说 [http://codex.wordpress.org/Class_Reference/wpdb] $wpdb->insert() 的这个格式参数是可选的。
根据数据库表创建数组。然后使用 WP 插入 .
global $wpdb;
$order = [
'product_id' =>'1',
'discount' => '1',
'total' => '1',
'status' => '1',
'payment_method' => '1',
'payment_reference' => '1',
'sold_by' => '1'
];
$wpdb->insert('DB_TABLE_NAME', $order );
$global $wpdb;
$table_name1 = $wpdb->prefix.'php_crud';
$data_insert= array(
'uname1' => $uname1,
'uemail1' => $uemail1,
'upass1' => $upass1,
'language1' => $language1,
'gender1' => $gender1,
'ustate1' => $ustate1,
'file1' => $finalimg
);
$form_insert1=$wpdb->insert($table_name1,$data_insert);
if($form_insert1)
{
echo "<div class='container' style{ color:red;} >Form
Submitted Successfully<div>";
}
else
{
echo "ERROR";
}
我认为你的sql字符串中有2个错误。
认为它应该是应该连接的$table_name
变量
$sql = "CREATE TABLE" . $table_name . "(
id mediumint(9) AUTO_INCREMENT,
album VARCHAR(50),
artist VARCHAR(50),
PRIMARY KEY (id)
)";
并删除最后一行的;
。