我正在尝试为客户Woocommerce商店创建一个英国邮政编码检查器。我在Wordpress中创建了一个包含单元格、邮政编码、城镇和县的表格。
当用户在表单中输入邮政编码并单击提交时,我想检查我们的数据库中是否存在邮政编码,并让用户知道。
我的代码无法识别数据库中是否存在邮政编码,或稍后输出城镇。
任何帮助都将不胜感激。
<?php
global $wpdb;
$table_name = $wpdb->prefix.\'vw_postcode_checker\';
if (!empty($_POST[postcode]))
{
$postcode = $_POST[postcode];
$entry = $wpdb->get_results("SELECT * FROM ".$tablename."WHERE postcode =" . $postcode);
if(count($entry) == 1){
$id = $entry->id; //We need this for the delete to work.
$postcode = $entry->postcode;
$town = $entry->town;
$county = $entry->county;
$confirmed = "Contratulations, we are delivering to" . $town;
echo $confirmed;
}
else {
$notconfirmed = "Sorry, we are not delivering to your area at this time.";
echo $notconfirmed;
}
}
?>
SO网友:Victoria Whitehead
谢谢@user141080,我将表名声明为错误。我还缺少“$postcode”周围的“”,您可以在where子句和的末尾添加一个额外的“”。变量后的“\'”。
下面的代码现在可以工作了。
<?php
/*-------------------------------------------------*/
/*-------------Check Postcode -----------------*/
/*-------------------------------------------------*/
global $wpdb;
$table_name = $wpdb->prefix.\'vw_postcode_checker\';
if (!empty($_POST[postcode]))
{
$postcode = $_POST[postcode];
$entry = $wpdb->get_row( "SELECT * FROM ".$table_name." WHERE postcode = \'" . $postcode . "\'");
if (isset($entry))
{
$town = $entry->town;
$county = $entry->county;
$your_area = $town . ", " . $county;
$confirmed = "Congratulations, we are delivering to " . $your_area;
echo $confirmed;
}
else
{
$notconfirmed = "Sorry, we are not delivering to your area at this time.";
echo $notconfirmed;
}
}
?>