|
 |
|
ผมต้องการตรวจสอบข้อมูลว่ามีค่าซ้ำกันมั้ย ก่อนนำข้อมูลลงฐานข้อมูล ไม่ทราบว่าต้องเขียนโค้ดยังไงบ้าง |
|
 |
|
|
 |
 |
|
Code (PHP)
<?php
$con = mysql_connect("localhost","root","1234");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con);
$cs1 = "SET character_set_results=tis620";
mysql_query($cs1) or die('Error query: ' . mysql_error());
$cs2 = "SET character_set_client = tis620";
mysql_query($cs2) or die('Error query: ' . mysql_error());
$cs3 = "SET character_set_connection = tis620";
mysql_query($cs3) or die('Error query: ' . mysql_error());
$rr="$_POST[roomddl]";
$uu="$_POST[txtdate]";
$tt="$_POST[timedll]";
$select="SELECT * FROM tbres WHERE room='$rr' AND use_date='$uu' AND time='$tt' ";//username='$user' AND pass='$pass'";
$result=mysql_query($select);
$num=mysql_num_rows($result);
$rs=mysql_fetch_array($result);
$r=$rs["room"];
$u=$rs["use_date"];
$t=$rs["time"];
/*
echo "$r<BR>";
echo "$u<BR>";
echo "$t<BR>";
*/
echo "$rr<BR>";
echo "$uu<BR>";
echo "$tt<BR>";
if($r=$rr AND $u=$uu AND $t=$tt){
echo "<script>alert('ไม่สามารถเพิ่มข้อมูลได้ เนื่องจากมีการเพิ่มแล้ว กรุณากลับไปแก้ไข');
window.location='index.php';
</script>";
}
else{
$sql="INSERT INTO tbres (room,log_date, use_date, time,res_ing,unit)
VALUES
('$_POST[roomddl]',CURDATE(),'$_POST[txtdate]','$_POST[timedll]','$_POST[namerestxt]','$_POST[txtunit]')";
if (!mysql_query($sql,$con))
{
echo "<script>alert('ไม่สามารถเพิ่มข้อมูลได้');
window.location='index.php';
</script>";
exit();
}
echo "<script>alert('คุณได้ทำการเพิ่มข้อมูลได้');
window.location='index.php';
</script>";
exit();
}
mysql_close($con)
?>
Tag : - - - -

|
|
 |
 |
 |
 |
Date :
2009-09-22 11:17:11 |
By :
oasiis |
View :
1642 |
Reply :
2 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใช้ตัวแปร $num เช็คก็ได้นะครับ
เพราะถ้าไม่มีค่า $num = 0
แต่ถ้ามี $num > 0
ตัด $rs=mysql_fetch_array($result);
ออก แล้วเช็ค IF $num > 0 ซ้ำ else insert
|
 |
 |
 |
 |
Date :
2009-09-22 11:21:10 |
By :
taobsd |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แมนยู
แมนแล้ว
แมนอีหลี
แมนคักๆ
|
 |
 |
 |
 |
Date :
2009-09-22 11:28:34 |
By :
Sek-Artdrinker |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 05
|