我正在构建一个简单的Wordpress插件来处理一些数据。对于用户,它以JSON格式返回数据;对于管理员,它允许通过管理面板扩展来操纵数据。我有非常简单的数据模式:
CREATE TABLE shops (
id int(11) NOT NULL AUTO_INCREMENT,
area VARCHAR(64) NOT NULL,
name VARCHAR(64) NOT NULL,
address VARCHAR(256),
tel VARCHAR(16),
zip VARCHAR(12),
website VARCHAR(128),
email VARCHAR(128),
UNIQUE KEY id (id)
);
CREATE TABLE ratings (
id int(11) NOT NULL AUTO_INCREMENT,
shop INT(11) NOT NULL,
rating INT(1),
UNIQUE KEY id (id)
);
我想选择一个商店列表,并将其平均评分放在每个商店旁边。选择查询是
SELECT
shops.id AS id,
shops.area AS area,
shops.name AS name,
shops.address AS address,
shops.tel AS tel,
shops.zip AS zip,
shops.website AS website,
shops.email AS email,
AVG(ratings.rating) AS rating
FROM shops AS shops
LEFT JOIN ratings AS ratings
ON shops.id = ratings.shop
GROUP BY shops.id;
所以,我也希望用户插入一些数据。正如WP Codex所表示的那样,我应该从POST请求查询数据中使用$wpdb->insert(...)
:
global $wpdb;
$wpdb->insert(
'shops',
array(
'name' => $_POST['name'],
'area' => $_POST['area'],
'address' => $_POST['address'],
'tel' => $_POST['tel'],
'zip' => $_POST['zip'],
'website' => $_POST['website'],
'email' => $_POST['email']
),
array('%s', '%s', '%s', '%s', '%s', '%s', '%s')
);
数据被插入,没有显示数据。我使用这样的结构:
$rows = $wpdb->get_results("
SELECT
shops.id AS id,
shops.area AS area,
shops.name AS name,
shops.address AS address,
shops.tel AS tel,
shops.zip AS zip,
shops.website AS website,
shops.email AS email,
AVG(ratings.rating) AS rating
FROM shops AS shops
LEFT JOIN ratings AS ratings
ON shops.id = ratings.shop
GROUP BY shops.id;
");
foreach ($rows as $row) {
echo '<tr>';
echo '<td>' . $row->id . '</td>';
echo '<td>' . $row->area . '</td>';
echo '<td>' . $row->name . '</td>';
echo '<td>' . $row->address . '</td>';
echo '<td>' . $row->tel . '</td>';
echo '<td>' . $row->zip . '</td>';
echo '<td>' . $row->website . '</td>';
echo '<td>' . $row->email . '</td>';
echo '<td>' . $row->rating . '</td>';
echo '<td><a href="' . admin_url('admin.php?page=rnr_admin_upd&id=' . $row->id) . '">Update</a></td>';
echo '</tr>';
}
答案 0 :(得分:0)
请检查格式如果是字符串,格式为%s
如果格式错误,请将其更改为array('%s', '%s', '%s', '%s', '%s', '%s', '%s')