2

我正在开发一个名为county manager的模块,我在检查counties mysql表中已经存在的县及其国家时遇到了问题。

数据库表 县表

让我解释

添加页面 在添加页面中,我有 2 个字段(检查屏幕截图) 添加页面

这是我在表格中保存县的代码

$country = stripslashes($_POST["country"]);
$county_name = stripslashes($_POST["county_name"]);

// County Already Exist Check
$sql = "SELECT county_id FROM counties WHERE country='$country' AND county_name='$county_name' LIMIT 1";
$query = $mysql->query($sql);

if($mysql->rowCount($query) > 0)
{
    header("location:counties_add.php?type=warning&msg=" .urlencode("County With This Country Already Exist"));
    exit();
}

$sql = "INSERT INTO ". TABLE_COUNTIES ."";
$sql .= "(country, county_name, datecreated, datemodified) VALUES";
$sql .= "('$country', '$county_name', now(), now())";
$query = $mysql->query($sql);

$msg = "County Added Successfully";
header("location:counties_view.php?type=success&msg=" .urlencode($msg));
exit();

在添加页面中,它在插入记录之前成功检查(与国家一起添加的县是否已经存在于表中)

但它在我的编辑页面中不起作用,或者您可以说我无法找到如何检查它的逻辑。

编辑页面

在下面查看我的编辑页面 编辑页面

这是编辑页面的代码

<!-- Form Starts -->
<form id="myform" name="myform" method="post" action="counties_edit_process.php" accept-charset="utf-8">

<!-- Widget Starts -->
<div class="widget">
    <div class="title js_opened">
        <div class="icon"><img src="themes/<?php echo ADMIN_PANEL_THEME; ?>/images/icons/navigation/counties<?php echo $retina_suffix; ?>.png" width="24" height="24" alt="" /></div>
        <span>Fill The Fields Marked With *</span>
    </div>
    <div class="content">
        <div class="form_row first">
            <label>Country</label>
            <div class="form_right">
                <select name="country">
                    <option value="">Please Choose An Option</option>
                    <option value="England" <?php if ($dbData["country"]=="England") echo "selected=\"selected\""; ?> >England</option>
                    <option value="Scotland" <?php if ($dbData["country"]=="Scotland") echo "selected=\"selected\""; ?> >Scotland</option>
                    <option value="Wales" <?php if ($dbData["country"]=="Wales") echo "selected=\"selected\""; ?> >Wales</option>
                </select>
            </div>
            <div class="clear"></div>
        </div>
        <div class="form_row last">
            <label>Country Name</label>
            <div class="form_right"><input type="text" name="county_name" maxlength="25" value="<?php echo $dbData["county_name"]; ?>" /></div>
            <div class="clear"></div>
        </div>
    </div>
</div>
<!-- Widget Ends -->

<div class="form_buttons">
    <input type="submit" name="submit" value="Update" />&nbsp;&nbsp;<a href="counties_view.php">Back To View</a>
    <input type="hidden" name="country_existing" value="<?php echo $dbData["country"]; ?>" />
    <input type="hidden" name="county_name_existing" value="<?php echo $dbData["county_name"]; ?>" />
    <?php $form->passValuesToNextPage("GET"); ?>
</div>
</form>
<!-- Form Ends -->

这是我在编辑页面中保存/更新记录的代码

$id = stripslashes($_POST["id"]);

// For Already Exist Checks
$country = stripslashes($_POST["country"]);
$country_existing = stripslashes($_POST["country_existing"]);
$county_name = stripslashes($_POST["county_name"];
$county_name_existing = stripslashes($_POST["county_name_existing"]);

// County Already Exist Check

    <--- Issue is here in the sql to check for already exist. Please read the end of the post to understand my question  --->

$sql = "SELECT county_id FROM ". TABLE_COUNTIES ." WHERE (county_name='$county_name' AND county_name!='$county_name_existing') AND (country='$country' AND country!='$country_existing') LIMIT 1";
$query = $mysql->query($sql);

if($mysql->rowCount($query) > 0)
{
    header("location:counties_edit.php?id=$id&case=edit&type=warning&msg=" .urlencode("County With This Country Already Exist"));
    exit();
}

$sql = "UPDATE ". TABLE_COUNTIES ." SET";
$sql .= " country='". $country ."',";
$sql .= " county_name='". $county_name ."',";
$sql .= " datemodified=now()";
$sql .= " WHERE county_id=$id";
$query = $mysql->query($sql);

header("location:counties_view.php?type=success&msg=" .urlencode("County Updated Successfully"));
exit();

我无法编写将在编辑/更新记录的情况下检查现有记录的 sql 逻辑,尽管我已经尝试在包含旧国家名称的表单中传递 2 个隐藏变量(检查上面的编辑页面代码)和县名,以便我可以在 sql 中检查比较它们以检查记录是否已经存在

我也可以在这里使用相同的添加页面逻辑,即

$sql = "SELECT county_id FROM counties WHERE country='$country' AND county_name='$county_name' LIMIT 1";

但问题是,如果我不更新 2 个值,即编辑页面中的国家和县,它仍然认为该记录已经存在于表中。我需要处理只有当其中一个或两个表单字段值都更新时它才应该检查已经存在的记录的方面。

请帮助我如何实现仅当表单中的值之一或两者都更新时才检查现有记录的逻辑。

4

2 回答 2

1

在编辑部分:
尝试比较已编辑记录的 ID,而不是比较旧/新名称,例如

$sql = "SELECT county_id FROM counties WHERE country='$country' AND county_name='$county_name' AND country_id !='$id' LIMIT 1";
于 2012-11-24T08:01:45.513 回答
1

我个人会在国家和县名上添加一个复合唯一索引

ALTER TABLE `your_table_name` ADD UNIQUE (
`country` ,
`county_name`
);
于 2012-11-24T08:10:51.910 回答