首先,我在wpdb中创建了一个自定义表:
global $wpdb;
$table_name = $wpdb->prefix . \'countdown_table\';
$sql = \'
`id` int(11) NOT NULL AUTO_INCREMENT,
`styleName` varchar(255) NOT NULL,
`css` blob NOT NULL,
`expiration` blob NOT NULL,
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
\';
require_once( ABSPATH . \'wp-admin/includes/upgrade.php\' );
dbDelta( $sql );
然后,我创建了一个表单,将值发送到自定义表中:
<p>Style Name</p>
<form action="" method="POST">
<input type="text" name="style_name">
<input type="submit" name="submit">
</form>
if (isset($_POST[\'style_name\'])) {
$style_name = $_POST[\'style_name\'];
global $wpdb;
$wpdb->insert(
\'countdown_table\',
array(
\'styleName\' => $style_name
),
array(
\'%s\'
)
);
}
下面是从表中获取结果的问题:
global $wpdb;
$output = \'\';
$table_name = $wpdb->prefix . \'countdown_table\';
$name = \'styleName\';
$results = $wpdb->get_results( $wpdb->prepare(\'SELECT * FROM \'.$table_name ) );
if ( $results ) {
foreach ( $results as $result ){
echo $result[0];
}
问题是我无法显示保存的数据,或者我是否在创建表或插入数据的代码中出错