<?PHP
$id=mysql_real_escape_string($_POST['id']);
$stdname=mysql_real_escape_string($_POST['stdname']);
$surname=mysql_real_escape_string($_POST['surname']);
$class=mysql_real_escape_string($_POST['select']);
if(empty($id)&& empty($stdname)&&empty($surname)&&empty($class))
{
echo "<script>alert('กรุณากรอกข้อมูลอย่างน้อย1อย่าง');history.back();</script>";
exit();
}
$tid = $_SESSION['login'];
$and =false;
$stdsql="SELECT SID,Sname,Ssname,Level_Id,Room FROM student";
if(trim($id)!="")
{
$stdsql.=" WHERE SID like '%".$id."%' && Teacher like '%$tid%' order by Level_Id,Room";
$and=true;
}
if(trim($stdname)!="")
{
if($and)$stdsql.=" AND ";
$stdsql.=" WHERE Sname like '%".$stdname."%' && Teacher like '%$tid%' order by Level_Id,Room";
$and=true;
}
if(trim($surname)!="")
{
if($and)$stdsql.=" AND ";
$stdsql.=" WHERE Ssname like '%".$surname."%' && Teacher like '%$tid%' order by Level_Id,Room";
$and=true;
}
if(trim($class)!="")
{
if($and)$stdsql.=" AND ";
$stdsql.=" WHERE Level_Id like '%".$class."%' && Teacher like '%$tid%' order by Level_Id,Room";
$and=true;
}
$stdresult=mysql_query($stdsql) OR die("<script>alert('Error!');history.back();</script>");
$row = mysql_num_rows($stdresult);
ไมได้เทสนะครับ มันเป็นเรื่องของ where ซ้ำๆ กัน ให้เปลี่ยนเป็น วงเล็บ และใช้ OR ในการหาอย่างใดอย่างหนึ่งแทน
Code (PHP)
<?PHP
$id=mysql_real_escape_string($_POST['id']);
$stdname=mysql_real_escape_string($_POST['stdname']);
$surname=mysql_real_escape_string($_POST['surname']);
$class=mysql_real_escape_string($_POST['select']);
if(empty($id)&& empty($stdname)&&empty($surname)&&empty($class))
{
echo "<script>alert('กรุณากรอกข้อมูลอย่างน้อย1อย่าง');history.back();</script>";
exit();
}
$tid = $_SESSION['login'];
$and =false;
$stdsql="SELECT SID,Sname,Ssname,Level_Id,Room FROM student WHERE ";
if(trim($id)!="")
{
$stdsql.=" ( SID like '%".$id."%' && Teacher like '%$tid%' order by Level_Id,Room )";
$and=true;
}
if(trim($stdname)!="")
{
if($and)$stdsql.=" OR ";
$stdsql.=" ( Sname like '%".$stdname."%' && Teacher like '%$tid%' order by Level_Id,Room )";
$and=true;
}
if(trim($surname)!="")
{
if($and)$stdsql.=" OR ";
$stdsql.=" ( Ssname like '%".$surname."%' && Teacher like '%$tid%' order by Level_Id,Room ) ";
$and=true;
}
if(trim($class)!="")
{
if($and)$stdsql.=" OR ";
$stdsql.=" ( Level_Id like '%".$class."%' && Teacher like '%$tid%' order by Level_Id,Room ) ";
$and=true;
}
$stdresult=mysql_query($stdsql) OR die("<script>alert('Error!');history.back();</script>");
$row = mysql_num_rows($stdresult);
$tid = $_SESSION['login'];
if($id){
$stdsql="select SID,Sname,Ssname,Level_Id,Room FROM student where SID='%$id%' and Teacher = '$tid'";
}
else if($stdname){
$stdsql="select SID,Sname,Ssname,Level_Id,Room FROM student where Sname='%$stdname%' and Teacher = '$tid'";
}
else if($surname){
$stdsql="select SID,Sname,Ssname,Level_Id,Room FROM student where Ssname='%$surname%' and Teacher = '$tid'";
}
else if($class){
$stdsql="select SID,Sname,Ssname,Level_Id,Room FROM student where Level_Id='$class' and Teacher = '$tid'";
}
$stdresult=mysql_query($stdsql) OR die("<script>alert('Error!');history.back();</script>");
$row = mysql_num_rows($stdresult);