首页 > 解决方案 > wordpress:在发送电子邮件时将字段保存到数据库 - 错误 500

问题描述

我正在尝试将提交表单的内容保存到数据库表中。但是当我提交表单时,我收到错误 500,但没有显示任何调试错误。如果我删除将数据插入表的行,则发送表单时不会出现任何错误。

这是代码:

function send_mail($fields)
{
    $subject = "New Quotation Form";
    $to = ["to@email.com"];

    extract($fields);
    ob_start();
    require 'email-template.php';
    $message = ob_get_contents();

    ob_end_clean();

    //insert quotation form contents to database table submitted_forms
    global $wbpd;
    $table = "submitted_forms";
    $data = array(
            'content' => $message,
            'datetime' => date("Y-m-d H:i:s"),
            'from_email' => $email
        );

    $ok = $wpdb->insert($table, $data); //it breaks here, if I remove it form is sent successfully

    if($wpdb->last_error !== '') :

        $str   = htmlspecialchars( $wpdb->last_error, ENT_QUOTES );
        $query = htmlspecialchars( $wpdb->last_query, ENT_QUOTES );

        print "<div id='error'>
        <p class='wpdberror'><strong>WordPress database error:</strong> [$str]<br />
        <code>$query</code></p>
        </div>";

    endif;

    $replyTo =   "Reply-To: ".$email;
    $headers = array(
        'Content-Type: text/html; charset=UTF-8',
        $replyTo
    );

    file_put_contents("mail.html", $message);

    $sent = wp_mail($to, $subject, $message, $headers);
    return $sent;
}

标签: phpmysqlwordpress

解决方案


你有一个错字:

global $wbpd; //global $wpdb;

推荐阅读