PHP-如何停止循环以注册两次数据?
我在这里面临一个问题 下面的代码在我的网站上运行,当我填写表单并检查数据库时,数据已经注册了两次 请从my functions.php文件中下载以下php代码PHP-如何停止循环以注册两次数据?,php,mysql,wordpress,forms,Php,Mysql,Wordpress,Forms,我在这里面临一个问题 下面的代码在我的网站上运行,当我填写表单并检查数据库时,数据已经注册了两次 请从my functions.php文件中下载以下php代码 function map_location_report_form() { global $wpdb; $this_page = $_SERVER['REQUEST_URI']; $page = $_POST['page']; if ( $page == NULL ) {
function map_location_report_form()
{
global $wpdb;
$this_page = $_SERVER['REQUEST_URI'];
$page = $_POST['page'];
if ( $page == NULL )
{
echo '<form method="post" action="' . $this_page .'">
<div class="formfield-report" id="formfield-report-firstname">
<label for="first_name" id="first_name">Navn: </label>
<input type="text" name="first_name" id="first_name" />
</div>
<div class="formfield-report" id="formfield-report-lastname">
<label for="last_name" id="last_name">Efternavn: </label>
<input type="text" name="last_name" id="last_name" />
</div>
<div class="formfield-report" id="formfield-report-locationtype">
<label for="location_type" id="location_type">Rapport type: </label>
<select name="location_type" />
<option value="sigtmelding" selected>Sigtmelding</option>
<option value="fangstrapport">Fangstrapport</option>
<option value="jagtomraade">Jagtområde</option>
</select>
</div>
<div class="formfield-report" id="formfield-report-latitude">
<label for="location_latitude" id="location_latitude">Breddegrad: </label>
<input type="text" name="location_latitude" id="location_latitude" />
</div>
<div class="formfield-report" id="formfield-report-longitude">
<label for="location_longitude" id="location_longitude">Længdegrad: </label>
<input type="text" name="location_longitude" id="location_longitude" />
</div>
<input type="hidden" value="0" name="page" />
<div id="formfield-report-button">
<input class="btn btn-default submit-form-button" type="Submit" />
</div>
</form>';
} //End Page 1 of Form
// Start Page 2 of Form
elseif ( $page == 0 )
{
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$location_type = $_POST['location_type'];
$location_latitude = $_POST['location_latitude'];
$location_longitude = $_POST['location_longitude'];
$page = $_POST['page'];
$page_one_table = 'maplocationreports';
$page_one_inputs = array
(
'first_name' => $first_name,
'last_name' => $last_name,
'location_type' => $location_type,
'location_latitude' => $location_latitude,
'location_longitude' => $location_longitude,
'page' => $page
);
$insert_page_one = $wpdb->insert($page_one_table, $page_one_inputs);
echo '<h3>Mange tak for dit bidrag!</h3>';
echo '<p>Der er sat stor pris på at du har taget dig tid til at registrere et punkt på kortet!</p>';
} // End Page 2 of Form
};
add_shortcode('map_location_report','map_location_report_form');`
function map\u location\u report\u form()
{
全球$wpdb;
$this\u page=$\u服务器['REQUEST\u URI'];
$page=$_POST['page'];
如果($page==NULL)
{
回声'
导航:
Efternavn:
融洽关系类型:
标志焊接
方舟港
贾格托姆雷德
布雷德格勒:
Længdegrad:
';
}//表格的第1页结束
//表格的起始页2
elseif($page==0)
{
$first\u name=$\u POST['first\u name'];
$last_name=$_POST['last_name'];
$location\u type=$\u POST['location\u type'];
$location\u latitude=$\u POST['location\u latitude'];
$location\u longitude=$\u POST['location\u longitude'];
$page=$_POST['page'];
$page_one_table='maplocationreports';
$page\u one\u输入=数组
(
“first\u name”=>$first\u name,
“姓氏”=>$last\u name,
“位置类型”=>$location\u类型,
“位置纬度”=>$location\u纬度,
“位置经度”=>$location\u经度,
“页面”=>$page
);
$insert_page_one=$wpdb->insert($page_one_table,$page_one_inputs);
回音“为bidrag的Mange-tak!”;
回音“在登记处和登记处之前,在数据挖掘处保存数据!”;
}//表单的第2页结束
};
添加快捷码(“地图位置报告”、“地图位置报告表”)`
如何阻止此代码两次注册数据
提前谢谢 检查下面的$\u帖子
if (!empty($_POST)) {
//Insert the values to table
}
if (empty($_POST)) {
//display the form
}
将您的
add\u shortcode()
方法包装在init
操作中
add_action('init', 'my_function');
function my_function(){
add_shortcode('map_location_report','map_location_report_form');`
}
由于某些原因,表单在标头中运行时只注册了一次数据,如下所示:
add_action('init', 'map_location_report_form');
function map_location_report_form()
{
global $wpdb;
$this_page = $_SERVER['REQUEST_URI'];
$page = $_POST['page'];
if ( $page == NULL )
{
echo '<form method="post" action="' . $this_page .'">
<div class="formfield-report" id="formfield-report-firstname">
<label for="first_name" id="first_name">Navn: </label>
<input type="text" name="first_name" id="first_name" />
</div>
<div class="formfield-report" id="formfield-report-lastname">
<label for="last_name" id="last_name">Efternavn: </label>
<input type="text" name="last_name" id="last_name" />
</div>
<div class="formfield-report" id="formfield-report-locationtype">
<label for="location_type" id="location_type">Rapport type: </label>
<select name="location_type" />
<option value="sigtmelding" selected>Sigtmelding</option>
<option value="fangstrapport">Fangstrapport</option>
<option value="jagtomraade">Jagtområde</option>
</select>
</div>
<div class="formfield-report" id="formfield-report-latitude">
<label for="location_latitude" id="location_latitude">Breddegrad: </label>
<input type="text" name="location_latitude" id="location_latitude" />
</div>
<div class="formfield-report" id="formfield-report-longitude">
<label for="location_longitude" id="location_longitude">Længdegrad: </label>
<input type="text" name="location_longitude" id="location_longitude" />
</div>
<input type="hidden" value="0" name="page" />
<div id="formfield-report-button">
<input class="btn btn-default submit-form-button" type="Submit" />
</div>
</form>';
} //End Page 1 of Form
// Start Page 2 of Form
elseif ( $page == 0 )
{
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$location_type = $_POST['location_type'];
$location_latitude = $_POST['location_latitude'];
$location_longitude = $_POST['location_longitude'];
$page = $_POST['page'];
$page_one_table = 'maplocationreports';
$page_one_inputs = array
(
'first_name' => $first_name,
'last_name' => $last_name,
'location_type' => $location_type,
'location_latitude' => $location_latitude,
'location_longitude' => $location_longitude,
'page' => $page
);
$insert_page_one = $wpdb->insert($page_one_table, $page_one_inputs);
echo '<h3>Mange tak for dit bidrag!</h3>';
echo '<p>Der er sat stor pris på at du har taget dig tid til at registrere et punkt på kortet!</p>';
} // End Page 2 of Form
};´
add_action('map_location_form', 'map_location_report_form');
function map_location_report_form()
{
global $wpdb;
$this_page = $_SERVER['REQUEST_URI'];
$page = $_POST['page'];
if ( $page == NULL )
{
echo '<form method="post" action="' . $this_page .'">
<div class="formfield-report" id="formfield-report-firstname">
<label for="first_name" id="first_name">Navn: </label>
<input type="text" name="first_name" id="first_name" />
</div>
<div class="formfield-report" id="formfield-report-lastname">
<label for="last_name" id="last_name">Efternavn: </label>
<input type="text" name="last_name" id="last_name" />
</div>
<div class="formfield-report" id="formfield-report-locationtype">
<label for="location_type" id="location_type">Rapport type: </label>
<select name="location_type" />
<option value="sigtmelding" selected>Sigtmelding</option>
<option value="fangstrapport">Fangstrapport</option>
<option value="jagtomraade">Jagtområde</option>
</select>
</div>
<div class="formfield-report" id="formfield-report-latitude">
<label for="location_latitude" id="location_latitude">Breddegrad: </label>
<input type="text" name="location_latitude" id="location_latitude" />
</div>
<div class="formfield-report" id="formfield-report-longitude">
<label for="location_longitude" id="location_longitude">Længdegrad: </label>
<input type="text" name="location_longitude" id="location_longitude" />
</div>
<input type="hidden" value="0" name="page" />
<div id="formfield-report-button">
<input class="btn btn-default submit-form-button" type="Submit" />
</div>
</form>';
} //End Page 1 of Form
// Start Page 2 of Form
elseif ( $page == 0 )
{
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$location_type = $_POST['location_type'];
$location_latitude = $_POST['location_latitude'];
$location_longitude = $_POST['location_longitude'];
$page = $_POST['page'];
$page_one_table = 'maplocationreports';
$page_one_inputs = array
(
'first_name' => $first_name,
'last_name' => $last_name,
'location_type' => $location_type,
'location_latitude' => $location_latitude,
'location_longitude' => $location_longitude,
'page' => $page
);
$insert_page_one = $wpdb->insert($page_one_table, $page_one_inputs);
echo '<h3>Mange tak for dit bidrag!</h3>';
echo '<p>Der er sat stor pris på at du har taget dig tid til at registrere et punkt på kortet!</p>';
} // End Page 2 of Form
};
add_action('init','map_location\u report\u form');
功能图\位置\报告\表格()
{
全球$wpdb;
$this\u page=$\u服务器['REQUEST\u URI'];
$page=$_POST['page'];
如果($page==NULL)
{
回声'
导航:
Efternavn:
融洽关系类型:
标志焊接
方舟港
贾格托姆雷德
布雷德格勒:
Længdegrad:
';
}//表格的第1页结束
//表格的起始页2
elseif($page==0)
{
$first\u name=$\u POST['first\u name'];
$last_name=$_POST['last_name'];
$location\u type=$\u POST['location\u type'];
$location\u latitude=$\u POST['location\u latitude'];
$location\u longitude=$\u POST['location\u longitude'];
$page=$_POST['page'];
$page_one_table='maplocationreports';
$page\u one\u输入=数组
(
“first\u name”=>$first\u name,
“姓氏”=>$last\u name,
“位置类型”=>$location\u类型,
“位置纬度”=>$location\u纬度,
“位置经度”=>$location\u经度,
“页面”=>$page
);
$insert_page_one=$wpdb->insert($page_one_table,$page_one_inputs);
回音“为bidrag的Mange-tak!”;
回音“在登记处和登记处之前,在数据挖掘处保存数据!”;
}//表单的第2页结束
};´
但在使用页面模板上的do_action功能在页面上运行时,正在注册两次数据:
<?php do_action('map_location_form'); ?>
…以及functions.php中的代码,如下所示:
add_action('init', 'map_location_report_form');
function map_location_report_form()
{
global $wpdb;
$this_page = $_SERVER['REQUEST_URI'];
$page = $_POST['page'];
if ( $page == NULL )
{
echo '<form method="post" action="' . $this_page .'">
<div class="formfield-report" id="formfield-report-firstname">
<label for="first_name" id="first_name">Navn: </label>
<input type="text" name="first_name" id="first_name" />
</div>
<div class="formfield-report" id="formfield-report-lastname">
<label for="last_name" id="last_name">Efternavn: </label>
<input type="text" name="last_name" id="last_name" />
</div>
<div class="formfield-report" id="formfield-report-locationtype">
<label for="location_type" id="location_type">Rapport type: </label>
<select name="location_type" />
<option value="sigtmelding" selected>Sigtmelding</option>
<option value="fangstrapport">Fangstrapport</option>
<option value="jagtomraade">Jagtområde</option>
</select>
</div>
<div class="formfield-report" id="formfield-report-latitude">
<label for="location_latitude" id="location_latitude">Breddegrad: </label>
<input type="text" name="location_latitude" id="location_latitude" />
</div>
<div class="formfield-report" id="formfield-report-longitude">
<label for="location_longitude" id="location_longitude">Længdegrad: </label>
<input type="text" name="location_longitude" id="location_longitude" />
</div>
<input type="hidden" value="0" name="page" />
<div id="formfield-report-button">
<input class="btn btn-default submit-form-button" type="Submit" />
</div>
</form>';
} //End Page 1 of Form
// Start Page 2 of Form
elseif ( $page == 0 )
{
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$location_type = $_POST['location_type'];
$location_latitude = $_POST['location_latitude'];
$location_longitude = $_POST['location_longitude'];
$page = $_POST['page'];
$page_one_table = 'maplocationreports';
$page_one_inputs = array
(
'first_name' => $first_name,
'last_name' => $last_name,
'location_type' => $location_type,
'location_latitude' => $location_latitude,
'location_longitude' => $location_longitude,
'page' => $page
);
$insert_page_one = $wpdb->insert($page_one_table, $page_one_inputs);
echo '<h3>Mange tak for dit bidrag!</h3>';
echo '<p>Der er sat stor pris på at du har taget dig tid til at registrere et punkt på kortet!</p>';
} // End Page 2 of Form
};´
add_action('map_location_form', 'map_location_report_form');
function map_location_report_form()
{
global $wpdb;
$this_page = $_SERVER['REQUEST_URI'];
$page = $_POST['page'];
if ( $page == NULL )
{
echo '<form method="post" action="' . $this_page .'">
<div class="formfield-report" id="formfield-report-firstname">
<label for="first_name" id="first_name">Navn: </label>
<input type="text" name="first_name" id="first_name" />
</div>
<div class="formfield-report" id="formfield-report-lastname">
<label for="last_name" id="last_name">Efternavn: </label>
<input type="text" name="last_name" id="last_name" />
</div>
<div class="formfield-report" id="formfield-report-locationtype">
<label for="location_type" id="location_type">Rapport type: </label>
<select name="location_type" />
<option value="sigtmelding" selected>Sigtmelding</option>
<option value="fangstrapport">Fangstrapport</option>
<option value="jagtomraade">Jagtområde</option>
</select>
</div>
<div class="formfield-report" id="formfield-report-latitude">
<label for="location_latitude" id="location_latitude">Breddegrad: </label>
<input type="text" name="location_latitude" id="location_latitude" />
</div>
<div class="formfield-report" id="formfield-report-longitude">
<label for="location_longitude" id="location_longitude">Længdegrad: </label>
<input type="text" name="location_longitude" id="location_longitude" />
</div>
<input type="hidden" value="0" name="page" />
<div id="formfield-report-button">
<input class="btn btn-default submit-form-button" type="Submit" />
</div>
</form>';
} //End Page 1 of Form
// Start Page 2 of Form
elseif ( $page == 0 )
{
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$location_type = $_POST['location_type'];
$location_latitude = $_POST['location_latitude'];
$location_longitude = $_POST['location_longitude'];
$page = $_POST['page'];
$page_one_table = 'maplocationreports';
$page_one_inputs = array
(
'first_name' => $first_name,
'last_name' => $last_name,
'location_type' => $location_type,
'location_latitude' => $location_latitude,
'location_longitude' => $location_longitude,
'page' => $page
);
$insert_page_one = $wpdb->insert($page_one_table, $page_one_inputs);
echo '<h3>Mange tak for dit bidrag!</h3>';
echo '<p>Der er sat stor pris på at du har taget dig tid til at registrere et punkt på kortet!</p>';
} // End Page 2 of Form
};
add_action('map_location_form'、'map_location_report_form');
功能图\位置\报告\表格()
{
全球$wpdb;
$this\u page=$\u服务器['REQUEST\u URI'];
$page=$_POST['page'];
如果($page==NULL)
{
回声'
导航:
Efternavn:
融洽关系类型:
标志焊接
方舟港
贾格托姆雷德
布雷德格勒:
Længdegrad:
';
}//表格的第1页结束
//表格的起始页2
elseif($page==0)
{
$first\u name=$\u POST['first\u name'];
$last_name=$_POST['last_name'];