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?
$wpdb->query($sql);?