รายละเอียดของการตอบ ::
ผมลองใส่เเล้วมันขึ้นเเบบนี้น่ะคับ Error Query [select * from computeripname where like '%10.0.255.18 %'] ผมใส่อะไรผิดไปหรือป่าวหรอครับ ตอนนี้ผมตั้ง value ของ select option เก็บเป็นชื่อ field เเล้วครับ มีทั้งหมด 3 อันคือ ipaddress namecom party หน่ะครับ ผมต้องแก้ไขหรือเพิ่มตรงไหนหรอครับ ขอบคุณคับ
Code
<html>
<head>
<title>PHP MySQL Search Record Paging/Pagination</title>
<meta http-equiv=Content-Type content="text/html; charset=UTF-8">
</head>
<body>
<p align="center">ระบบค้นหาชื่อเครื่องคอมพิวเตอร์ (Computer Name)</p>
<form name="frmSearch" method="get" action="<?=$_SERVER['SCRIPT_NAME'];?>">
<table width="599" border="1" align="center">
<tr>
<th>Search
<select name="ddlSelect" id="ddlSelect">
<option>- Select -</option>
<option value="ipaddress" <?if($_POST["ddlSelect"]=="ipaddress"){echo"selected";}?>>ipaddress</option>
<option value="namecom" <?if($_POST["ddlSelect"]=="namecom"){echo"selected";}?>>namecom</option>
<option value="party" <?if($_POST["ddlSelect"]=="party"){echo"selected";}?>>party</option>
</select>
Keyword
<<input name="txtKeyword" type="text" id="txtKeyword" value="<?=$_POST["txtKeyword"];?>">
<input type="submit" value="Search"></th>
</tr>
</table>
</form>
<p align="center"><a href="phpMySQLAddForm.php" target="_blank">เพิ่มข้อมูล</a>
<?
$objConnect = mysql_connect("localhost","root","11223344") or die("Error Connect to Database");
$objDB = mysql_select_db("mydatabase");
mysql_query("SET NAMES UTF8");
// Search By Name or Email
$strSQL = "SELECT * FROM computeripname WHERE 1";
if(!empty($_GET['ddlSelect'])){
$field = $_GET['ipaddress']; //ตรงนี้จะได้ว่า จะ where ด้วย field อะไร
$keyword = $_GET['txtKeyword'];
$strSQL = "select * from computeripname where $field like '%$keyword %'";
}
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
$Num_Rows = mysql_num_rows($objQuery);
$Per_Page = 10; // Per Page
$Page = $_GET["Page"];
if(!$_GET["Page"])
{
$Page=1;
}
$Prev_Page = $Page-1;
$Next_Page = $Page+1;
$Page_Start = (($Per_Page*$Page)-$Per_Page);
if($Num_Rows<=$Per_Page)
{
$Num_Pages =1;
}
else if(($Num_Rows % $Per_Page)==0)
{
$Num_Pages =($Num_Rows/$Per_Page) ;
}
else
{
$Num_Pages =($Num_Rows/$Per_Page)+1;
$Num_Pages = (int)$Num_Pages;
}
$strSQL .=" order by ipaddress ASC LIMIT $Page_Start , $Per_Page";
$objQuery = mysql_query($strSQL);
?>
</p>
</p>
<table width="1024" border="1" align="center">
<tr bgcolor="#33FF33">
<th width="92"> <div align="center">แก้ไข | ลบ</div></th>
<th width="157"> <div align="center">IP Address </div></th>
<th width="261"> <div align="center">ชื่อเครื่องคอมพิวเตอร์</div></th>
<th width="33"> <div align="center">ฝ่าย</div></th>
<th width="112"> <div align="center">ยี่ห้อ</div></th>
<th width="242"> <div align="center">ที่ตั้ง</div></th>
<th width="81"> <div align="center">หมายเหตุ</div></th>
</tr>
<?
while($objResult = mysql_fetch_array($objQuery))
{
?>
<tr>
<td align="center"><a href="phpMySQLEditRecordForm.php?ipaddress=<?=$objResult["ipaddress"];?>">Edit</a> | <a href="JavaScript:if(confirm('Confirm Delete?')==true){window.location='phpMySQLDeleteRecord.php?ipaddress=<?=$objResult["ipaddress"];?>';}">Delete</a></td>
<td><div align="center"><?=$objResult["ipaddress"];?></div></td>
<td align="center" bgcolor="#CCCCCC"><?=$objResult["namecom"];?></td>
<td align="center"><?=$objResult["party"];?></td>
<td align="center"><?=$objResult["brand"];?></td>
<td align="center"><?=$objResult["local"];?></td>
<td align="center"><?=$objResult["note"];?></td>
</tr>
<?
}
?>
</table>
<br>
<p align="center">Total <?= $Num_Rows;?> Record : <?=$Num_Pages;?> Page :
<?
if($Prev_Page)
{
echo " <a href='$_SERVER[SCRIPT_NAME]?Page=$Prev_Page&txtKeyword=$_GET[txtKeyword]'><< Back</a> ";
}
for($i=1; $i<=$Num_Pages; $i++){
if($i != $Page)
{
echo "[ <a href='$_SERVER[SCRIPT_NAME]?Page=$i&txtKeyword=$_GET[txtKeyword]'>$i</a> ]";
}
else
{
echo "<b> $i </b>";
}
}
if($Page!=$Num_Pages)
{
echo " <a href ='$_SERVER[SCRIPT_NAME]?Page=$Next_Page&txtKeyword=$_GET[txtKeyword]'>Next>></a> ";
}
mysql_close($objConnect);
?>
</body>
</html>