逻辑已经存在的记录检查,但仅在更新表单值的情况下 [英] Logic for already exist record check but only in case of updated form values

查看:64
本文介绍了逻辑已经存在的记录检查,但仅在更新表单值的情况下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个称为县经理"的模块 我在检查县mysql表中是否存在其国家/地区已存在的县时遇到问题.

I am working on a module called county manager I have issues with checking for already existing county with its country in the counties mysql table.

数据库表

让我解释一下

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

Add Page In add page i am having 2 fields (check screenshot)

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

This is my code for saving the county in table

$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();

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

In add page it succesfully checks (whether the county that is being added along with the country already exists in the table or not) before inserting a record

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

But its not working in my edit page or you can say i am unable to find the logic behind how to check it.

编辑页面

Edit Page

在下面查看我的编辑页面

Check out my edit page below

这是用于编辑页面的代码

This is code for editing page

<!-- 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 -->

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

and this is my code for saving / updating the record in edit page

$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中进行比较以检查记录是否已存在

I am unable to code the sql logic that will check for already existing record in case of edit / updating the record, though i have tried something with passing 2 hidden variables in form (check in edit page code above) that contains the old country name and county name so that I can check compare them in sql to check whether the record already exists or not

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

I can use the same add page logic here as well i.e

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

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

but the problem is that in case i dont update the 2 values i.e country and county in edit page it still considers the record as already existing in the table. I need to handle that aspect that it should check for the already existing record only if the one of them or both the form field values are updated.

请帮助我如何实现仅当值中的一个或两个都以表格形式更新时才检查现有记录的逻辑.

Please help me how to achieve the logic that will check for already existing record only if one of the values or both are updated in the form.

推荐答案

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

in edit part:
try to compare ID of edited record instead of comparing old/new names e.g.

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

这篇关于逻辑已经存在的记录检查,但仅在更新表单值的情况下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆