mysql - wpdb->insert into custom wp table using prepare() -



mysql - wpdb->insert into custom wp table using prepare() -

working on time scheme in wordpress , run unusual problem wp insert when inserting custom table.

i running next function in 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 $wpdb->wp_diss_users_sessions (session_id,session_begin) values ( %d, %s ) ", $user_id,'test' ) );

i've tested function update query wp_users table verify function firing appropriately desired.

the error receiving follows:

wordpress database error: [you have error in sql syntax; check manual corresponds mysql server version right syntax utilize near '(session_id,session_begin) values ( 2, 'test' )' @ line 2] insert (session_id,session_begin) values ( 2, 'test' )

my table designed such:

[session_id] - int(11) pk nn [session_begin] - varchar(45)

i understand best practice utilize prepare trying build of queries appropriately. believe $wpdb->wp_diss_users_sessions = right syntax reference custom table researching. maybe i'm overlooking simple head hurts tonight.

thanks all

you error because don't have table name in query:

insert (session_id,session_begin) values ( 2, 'test' ) ^ here should table name

just using $wpdb->wp_diss_users_sessions not work, table name not appear magically within wpdb object. have store there yourself.

in this thread in wordpress back upwards forum suggested add together in function of plugin/theme:

$wpdb->myplugintable = $table_prefix . 'myplugintable';

if place in function, don't forget pull $table_prefix within scope global keyword.

afterwards can access table name in manner intend.

mysql wordpress insert wpdb prepare

Comments

Popular posts from this blog

model view controller - MVC Rails Planning -

ruby on rails - Devise Logout Error in RoR -

html - Submenu setup with jquery and effect 'fold' -