【问题标题】:How to insert a single row to a Wordpress database table?如何在 Wordpress 数据库表中插入一行?
【发布时间】:2015-10-28 03:09:03
【问题描述】:

简介:

我有一个函数可以处理我网站中的联系表单变量。

  • 用户输入姓名、电子邮件和文本。
  • function.php 使用 javascript 获取变量。
  • 我在途中获得了有关用户的更多信息 - 例如 IP、国家、UTM 标签(如果有)等。

然后我添加了一个代码部分,将变量分别保存在 SQL 表中。

我使用 phpMyAdmin 创建了表“wp_contact_form”。

在函数中使用了这个代码部分:

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'
    ) 
);

我仍然得到一张空白表。 我试着遵循这个: https://codex.wordpress.org/Class_Reference/wpdb#INSERT_rows

没有任何成功。

数据库结构: http://i.stack.imgur.com/kQ8oZ.png

完整功能代码:

/**
    Contact form using Ajax 
**/ 

add_action('wp_ajax_nopriv_submit_contact_form', 'submit_contact_form'); 

// Send information from the contact form 
function submit_contact_form(){

    // Get the UTM variables from the 'get_the_utm_vars()' function
    //$utm = get_the_utm_vars();

    // If there is a $_POST['email']...
    if( isset($_POST['email']) && ($_POST['validation'] == true ) ) {

        // Get parameters
        $email = $_POST['email']; // Gets the email of the 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 to YH, and if sent - do:
        if ( wp_mail($email_to,$email_subject,$message,$headers) ) {

            // Tells me that the mail has been sent
            echo json_encode( array("result"=>"complete") );

            //Add the UTM variables to the 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']." ";
            // A mail for tova with the 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'
        ) 
    );







}

【问题讨论】:

    标签: php database function wordpress


    【解决方案1】:

    试试这个

         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';
    
    }
    

    【讨论】:

    • 不应该$table = wp_contact_form;$table = "wp_contact_form"; 吗?
    【解决方案2】:

    根据我的评论,我们插入的字段越多,我们必须使用那么多 %s 。

    但是您使用的是 9 格式 %s,并且您要插入 12 个值。

    【讨论】:

      猜你喜欢
      • 2017-03-17
      • 1970-01-01
      • 1970-01-01
      • 2014-03-24
      • 2016-11-22
      • 1970-01-01
      • 1970-01-01
      • 2013-10-25
      • 1970-01-01
      相关资源
      最近更新 更多