Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql wpdb->;使用prepare()插入自定义wp表_Mysql_Insert_Wordpress_Prepare - Fatal编程技术网

Mysql wpdb->;使用prepare()插入自定义wp表

Mysql wpdb->;使用prepare()插入自定义wp表,mysql,insert,wordpress,prepare,Mysql,Insert,Wordpress,Prepare,在Wordpress中处理时间系统时,在插入到自定义表时,wp insert遇到了一些奇怪的问题 我正在我的functions.php中运行以下函数 function clock_in() { if ( isset( $_POST['punch-in'] ) && '1' == $_POST['punch-in'] ) { $user_id = get_current_user_id(); global $wpdb; $wpdb->query(

在Wordpress中处理时间系统时,在插入到自定义表时,wp insert遇到了一些奇怪的问题

我正在我的functions.php中运行以下函数

function clock_in() {

if ( isset( $_POST['punch-in'] ) && '1' == $_POST['punch-in'] ) {

    $user_id = get_current_user_id();

    global $wpdb;
    $wpdb->query( $wpdb->prepare(
                    "
                        INSERT INTO $wpdb->wp_diss_users_sessions
                        (session_id,session_begin)
                        VALUES ( %d, %s )
                    ", 
                        $user_id,'test' 

) );
我已经使用对wp_users表的更新查询测试了该函数,以验证该函数是否按照需要正确启动

我收到的错误如下:

WordPress database error: [You have an error in your SQL syntax;
check the manual that     corresponds to your MySQL server version 
for the right syntax to use near 
'(session_id,session_begin) VALUES ( 2, 'test' )' at line 2]
INSERT INTO (session_id,session_begin) VALUES ( 2, 'test' )
我的桌子是这样设计的:

[session_id] - INT(11) PK NN
[session_begin] - VARCHAR(45)
我知道最好的做法是使用prepare,所以我尝试适当地构建所有查询。我相信$wpdb->wp\u diss\u users\u sessions=引用我的自定义表的语法是正确的。也许我忽略了一些简单的事情,但今晚我的头很痛


谢谢大家

由于查询中没有表名,因此出现错误:

INSERT INTO (session_id,session_begin) VALUES ( 2, 'test' )
            ^ here should be a table name
仅使用
$wpdb->wp_diss_users_sessions
将不起作用,您的表名将不会神奇地出现在wpdb对象中。你必须自己把它储存在那里

在中,建议将其添加到插件/主题的函数中:

$wpdb->myplugintable = $table_prefix . 'myplugintable';
如果将其放在函数中,请不要忘记使用
全局
关键字将
$table_前缀
拉入其作用域


之后,您可以按照您想要的方式访问表名。

查询中缺少表名。您可能需要阅读以下内容:这基本上是相同的问题。