Php WordPressMySQL错误
我是这里的初学者 我需要订阅邮件 因此,我使用php和mysql创建了这个表单Php WordPressMySQL错误,php,mysql,wordpress,Php,Mysql,Wordpress,我是这里的初学者 我需要订阅邮件 因此,我使用php和mysql创建了这个表单 <?php /* Template name: mail form */ // if using a custom function, you need this $Email = $_POST['email-form']; $firstn = $_POST['First-name']; global $wpdb; $table_name = $wpdb->pre
<?php
/*
Template name: mail form
*/
// if using a custom function, you need this
$Email = $_POST['email-form'];
$firstn = $_POST['First-name'];
global $wpdb;
$table_name = $wpdb->prefix . "new_mail_form";
$wpdb->insert( $table_name, array( 'email' => $Email, 'name' => $firstn ) )
?>
<form method="post" action="#" enctype="multipart/form-data" id="Submitform" >
<fieldset class="email-form">
<label for="email-form">:</label>
<input type="text" name="email-form" id="email-form" placeholder="Enter Your Email here" />
</fieldset>
<fieldset class="First-name">
<label for="First-name">:</label>
<input type="text" name="First-name" id="First-name" placeholder="Enter Your Name here" />
</fieldset>
< input type="submit" value="Publish Post" tabindex="40" id="submit" name="submit" class="submitbutton" />
</form>
:
:
但问题是,在每次页面刷新时,它都会在我的sql中添加一个条目
因此,当有人加载页面时,它会自动添加空白条目
我怎样才能避免这样做?/这是因为您没有检查表单是否先提交。试试这个:
if (!empty($_POST)) {
$Email = $_POST['email-form'];
$firstn = $_POST['First-name'];
global $wpdb;
$table_name = $wpdb -> prefix . "new_mail_form";
$wpdb -> insert($table_name, array('email' => $Email, 'name' => $firstn));
}