php - How to insert a single row to a Wordpress database table? -
intro:
i have function handles contact form variables in website.
- user enters name, email , text.
- function.php gets variables using javascript.
- i more information on user on way - ip, country, utm tags if any, ect.
then added code part saves variables respectively on sql table.
i created table 'wp_contact_form' using phpmyadmin.
used code part in function:
global $wpdb; $wpdb->insert( 'wp_contact_form', array( 'con_ip' => $_cookie['ip'], 'con_name' => $fullname, 'con_email' => $email, 'con_text' => $message, 'con_country' => $_cookie['country'], 'con_reigon' => $_cookie['region'], 'con_city' => $_cookie['city'], 'con_utm_source' => $_cookie['utm_source'], 'con_utm_medium' => $_cookie['utm_medium'], 'con_utm_campain' => $_cookie['utm_campaign'], 'con_utm_term' => $_cookie['utm_term'], 'con_utm_content' => $_cookie['utm_content'] ), array( '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' ) );
and still blank table. tried following this: https://codex.wordpress.org/class_reference/wpdb#insert_rows
without success.
db structure: http://i.stack.imgur.com/kq8oz.png
full function code:
/** contact form using ajax **/ add_action('wp_ajax_nopriv_submit_contact_form', 'submit_contact_form'); // send information contact form function submit_contact_form(){ // utm variables 'get_the_utm_vars()' function //$utm = get_the_utm_vars(); // if there $_post['email']... if( isset($_post['email']) && ($_post['validation'] == true ) ) { // parameters $email = $_post['email']; // gets email of user.. $email_to = "arik@ example.pro"; $utm_emails = array( 'tova@ example.pro', 'yonatan@ example.pro', 'arik@ example.pro', 'gal@ example.pro', 'shai@ example.pro', 'walid@ example.pro' ); $fullname = $_post['fullname']; $message = $_post['text']; $email_subject = " example intro: $email"; $headers = array( 'from: '. $fullname .' <'. $email .'>', 'bcc: yonatan@ example.pro', 'bcc: gal@ example.pro', 'bcc: eran@ example.pro', 'bcc: tova@ example.pro', 'bcc: walid@ example.pro', 'content-type: text/html; charset=\"utf-8\"; format=flowed \r\n' ); $utm_headers = array( 'from: '. $fullname .' <'. $email .'>' ); // send email yh, , if sent - do: if ( wp_mail($email_to,$email_subject,$message,$headers) ) { // tells me mail has been sent echo json_encode( array("result"=>"complete") ); //add utm variables emails text $message .= "\r\n \r\n \r\n ip: ". $_cookie['ip'] ."\r\n country: ". $_cookie['country'] ."\r\n region: ". $_cookie['region'] ."\r\n city: ". $_cookie['city'] ." \r\n utm source: ".$_cookie['utm_source']." \r\n utm medium: ".$_cookie['utm_medium']." \r\n utm campaign: ".$_cookie['utm_campaign']."\r\n utm term: ".$_cookie['utm_term']." \r\n utm content: ".$_cookie['utm_content']." "; // mail tova utm paramseters wp_mail($utm_emails,$email_subject,$message,$utm_headers); } else { echo json_encode(array("result"=>"mail_error")); var_dump($globals['phpmailer']->errorinfo); } wp_die(); } global $wpdb; $wpdb->insert( 'wp_contact_form', array( 'con_ip' => $_cookie['ip'], 'con_name' => $fullname, 'con_email' => $email, 'con_text' => $message, 'con_country' => $_cookie['country'], 'con_reigon' => $_cookie['region'], 'con_city' => $_cookie['city'], 'con_utm_source' => $_cookie['utm_source'], 'con_utm_medium' => $_cookie['utm_medium'], 'con_utm_campain' => $_cookie['utm_campaign'], 'con_utm_term' => $_cookie['utm_term'], 'con_utm_content' => $_cookie['utm_content'] ), array( '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' ) ); }
try this
global $wpdb; $table = wp_contact_form; $data = array( 'con_ip' => $_cookie['ip'], 'con_name' => $fullname, 'con_email' => $email, 'con_text' => $message, 'con_country' => $_cookie['country'], 'con_reigon' => $_cookie['region'], 'con_city' => $_cookie['city'], 'con_utm_source' => $_cookie['utm_source'], 'con_utm_medium' => $_cookie['utm_medium'], 'con_utm_campain' => $_cookie['utm_campaign'], 'con_utm_term' => $_cookie['utm_term'], 'con_utm_content' => $_cookie['utm_content'] ); $format = array( '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' ); $success=$wpdb->insert( $table, $data, $format ); if($success) { echo 'success'; } else { echo 'not success'; }
Comments
Post a Comment