我上一個模塊調用縣經理 工作,我有與它在縣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" /> <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
@Dale - 它們用於可以說我沒有更新表單字段並需要sql中的2個值來比較的場景,比如select * from country where country ='newly_selected_country_in_edit_page aka $ country'and country!='old_value_during_edit_page_load aka $ country_existing' 。我希望你明白嗎? – Asnexplore
我明白了,但肯定意味着數據庫中的其他所有內容? – Dale