Wordpress从表单插入数据不起作用

时间:2013-05-08 11:48:55

标签: wordpress-plugin wordpress

function db_install() {
   global $wpdb;
   global $contact_form_db_version;
    global $table_name;
   $table_name = $wpdb->prefix . "FantasticContactForm";

   $sql = "CREATE TABLE $table_name (
  id mediumint(9) NOT NULL AUTO_INCREMENT,
  time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
  name tinytext NOT NULL,
  email text NOT NULL,
  subject text NOT NULL,
  url VARCHAR(55) DEFAULT '' NOT NULL,
  ip VARCHAR(40),
  message text NOT NULL,
  UNIQUE KEY id (id)
    );";

   require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
   dbDelta( $sql );

   add_option( "contact_form_db_version", $contact_form_db_version );
}

function db_install_data() {


       global $table_name;
      global $welcome_name;
      $welcome_name = $_POST['fname'];
      global $welcome_text;
      $welcome_text = $_POST['message'];
       global $welcome_url;
       $welcome_url = $_POST['url'];
       global $welcome_subject;
       $welcome_subject = $_POST['subject'];
      global  $welcome_mail;
      $welcome_mail = $_POST['email'] ;
       $admin_mail = get_option('admin_email');
       global $ip;
       $ip = getenv('REMOTE_ADDR');
    /*$rows_affected = $wpdb->insert( $table_name, array( 'time' => current_time('mysql'), 'name' => $welcome_name, 'email' => $welcome_mail, 'url' => $welcome_url, 'ip' => $ip, 'message' => $welcome_text, 'subject' => $welcome_subject ) );*/


       $header="from: $welcome_name <$welcome_mail>";
       FantasticContactForm::insert_data_table();
      /* $send_contact=mail($admin_mail,$welcome_subject,$welcome_text,$header);
       if($send_contact){
            echo "Thanks for Contacting us. We Will Get Back You Soon";

        }
        else {
            echo "ERROR";
        }*/
    }
    function insert_data_table() {
    require_once( ABSPATH . 'wp-load.php' );
     global $wpdb,$welcome_name,$welcome_text,$welcome_url,$welcome_subject,$welcome_mail,$ip,$table_name;
     $insert =    $wpdb->query("INSERT INTO $table (time, name,email,url,ip,message,subject) VALUES (current_time('mysql'),$welcome_name,$welcome_mail,$welcome_url,$ip,$welcome_text,$welcome_subject)");
    if($insert) {

    echo "Success";

    }
    else {
    echo "fail";
    }
    }

大家好我创建了简单的联系表格。我无法将该数据插入wordpress数据库。这是我尝试过的以下代码。我不知道存储的确切原因。 我google了很多。我仍然无法存储输入数据。

谢谢, 维基

1 个答案:

答案 0 :(得分:-1)

为什么你不使用wordpress帖子来存储这些所有数据,因为它易于使用和处理。