|
 |
|
สอบถามครับ เรื่องการกำหนดข้อมูลซ้ำครับ ว่าจะให้ซ้ำได้กี่ record |
|
 |
|
|
 |
 |
|
ให้นับจำนวนก่อนบันทึกครับ นับโดยใช้เงื่อนไข loc_id ในการเทียบข้อมูล
Code (SQL)
SELECT COUNT(*) AS cnt FROM supervision_teacher WHERE loc_id = 'xxx'
ถ้า cnt มากกว่า หรือ เท่ากับ 3 ก็ไม่ให้ INSERT
|
 |
 |
 |
 |
Date :
2014-05-14 07:35:38 |
By :
{Cyberman} |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แยกเช็กสองครั้งไปเลยครับ
ปล. ครั้งแรกเช็คว่าอาจารย์ซ้ำรึเปล่าก่อนนะ
|
ประวัติการแก้ไข 2014-05-15 23:35:36
 |
 |
 |
 |
Date :
2014-05-15 23:35:01 |
By :
{Cyberman} |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ยังติดปัญหาอยู่ครับ
ตอนนี้มีอาจารย์ในโรงบาลอยู่ 2 ท่านครับ

จากนั้นผมเพิ่มเข้าไปอีก 2 ท่าน ซึ่งมันก็แจ้งเตือนว่า สามารถกำหนดอาจารย์ได้สูงสุด 3 ท่านเท่านั้น เพราะเพิ่มไปอีก 2 มันก็เกิน 3 แล้ว

แต่พอกลับไปดูจุดที่แสดงข้อมูล มันกลับ Insert อาจารย์ เข้ามา จากที่เลือกไป 2 คน

ทำไมมันยัง Insert เข้ามาอีกอ่ะครับ ทั้งๆ ที่ก็เข้าเงื่อนไข ที่ไม่ให้อาจารย์เกิน 3 คนแล้ว
คือถ้ามันแจ้งเตือนแล้ว อยากให้มันไม่ต้อง Insert อ่ะครับ
ปล. ผมใช้ group_concat ในการแสดงข้อมูลครับ
Code (PHP)
<?PHP
include "config/connect.php";
// รับค่าที่ส่งมา
$fav_id = $_GET['fav_id'];
$sql = "SELECT * FROM favor";
$sql .= " LEFT JOIN location ON favor.loc_id = location.loc_id";
$sql .= " WHERE fav_id = '$fav_id'";
$result = mysql_query($sql);
$show = mysql_fetch_array($result);
if(isset($_GET['submit'])){
for($i=0;$i<count($_GET["per_id"]);$i++){
$per_id = $_GET["per_id"][$i];
$loc_id = $_GET["loc_id"][$i];
if($_GET["per_id"][$i] != ""){
// เงื่อนไข ไม่ให้มีอาจารย์ซ้ำ
$sql1 = "SELECT * FROM supervision_teacher WHERE loc_id = '$loc_id' AND per_id = '$per_id'";
$result1 = mysql_query($sql1);
$num1 = mysql_num_rows($result1);
// เงื่อนไข ไม่ให้มีอาจารย์มากกว่า 3
$sql2 = "SELECT COUNT(loc_id) AS cnt FROM supervision_teacher WHERE loc_id = '$loc_id'";
$result2 = mysql_query($sql2);
$show2 = mysql_fetch_array($result2);
if($num1 > 0){
echo "<script>alert('สถานประกอบการนี้มีอาจารย์ที่เลือกแล้ว');window.location='visionTeacherAdd.php?fav_id=$fav_id';</script>";
exit();
}else if($show2['cnt'] >= '3'){
echo "<script>alert('สามารถกำหนดอาจารย์ได้สูงสุด 3 ท่านเท่านั้น');window.location='".$_SESSION['visionView']."';</script>";
exit();
}else{
$sql = "INSERT INTO supervision_teacher (loc_id, per_id) VALUES ('$loc_id', '$per_id')";
$result = mysql_query($sql);
}
}
}
echo "<script>alert('บันทึกข้อมูลเรียบร้อยแล้ว');window.location='".$_SESSION['visionView']."';</script>";
}
?>
<form name="visionViewN" action="<?PHP $_SERVER['PHP_SELF']?>" enctype="multipart/form-data">
<table width="100%" cellpadding="0" cellspacing="1" border="1">
<tr bgcolor="ghostwhite">
<td colspan="2"><b> » จัดการผู้นิเทศก์ : </b><?=$show['loc_name']; if($show['loc_suborder']==""){ echo "<font color=\"gray\"><i>(ไม่มีหนวยงานย่อย)</i></font>";} else { echo " (".$show['loc_suborder'].")"; }?></td>
</tr>
<tr>
<td></td>
<td>
<select name="per_id[]">
<option value="">ว่าง</option>
<?PHP
$sqlVis = "SELECT * FROM personnel WHERE cyp_id = '1'";
$sqlVis .= " ORDER BY per_name ASC";
$resultVis = mysql_query($sqlVis);
while($showVis = mysql_fetch_array($resultVis)){
?>
<option value="<?=$showVis['per_id']?>"><?=$showVis['per_name']?></option>
<?PHP } ?>
</select>
<input type="hidden" name="loc_id[]" value="<?=$show['loc_id']?>"/>
</td>
</tr>
<tr>
<td></td>
<td>
<select name="per_id[]">
<option value="">ว่าง</option>
<?PHP
$sqlVis = "SELECT * FROM personnel WHERE cyp_id = '1'";
$sqlVis .= " ORDER BY per_name ASC";
$resultVis = mysql_query($sqlVis);
while($showVis = mysql_fetch_array($resultVis)){
?>
<option value="<?=$showVis['per_id']?>"><?=$showVis['per_name']?></option>
<?PHP } ?>
</select>
<input type="hidden" name="loc_id[]" value="<?=$show['loc_id']?>"/>
</td>
</tr>
<tr>
<td></td>
<td>
<select name="per_id[]">
<option value="">ว่าง</option>
<?PHP
$sqlVis = "SELECT * FROM personnel WHERE cyp_id = '1'";
$sqlVis .= " ORDER BY per_name ASC";
$resultVis = mysql_query($sqlVis);
while($showVis = mysql_fetch_array($resultVis)){
?>
<option value="<?=$showVis['per_id']?>"><?=$showVis['per_name']?></option>
<?PHP } ?>
</select>
<input type="hidden" name="loc_id[]" value="<?=$show['loc_id']?>"/>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="hidden" name="fav_id" value="<?=$fav_id;?>"/>
<input type="submit" name="submit" style="width: 100px" value="บันทึก"/>
<input type="button" style="width: 100px" onclick="window.location.href='<? echo $_SESSION['visionView']; ?>'" value="ย้อนกลับ"/>
</td>
</tr>
</table>
</form>
|
ประวัติการแก้ไข 2014-05-17 13:59:36 2014-05-17 14:00:04 2014-05-17 14:01:08 2014-05-17 14:02:08 2014-05-17 14:02:39 2014-05-17 14:06:24 2014-05-17 14:09:29 2014-05-17 14:12:48 2014-05-17 14:15:12
 |
 |
 |
 |
Date :
2014-05-17 13:59:02 |
By :
sabaitip |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แสดงว่ามันทำงานแบบไม่สนใจ 2 เงื่อนไขนี้
if($num1 > 0){
.
.
.
}else if($show2['cnt'] >= '3'){
.
.
ดันไปเข้า else หมดเลยบันทึกข้อมูลได้
ลองสร้างข้อมูลทดสอบ แบบตั้งใจให้เข้าทีละเงื่อนไขดูครับ แล้วecho $sql ก่อนปิดแท็ก ว่าจะมีผลลัพธ์ออกมาหรือไม่?
ถ้ามีแสดงว่าเกิดข้อผิดพลาดตรงเงื่อนไขครับ
|
 |
 |
 |
 |
Date :
2014-05-17 18:20:41 |
By :
{Cyberman} |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 02
|