0

Recently I have started to make a WordPress plugin what will work with MySQL database in WordPress. But, some of the bugs are preventing me from earning success with this subject. Actually, I want to make a unique table on database what will have only usernames and passwords to log in on this site.

First, My Plugin will make a table on the database on the First Visit of the plugin's Admin Panel. I have tried this code on for SQL and it works fine.

function jal_install() {
global $wpdb;
$table_name = $wpdb->prefix . "mydb"; // so the table name will be similar as: wp_mydb

$sql = "CREATE TABLE IF NOT EXISTS $table_name (
id INT( 10 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
u_name VARCHAR( 100 ) NOT NULL ,
p_word VARCHAR( 100 ) NOT NULL ,
UNIQUE (
u_name
)
) ENGINE = MYISAM ;";

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

   add_option( "jal_db_version", $jal_db_version );
}
jal_install();

Now, The admin panel page of the plugin has a form to upload data via Excel file(.xls). I used excel reader for this code and it works fine. First, see the code:-

$path  = plugin_dir_path( __FILE__); 
$path .= 'upload'; 
$data = new Spreadsheet_Excel_Reader($path . "/" . $_FILES["xlsfile"]["name"]);
$rows_c = $data->rowcount($sheet_index=0);
$rows_c = $rows_c + 1;
$table_name = $wpdb->prefix . "mydb"; // it will look like -> wp_mydb with wp_ prefix
echo "<br />Data are added:-<br />";
for ($i = 1; $i < $rows_c; $i++) {
    $u1nam2e = $data->val($i,'A'); // Should be a username like: myuname, uname_1 etc.
    $p1wor2d = $data->val($i,'B'); // Should be a password like: mypword, asdfghjk!123 etc.
    echo "=>When username-><b>" . $u1nam2e . "</b> Then Password-><b>" . $p1wor2d . "</b>;<br />";
    $sql = "INSERT INTO excel_db (u_name,p_word) 
        VALUES ('$u1nam2e','$p1wor2d')";
    $wpdb->query($sql);
}

When I run the code on my plugin's admin page, it shows something like this:-

Fatal error: Call to a member function query() on a non-object in C:\xampp\htdocs.......\my plugin's path\admin_panel.php on line XX

I can't see any data added to the database.

Now, Please anyone help me how can I add data successfully on the database with the code above?

3
  • is it safe to assume that line XX is the one in the above code that reads $wpdb->query($sql);? Commented Mar 27, 2013 at 19:55
  • No. It isn't. But the code is bigger then i wrote. Commented Mar 28, 2013 at 6:45
  • Super useful to not include the code that the error refers too.. Commented Mar 17, 2014 at 20:26

1 Answer 1

1

I have found my answer. Actually I have to write

global $wpdb;

code before calling the $wpdb array. So, The code will be:-

$path  = plugin_dir_path( __FILE__); 
$path .= 'upload'; 
$data = new Spreadsheet_Excel_Reader($path . "/" . $_FILES["xlsfile"]["name"]);
$rows_c = $data->rowcount($sheet_index=0);
$rows_c = $rows_c + 1;

$table_name = $wpdb->prefix . "mydb"; // it will look like -> wp_mydb with wp_ prefix
echo "<br />Data are added:-<br />";
global $wpdb;
for ($i = 1; $i < $rows_c; $i++) {
    $u1nam2e = $data->val($i,'A'); // Should be a username like: myuname, uname_1 etc.
    $p1wor2d = $data->val($i,'B'); // Should be a password like: mypword, asdfghjk!123 etc.
    echo "=>When username-><b>" . $u1nam2e . "</b> Then Password-><b>" . $p1wor2d . "</b>;<br />";
    $sql = "INSERT INTO excel_db (u_name,p_word) 
        VALUES ('$u1nam2e','$p1wor2d')";
    $wpdb->query($sql);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.