【问题标题】:Logic for already exist record check but only in case of updated form values [duplicate]已存在记录检查的逻辑,但仅在更新表单值的情况下[重复]
【发布时间】:2012-11-25 01:49:01
【问题描述】:

我正在开发一个名为县经理的模块 我在县 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 个值,即编辑页面中的国家和县,它仍然认为该记录已存在于表中。我需要处理这方面的问题,即只有在其中一个或两个表单字段值被更新时,它才应该检查已经存在的记录。

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

【问题讨论】:

  • county_name_existing 字段有什么用?我认为问题出在哪里,因为这是从表中获取所有内容?
  • @Dale - 它们用于假设我没有更新表单字段并且需要 sql 中的 2 个值进行比较的场景,例如 select * fromcounties where country='newly_selected_country_in_edit_page aka $country' 和国家! ='old_value_during_edit_page_load aka $country_existing'。我希望你明白吗?
  • 我明白了,但这肯定意味着数据库中的其他所有内容?
  • 如何在国家和县名上创建一个复合唯一索引?现在数据库正在为您处理逻辑

标签: php mysql sql


【解决方案1】:

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

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

【讨论】:

  • 感谢伙计,它确实有效。我受够了,以至于我没有意识到把它做对是那么容易的。非常感谢朋友。你真的解决了我的一个主要问题。我真的很感激。
【解决方案2】:

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

ALTER TABLE `your_table_name` ADD UNIQUE (
`country` ,
`county_name`
);

【讨论】:

  • 可能会有同一个县在其他国家的情况。我也必须考虑到这一点。
  • 此唯一索引将允许相同的county_name 与不同的国家/地区
  • 我也将结合您的解决方案 :) 这将使我的代码得到更充分的证明。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-08
  • 2015-07-08
  • 2017-02-05
  • 1970-01-01
  • 2015-04-19
  • 1970-01-01
相关资源
最近更新 更多