安全地将数据插入WPDB的示例。它是防止sql注入的预处理方法。首先创建表单,然后按照以下结构插入数据。Example:
$insert_value_array = array(
\'input\' => \'\', // name of your input field
\'textarea\' => \'\', // field name
\'select\' => \'\', // field name
\'checkbox\' => \'\', // field name
);
$form_item = shortcode_atts( $insert_value_array, $_REQUEST );
if(isset($_POST[\'submit\'])){
global $wpdb;
$full_table_name = $wpdb->prefix . \'your_table_name\';
// $wpdb->insert( $full_table_name, $form_item); // It\'s without prepare method
// Safe data insertion with prepare method ( successful )
$wpdb->query( $wpdb->prepare(
" INSERT INTO $full_table_name
( column_name_1, column_name_2, column_name_3, column_name_4 )
VALUES ( %s, %s, %s, %s )
", // VALUES depends on your datatype. %s for string, %d for integer
$form_item
) );
};