我目前正在开发一个 WordPress 插件,并且已经使用我的插件创建了一个表格,样式如下:
$sql = "CREATE TABLE IF NOT EXISTS $table_name (
id INTEGER NOT NULL AUTO_INCREMENT,
post_id INTEGER NOT NULL,
lat FLOAT NOT NULL,
lng FLOAT NOT NULL,
str VARCHAR(55),
plz INTEGER,
ort VARCHAR(20),
UNIQUE KEY id (id) )
我可以通过“MySQL查询浏览器”看到数据库存在...
并希望使用以下代码将一些信息插入表中:
(str、plz、ort 还不是必需的,所以我想让它们为空...)
而且我相信每次插入内容时 id 都会自动增加,即使这样我也不太确定我是否需要 id 作为键,因为每个帖子都只有一个条目,所以 post_id 可以也是关键...
function install_data($latlng, $post_id) {
global $wpdb;
if ( ! $wpdb->update($table_name, array('lat' => $latlng[0], 'lng' => $latlng[1]), array('post_id' => $post_id)))
{
$wpdb->insert( $table_name, array('post_id' => $post_id, 'lat' => $latlng[0], 'lng' => $latlng[1]));
}
$wpdb->show_error();
$wpdb->print_error();
}
我尝试了这两个错误..但是没有在控制台中看到一个...也没有在错误日志中看到...
我也试过再次删除更新功能,但仍然没有任何帮助......
好吧..
这有点奇怪,但我解决了一半 xD
function install_data($latlng, $post_id) {
global $wpdb;
require_once( ABSPATH . 'wp-config.php');
if ( ! $wpdb->update($table_name, array('lat' => $latlng[0], 'lng' => $latlng[1]), array('post_id' => $post_id)))
{
$wpdb->insert( $table_name, array('post_id' => $post_id, 'lat' => $latlng[0], 'lng' => $latlng[1]));
}
$wpdb->show_error();
$wpdb->print_error();
}
现在只有更新不工作了...
它又停止工作了……我只是不知道为什么…… 读取也不起作用......
function read_data($post_id) {
global $wpdb;
require_once( ABSPATH . 'wp-config.php'); //Just a try, cause insert worked afterwards... for a short time -.-
$latlng = $wpdb->get_results( "SELECT lat, lng FROM $table_name WHERE post_id = '$post_id'" );
return $latlng;
}
最佳答案
function socialMediaFacebook(){
require_once(ABSPATH .'/wp-load.php');
require_once(ABSPATH .'/wp-config.php');
global $wpdb;
$idvideonova = 0;
$app_id = $_POST['app_id'];
$app_link = $_POST['app_link'];
$app_caption = $_POST['app_caption'];
$app_description = $_POST['app_description'];
$table_name = $wpdb->prefix . "tableBloo;
$rows_affected = $wpdb->insert( $table_name, array('id' => $idvideonova, 'app_id' => $app_id, 'app_link' => $app_link, 'app_caption' => $app_caption, 'app_description' => $app_description) );
$lastid = $wpdb->insert_id;
echo " <script type=\"text/javascript\">
alert('Social Media Settings Saved');
</script>
";
我也有过这样的经历..呵呵试着 echo 找到你在哪里。或使用我的代码..帖子是文本框的名称.. 选项卡
关于php - WordPress wpdb->插入/更新不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18612934/