一直在使用表單。我想將表單中的信息添加到數據庫中,以便我可以在網站上的其他日曆中顯示它。如何將多條記錄添加到我的數據庫中
我在網上發現了一些其他代碼,但只適用於1值。由於某種原因只有一次。 現在有這個(在WordPress工作BTW)
function elh_insert_into_db() {
global $wpdb;
// creates my_table in database if not exists
$table = $wpdb->prefix . "table_form";
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE IF NOT EXISTS $table (
`id` mediumint(9) NOT NULL AUTO_INCREMENT,
`name` text NOT NULL,
`email` text NOT NULL,
`date` text NOT NULL,
`starttijd` text NOT NULL,
`eindtijd` text NOT NULL,
`opmerkingen` text NOT NULL,
UNIQUE (`id`)
) $charset_collate;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
// starts output buffering
ob_start();
?>
<form action="#v_form" method="post" id="v_form">
<label for="visitor_name"><h3>Naam:</h3></label>
<input type="text" name="visitor_name" id="visitor_name" />
<label for="visitor_email"><h3>E-mail:</h3></label>
<input type="email" name="visitor_email" id="visitor_email" />
<label for="visitor_date"><h3>Datum:</h3></label>
<input type="date" name="visitor_date" id="visitor_date" />
<label for="visitor_start_time"><h3>Starttijd:</h3></label>
<input type="time" name="visitor_start_time" id="visitor_start_time" />
<label for="visitor_end_time"><h3>Eindtijd:</h3></label>
<input type="time" name="visitor_end_time" id="visitor_end_time" />
<label for="visitor_text"><h3>Opmerkingen:</h3></label>
<input type="text" name="visitor_text" id="visitor_text" />
<input type="submit" name="submit_form" value="Aanvragen" />
</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"] != "") {
$table = $wpdb->prefix."table_form";
$name = strip_tags($_POST["visitor_name"], "");
$wpdb->insert(
$table,
array(
'name' => $name
)
);
$html = "<p>check this is inserted in the database <strong>$name, $email, $date , $starttijd,
$eindtijd, $opmerkingen</strong> what a succes!</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');
所以這是工作,或者它不工作? – tadman
現在不工作 –
什麼是不工作?它應該做什麼,它究竟在做什麼? – tadman