Многостолбцовый дб вставить форму - PullRequest
0 голосов
/ 18 октября 2018

Я вставляю данные формы в базу данных.

Моя проблема в том, что заполняется только один столбец, в то время как 2-й столбец остается пустым (допускается ноль).

С помощью следующего кода я могу вставить данные в wp_my_table, но только в 1 столбец,Что я делаю не так?

Также, как мне вставить Datepicker для выбора некоторых значений из выпадающего меню?

function elh_insert_into_db() {

global $wpdb;


// creates my_table in database if not exists
$table = $wpdb->prefix . "my_table"; 
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE IF NOT EXISTS $table (
    `id` mediumint(9) NOT NULL AUTO_INCREMENT,
    `name` text NOT NULL,
`Desc` text NULL,
UNIQUE (`id`)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
// starts output buffering
ob_start();

?>
<form action="" method="post" id="v_form">
    <label for="visitor_name"><h3>Hello there! What is your name?</h3> 
</label>
    <input type="text" name="visitor_name" id="visitor_name" value="" />
    <input type="text" name="Desc" id="Desc" value="?php echo $name;?"/>
<input type="submit" name="submit_form" value="submit" />
</form>

<?php
$html = ob_get_clean();
// does the inserting, in case the form is filled and submitted
if ( isset( $_POST["submit_form"] ) && $_POST["visitor_name"] != "" ) {
   global $wpdb;
 $table = $wpdb->prefix."my_table";

 $data=array(
'name'=> $_POST['visitor_name'] ,
'Desc'=> $_POST['Desc']
 );

$wpdb->insert($table,$data);



$wpdb->show_errors();
$wpdb->print_error();

    $html = "<p>Your name <strong>$name</strong> was successfully recorded. 
Thanks!!</p>";
}
// if the form is submitted but the name is empty
if ( isset( $_POST["submit_form"] ) && $_POST["visitor_name"] == "" )
    $html .= "<p>You need to fill the required fields.</p>";
// outputs everything
return $html;

}
// adds a shortcode you can use: [insert-into-db]
add_shortcode('elh-db-insert', 'elh_insert_into_db');
...