gpt4 book ai didi

php - 已存在记录检查的逻辑,但仅在更新表单值的情况下

转载 作者:行者123 更新时间:2023-11-29 02:01:47 26 4
gpt4 key购买 nike

<分区>

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

数据库表 Counties table

我来解释一下

添加页面在添加页面我有 2 个字段(查看屏幕截图) Add Page

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

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

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

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

编辑页面

查看下面我的编辑页面 Edit 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 -->

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

$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 逻辑,尽管我已经尝试过在包含旧的国家名称和县名称,以便我可以在 sql 中检查比较它们以检查记录是否已存在

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

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

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

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com