我是初學者,希望將值從自定義模板頁面插入數據庫,但不存儲值。無法使用wordpress將值保存到數據庫中
這是我編輯的代碼,並添加了完整的模板代碼,我找不到爲什麼它不工作,從此
<?php /* Template Name: Contact */ ?>
<?php
function contact_form() {
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$comment = $_POST['comment'];
global $wpdb;
$table_name = $wpdb->prefix . "wp_contact";
$wpdb->insert($table_name, array(
'name' => $name,
'email' => $email,
'subject' => $subject,
'comment' => $comment
),array(
'%s',
'%s',
'%s',
'%s')
);
}
if(isset($_POST['submit'])) contact_form();
?>
<?php get_header(); ?>
<div class="container" id="contact">
<div class="row">
<h2>Contact us</h2>
<p>Lets get in touch and talk about your and our next project.</p><br>
<form action="" method="post">
<input type="text" placeholder="Name" required name="name">
<input type="text" placeholder="Email" required name="email">
<input type="text" placeholder="Subject" required name="subject">
<input type="text" placeholder="Comment" required name="comment">
<button name="submit" type="submit">
<i class="fa fa-paper-plane"></i> SEND MESSAGE
</button>
</form>
</div>
</div>
<?php get_footer(); ?>
提供完整的模板代碼。 –