简体   繁体   中英

WordPress plugin with database (mysql) query

I am trying to create a plugin and I met a little problem.

The plugin contains a simple admin page with checkboxes to perform functions. So I created a function that creates a new table when installing the plugin. The table contains 3 columns (ID, field, state):

global $wpdb;
$mnzwpc_table_prefix=$wpdb->prefix.'mnzwpc_';
define('MNZWPC_TABLE_PREFIX', $mnzwpc_table_prefix);

function mnzwpc_install() {
    global $wpdb;
    $table = MNZWPC_TABLE_PREFIX."settings";
    $structure = "CREATE TABLE $table (
        id INT(9) NOT NULL AUTO_INCREMENT,
        field VARCHAR(50) NOT NULL,
        state VARCHAR(50) NOT NULL,
        UNIQUE KEY id (id)
    )";
    $wpdb->query($structure);

    $wpdb->query("INSERT INTO $table(field,state)
        VALUES('setting','')
    ");
}

No problems here, the table is created when I install the plugin and removed when I uninstall.

My problem comes from my input".

I can not change the options in my table when a checkbox is checked and you click "Send".

Currently, I use this variables i created:

$my_setting = 'setting'; // Set the field name 

$hidden_field_name = 'submit_hidden'; // Save my input hidden in a var
$data_field_name = 'setting'; // Set the field name in DB

// If we click on "Save Changes", add the datas to my db
if ( isset( $_POST[ $hidden_field_name ] ) && $_POST[ $hidden_field_name ] == 'Y' ) {
    $opt_val = $_POST[ $data_field_name ];

    update_option( $my_setting, $opt_val );

?> <p><strong>Changes Saved.</strong></p>
<?php } // End if ?>

And this is my admin page:

    <form action="" method="post">
        <input type="hidden" name="<?php echo $hidden_field_name; ?>" value="Y">

        <label for="<?php echo $data_field_name; ?>">Setting name</label>
        <input id="<?php echo $data_field_name; ?>" type="checkbox" name="<?php echo $data_field_name; ?>" value="<?php echo $opt_val; ?>" />

        <hr />

        <p class="submit">
            <input type="submit" name="Submit" class="button-primary" value="<?php esc_attr_e('Save Changes') ?>" />
        </p>
    </form>

Of course, in my plugin code, I check the options and do the desired stuff:

function do_the_job() {

    // Variables for the field and option names
    $my_setting = 'setting';

    // Read in existing option value from database
    $opt_val = get_option( $my_setting );

    if ($opt_val != '1') {
        // Do something
    }
}

In my plugin admin page, if I check a checkbox and then I click on "Save Changes", the settings are not saved on the database and the checkbox appears unchecked. But the admin page is refreshed and my message appears: Changes Saved.

Any idea? I can upload my plugin on Github if you need the full code :/

Checkboxes get checked like this:

checked="checked"

So, you have to do something like this (I am using separate lines for readability):

<input id="<?php echo $data_field_name; ?>" 
       type="checkbox" 
       name="<?php echo $data_field_name; ?>" 
       value="<?php echo "the value to save in the database"; ?>" 
       <?php if($opt_val == "the value to save in the database") { ?>
       checked="checked"
       <?php } ?>
/>

You have another problem as well. Checkboxes upload if they are checked, but do not upload at all if they are not checked. How will you save it in your database if someone unchecks the checkbox? You have to have code that finds out whether a previously-checked checkbox is no longer submitted.

Yes indeed the "checked" value was not good on my input. I decided to use another method actually.

<input id="my_field"
       type="checkbox" 
       name="my_field" 
       value="1" 
       <?php checked( (bool) get_option( 'my_field' ) ); ?> 
/>

The "checked" is now stored in wp_options (option_value = 1) so I can verify the checked status with: if ( get_option( 'my_field' ) == true ) { // Do something }

And of course, is the user uncheck it, the option_value is removed.

I've rebuild all the plugin code and it works fine now. Of course, all my Database code was wrong. If I want to work on a new table, I have to use something like $wpdb->update and add my options in an array.

Thx for your help @spsc_tech :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM