WordPress.org

Ready to get started?Download WordPress

Forums

Custom WP Plugin Admin Panel Insert To Custom Table - Not Working (2 posts)

  1. -JFK-
    Member
    Posted 4 years ago #

    Hello. I have written custom Plugin for WordPress 2.9.2.

    I have created unique table in mySQL scheme called "leaders".

    I am able to read from the table with a widget I wrote and the following code:

    global $wpdb;
    $rows = $wpdb->get_results('SELECT leader_id, leader_title FROM leaders WHERE project_abbrev = '.$project_abbrev.' ORDER BY leader_order');

    I am writing custom plugin that has Admin Panel with forms to insert the data into the table (to then be read by the widget).

    For the life of me, I can't figure out why the INSERT statement in my plugin is NOT WORKING. Everything executes with NO PHP errors/warning, but no records show up in the mySQL table 'leaders'.

    Here is my code for INSERTing to table 'leaders' in Plugin:

    global $wpdb;
    
    $project_abbrev 	= $_POST[ 'project_abbrev' ];
    $leader_title 		= $_POST[ 'leader_title' ];
    $leader_price 		= $_POST[ 'leader_price' ];
    $leader_desc 		= $_POST[ 'leader_desc' ];
    $leader_order 		= $_POST[ 'leader_order' ];
    
    $result = $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->leaders ( project_abbbrev, leader_title, leader_price, leader_desc, leader_order ) VALUES ( %s, %s, %s, %s, %d )", 'Test Abbrev','Test Title','$39,900','THIS IS DESC',2) );

    I have also tried the following INSERT statements... all using $wpdb for the INSERTs:

    $wpdb->insert( 'leaders', array(
    	'project_abbbrev' => $project_abbrev,
    	'leader_title' => $leader_title,
    	'leader_price' => $leader_price,
    	'leader_desc' => $leader_desc,
    	'leader_order' => $leader_order ),
    	array( '%s', '%s', '%s', '%s', '%d' ) );
    
    $wpdb->query( $wpdb->prepare( "
    	INSERT INTO $wpdb->leaders
    	( project_abbbrev, leader_title, leader_price, leader_desc, order )
    	VALUES ( %s, %s, %s, %s, %d )",
    	array('Test Abbrev','Test Title','$39,900','THIS IS DESC',2) ) );

    Anyone got some ideas/suggestions. Thanks in advance!

    Been searching these forums and elsewhere without finding anything.

    -JFK-

  2. -JFK-
    Member
    Posted 4 years ago #

    OK, update...

    I got the following SQL call to work with $wpdb:

    $wpdb->query("INSERT INTO wordpressmu.leaders (project_abbrev,leader_title,leader_price,leader_desc,leader_order) VALUES ('ABBREV','TITLE','PRICE','DESC',45)");

    Don't know why the other 3 statements didn't work. I feel like my syntax was wrong or perhaps it has something to do with defining the table. Like, $wpdb->leaders doesn't work like 'wordpressmu.leaders'.

    Any thoughts????

Topic Closed

This topic has been closed to new replies.

About this Topic