 |
ช่วยดูทีครับอยากได้ Code ที่ค้นหาข้อมูลโดยต้องกรอกข้อมูลอย่างน้อย1อย่างขึ้นไป |
|
 |
|
|
 |
 |
|
Code (PHP)
SELECT * FROM student WHERE SID LIKE '%$.....%' OR Sname LIKE '%$...%' OR ....
|
 |
 |
 |
 |
Date :
2012-03-24 13:35:11 |
By :
50121680 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมว่าอย่างเดียวไม่ดีนะครับ ถ้าเกิดชื่อซ้ำกันละ ???
เพราะงั้นก้ใช้อย่างน้อยก็รหัสอ่ะ ดีกว่า
แล้วก้ตั้งเงื่อนไขเอาสิครับ
if($id){
$SQL="select * form table where Id='$id'"
}else if($name){
$SQL="select * from table where name='$name'"
}
แล้วจบ if ค่อยส่งไป query
|
 |
 |
 |
 |
Date :
2012-03-24 14:01:30 |
By :
tongspy |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไมได้เทสนะครับ มันเป็นเรื่องของ 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);
|
 |
 |
 |
 |
Date :
2012-03-24 14:03:35 |
By :
13eachz |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอบความคิดเห็นที่ : 3 เขียนโดย : tongspy เมื่อวันที่ 2012-03-24 14:01:30
รายละเอียดของการตอบ ::
ลองตามวิธีของคุณ tongspy แล้ว
ทำนองนี่รึป่าวครับ
Code (PHP)
$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);
ค้นหาได้เพียงอย่างเดียวครับ มากกว่า1 เงื่อนไขไม่ได้
|
 |
 |
 |
 |
Date :
2012-03-24 14:38:47 |
By :
lnwsit |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อืม..ยังไง ครับ ค่าเดียว ก็เรา จะเอา ค่าอะไร select ออกมา ตามที่ where ไงครับ
ไม่เข้าใจคำถามนิดหน่อย
|
ประวัติการแก้ไข 2012-03-24 14:44:05 2012-03-24 14:50:42
 |
 |
 |
 |
Date :
2012-03-24 14:43:32 |
By :
tongspy |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เอาง่ายๆนะครับ
แบบที่คุณเขียนน่ะถูกแล้ว แต่ว่า เงื่อนไขในการเรียกใช้มันผิด
ผิดตรงไหน ผิดที่ where มันใช้ได้ครั้งเดียว แต่ เรียก where ซ้ำๆ เท่านั้นเองครับ แล้วก็ order ถ้ามี 2 ฟิลด์แปลว่า order 2 รอบมันก้ไม่ได้เช่นกัน
ผมไม่ทันสังเกตุลองดูอีกทีครับ
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."%' OR Teacher like '%$tid%' )";
$and=true;
}
if(trim($stdname)!="")
{
if($and)$stdsql.=" OR ";
$stdsql.=" ( Sname like '%".$stdname."%' OR Teacher like '%$tid%' )";
$and=true;
}
if(trim($surname)!="")
{
if($and)$stdsql.=" OR ";
$stdsql.=" ( Ssname like '%".$surname."%' OR Teacher like '%$tid%' ) ";
$and=true;
}
if(trim($class)!="")
{
if($and)$stdsql.=" OR ";
$stdsql.=" ( Level_Id like '%".$class."%' OR Teacher like '%$tid%' ) ";
$and=true;
}
$stdresult=mysql_query($stdsql) OR die("<script>alert('Error!');history.back();</script>");
$row = mysql_num_rows($stdresult);
ถ้ามี error อะไรบอกด้วยครับ
|
 |
 |
 |
 |
Date :
2012-03-24 14:59:11 |
By :
13eachz |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|