我的wordpress自定义插件无法连接到数据库

时间:2019-11-04 18:35:21

标签: php mysql database wordpress

我正在Wordpress中创建一个自定义插件,该插件将允许用户插入一些信息。 我还在数据库中创建了一个名为“ Patientinfo”的表,以及它们的属性(全名,耐心,性别,生日,电话号码,注册日期)

单击提交按钮后,它仍然无法保存在数据库中。我缺少将其连接到数据库的什么代码?

这是我的代码的片段

<?php

global $wpdb;

?>
<h1>Enter New Patient's Data:</h1>
<form method="post" enctype="multipart/form-data">
  <b>Full name:
  <input type="text" name="fullname" required>
  <p>Patient's ID:
  <input type="text" name="patientid" required></p>
  <p>Gender: <input type="radio" name="gender" value="male" required> Male
  <input type="radio" name="gender" value="female"> Female</p>
  <p>Birthday:
  <input type="date" name="bday" required></p>
  <p>Phone number:
  <input type="text" name="phonenumber" required></p>
  <p>Registration date:
  <input type="date" name="regisdate" required></p>



  </b>
  <br>
  <input type="submit" name="submit" value="Submit">
</form>


<?php
if($wpdb->insert(
     'patientinfo',
     array(
        'fullname' => $fullname,
        'patientid' => $patientid,
        'gender' => $gender,
        'bday' => $bday,
        'phonenumber' => $phonenumber,
        'regisdate' => $regisdate
        )
 ) == false) wp_die('Database insertion failed');
 else echo 'Database insertion successful';

编辑:var_dump($ wpdp)给了我

object(wpdb)#3 (62) { ["show_errors"]=> bool(false) ["suppress_errors"]=> 
bool(false) ["last_error"]=> string(0) "" ["num_queries"]=> int(19) 
["num_rows"]=> int(0) ["rows_affected"]=> int(0) ["insert_id"]=> int(390) 
["last_query"]=> string(89) "SELECT option_value FROM wp_options WHERE 
option_name = 'auto_core_update_failed' LIMIT 1" ["last_result"]=> array(0) 
{ } ["result":protected]=> object(mysqli_result)#677 (5) { 
["current_field"]=> int(0) ["field_count"]=> int(1) ["lengths"]=> NULL 
["num_rows"]=> int(0) ["type"]=> int(0) } ["col_meta":protected]=> array(0) 
{ } ["table_charset":protected]=> array(0) { } 
["check_current_query":protected]=> bool(true) 
["checking_collation":"wpdb":private]=> bool(false) ["col_info":protected]=> 
NULL ["queries"]=> NULL ["reconnect_retries":protected]=> int(5) 
["prefix"]=> string(3) "wp_" ["base_prefix"]=> string(3) "wp_" ["ready"]=> 
bool(true) ["blogid"]=> int(0) ["siteid"]=> int(0) ["tables"]=> array(10) { 
[0]=> string(5) "posts" [1]=> string(8) "comments" [2]=> string(5) "links" 
[3]=> string(7) "options" [4]=> string(8) "postmeta" [5]=> string(5) "terms" 
[6]=> string(13) "term_taxonomy" [7]=> string(18) "term_relationships" [8]=> 
string(8) "termmeta" [9]=> string(11) "commentmeta" } ["old_tables"]=> 
array(3) { [0]=> string(10) "categories" [1]=> string(8) "post2cat" [2]=> 
string(8) "link2cat" } ["global_tables"]=> array(2) { [0]=> string(5) 
"users" [1]=> string(8) "usermeta" } ["ms_global_tables"]=> array(8) { [0]=> 
string(5) "blogs" [1]=> string(8) "blogmeta" [2]=> string(7) "signups" [3]=> 
string(4) "site" [4]=> string(8) "sitemeta" [5]=> string(14) 
"sitecategories" [6]=> string(16) "registration_log" [7]=> string(13) 
"blog_versions" } ["comments"]=> string(11) "wp_comments" ["commentmeta"]=> 
string(14) "wp_commentmeta" ["links"]=> string(8) "wp_links" ["options"]=> 
string(10) "wp_options" ["postmeta"]=> string(11) "wp_postmeta" ["posts"]=> 
string(8) "wp_posts" ["terms"]=> string(8) "wp_terms" 
["term_relationships"]=> string(21) "wp_term_relationships" 
["term_taxonomy"]=> string(16) "wp_term_taxonomy" ["termmeta"]=> string(11) 
"wp_termmeta" ["usermeta"]=> string(11) "wp_usermeta" ["users"]=> string(8) 
"wp_users" ["blogs"]=> NULL ["blogmeta"]=> NULL ["blog_versions"]=> NULL 
["registration_log"]=> NULL ["signups"]=> NULL ["site"]=> NULL 
["sitecategories"]=> NULL ["sitemeta"]=> NULL ["field_types"]=> array(34) { 
["post_author"]=> string(2) "%d" ["post_parent"]=> string(2) "%d" 
["menu_order"]=> string(2) "%d" ["term_id"]=> string(2) "%d" 
["term_group"]=> string(2) "%d" ["term_taxonomy_id"]=> string(2) "%d" 
["parent"]=> string(2) "%d" ["count"]=> string(2) "%d" ["object_id"]=> 
string(2) "%d" ["term_order"]=> string(2) "%d" ["ID"]=> string(2) "%d" 
["comment_ID"]=> string(2) "%d" ["comment_post_ID"]=> string(2) "%d" 
["comment_parent"]=> string(2) "%d" ["user_id"]=> string(2) "%d" 
["link_id"]=> string(2) "%d" ["link_owner"]=> string(2) "%d" 
["link_rating"]=> string(2) "%d" ["option_id"]=> string(2) "%d" 
["blog_id"]=> string(2) "%d" ["meta_id"]=> string(2) "%d" ["post_id"]=> 
string(2) "%d" ["user_status"]=> string(2) "%d" ["umeta_id"]=> string(2) 
"%d" ["comment_karma"]=> string(2) "%d" ["comment_count"]=> string(2) "%d" 
["active"]=> string(2) "%d" ["cat_id"]=> string(2) "%d" ["deleted"]=> 
string(2) "%d" ["lang_id"]=> string(2) "%d" ["mature"]=> string(2) "%d" 
["public"]=> string(2) "%d" ["site_id"]=> string(2) "%d" ["spam"]=> 
string(2) "%d" } ["charset"]=> string(7) "utf8mb4" ["collate"]=> string(22) 
"utf8mb4_unicode_520_ci" ["dbuser":protected]=> string(4) "root" 
["dbpassword":protected]=> string(9) "sintia123" ["dbname":protected]=> 
string(9) "wordpress" ["dbhost":protected]=> string(9) "localhost" 
["dbh":protected]=> object(mysqli)#4 (19) { ["affected_rows"]=> int(0) 
["client_info"]=> string(79) "mysqlnd 5.0.11-dev - 20120503 - $Id: 
76b08b24596e12d4553bd41fc93cccd5bac2fe7a $" ["client_version"]=> int(50011) 
["connect_errno"]=> int(0) ["connect_error"]=> NULL ["errno"]=> int(0) 
["error"]=> string(0) "" ["error_list"]=> array(0) { } ["field_count"]=> 
int(1) ["host_info"]=> string(20) "localhost via TCP/IP" ["info"]=> NULL 
["insert_id"]=> int(0) ["server_info"]=> string(10) "5.7.17-log" 
["server_version"]=> int(50717) ["stat"]=> string(139) "Uptime: 371007 
Threads: 1 Questions: 15874 Slow queries: 0 Opens: 399 Flush tables: 1 Open 
tables: 392 Queries per second avg: 0.042" ["sqlstate"]=> string(5) "00000" 
["protocol_version"]=> int(10) ["thread_id"]=> int(984) ["warning_count"]=> 
int(0) } ["func_call"]=> string(103) "$db->query("SELECT option_value FROM 
wp_options WHERE option_name = 'auto_core_update_failed' LIMIT 1")" 
["is_mysql"]=> bool(true) ["incompatible_modes":protected]=> array(5) { 
[0]=> string(12) "NO_ZERO_DATE" [1]=> string(18) "ONLY_FULL_GROUP_BY" [2]=> 
string(19) "STRICT_TRANS_TABLES" [3]=> string(17) "STRICT_ALL_TABLES" [4]=> 
string(11) "TRADITIONAL" } ["use_mysqli":"wpdb":private]=> bool(true) 
["has_connected":"wpdb":private]=> bool(true) ["categories"]=> string(13) 
"wp_categories" ["post2cat"]=> string(11) "wp_post2cat" ["link2cat"]=> 
string(11) "wp_link2cat" }

1 个答案:

答案 0 :(得分:1)

出现任何错误吗?

应该更像这样:

$wpdb->insert(
 'patientinfo',
 array(
    'fullname' => $_POST['fullname'],
    'patientid' => $_POST['patientid'],
    'gender' => $_POST['gender'],
    'bday' => $_POST['bday'],
    'phonenumber' => $_POST['phonenumber'],
    'regisdate' => $_POST['regisdate']
    )
 )

然后再次检查表名和列名。