我已经写了一个小帖子的观点插件,以及至少开始了一个。
我的表格已设置,但当它们更新时,代码会在加载帖子和跟踪视图时向我的表格中添加2个条目。我真的不知道为什么会这样。看起来输入DB表的第二个post id是加载的post之后的post。示例:如果正在查看的帖子id=121,则代码将添加帖子121和124。
这是我的插入代码:
if( is_single() && !is_page() ):
// add a new data row into the views DB table with the post ID
$wpdb->query("INSERT INTO {$ppbv} (post_id, post_type) VALUES (\'{$post->ID}\',\'{$post->post_type}\');");
$data = $wpdb->get_row("SELECT * FROM {$ppbv_total} WHERE post_id=\'{$post->ID}\'", ARRAY_A); // get the data row that has the matching post ID
if(!is_null($data)): // if we have a matching data row
$new_views = $data[\'views\'] + 1; // increase the views by 1
// update the data row with the new views
$wpdb->query("UPDATE {$ppbv_total} SET views=\'{$new_views}\' WHERE post_id=\'{$post->ID}\';");
else: // if we don\'t have a matching data row (nobody\'s viewed the post yet)
// add a new data row into the DB with the post ID and 1 view
$wpdb->query("INSERT INTO {$ppbv_total} (post_id, post_type, views) VALUES (\'{$post->ID}\',\'{$post->post_type}\',\'1\');");
endif;
endif;
如果有人能解释一下,我将不胜感激!
SO网友:ANKUSH POFALE
$location = $_SERVER[‘DOCUMENT_ROOT’];
include ( $location . \'/wp-config.php\' );
include ( $location . \'/wp-load.php\' );
global $wpdb;
$contactus_table = $wpdb->prefix."user";
if( isset( $_POST[\'submit\'] ) ) {
$name=$_POST[\'name\'];
$email=$_POST[\'email\'];
$phone=$_POST[\'phone\'];
$message=$_POST[\'message\'];
$sql = "INSERT INTO $contactus_table
(`name`,`email`,`phone`,`message`)VALUES
(\'$name\',\'$email\',\'$phone\',\'$message\')";
if( $wpdb->query( $sql ) ) {
echo "data is inserted";
}
}