|
|
|
ช่วยด้วยค่ะ ทำ search ชื่อ ก่อนออกมาเป็นตาราง แบบ [Search/Insert/Edit/Update/Delete] ทำไงค่ะ |
|
|
|
|
|
|
|
Code (PHP)
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>จัดการข้อมูลนักเรียน</title>
</head>
<body>
<?
$host = "localhost";
$username = "root";
$password = "1234";
$dbname = "tonkla_db";
$tblname = "student";
$link = mysql_connect($host,$username,$password);
mysql_query("SET NAMES UTF8");
mysql_select_db($dbname,$link);
$result = mysql_query($query,$link);
//$num_rows = mysql_num_rows($result);
//$objQuery = mysql_query($query);
//$objResult = mysql_fetch_array($objQuery);
?>
<form name="frmSearch" method="get" action="<?=$_SERVER['SCRIPT_NAME'];?>">
<table width="599" border="1" align="center">
<tr>
<th>Keyword
<input name="txkeyword" type="text" id="txkeyword" value="<?=$_GET["txkeyword"];?>" />
<input type="submit" value="Search" /></th>
</tr>
</table><br />
</form>
<?
if($_GET["txkeyword"] != "")
{
$objConnect = mysql_connect("localhost","root","1234") or die ("Error Connect to Database");
$objDB = mysql_select_db("tonkla_db");
$strSQL = " SELECT * FROM student WHERE (stu_name LIKE '%".$_GET["txkeyword"]. "%' )";
mysql_query("SET NAMES UTF8");
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
$Num_Rows = mysql_num_rows($objQuery);
$Per_Page = 5;
$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)
{
$Next_Page = 1;
}
else if(($Num_Rows % $Per_Page)==0)
{
$Num_Page = ($Num_Rows/$Per_Page);
}
else
{
$Num_Page = ($Num_Rows/$Per_Page)+1;
$Num_Page = (int)$Num_Page;
}
?>
<?
//add
if($_POST["hdnCmd"]=="Add")
{
$strSQL = " INSERT INTO student ";
$strSQL .= " (stu_id, stu_pname, stu_name, stu_sex, stu_password, stu_address, stu_tel, stu_parent, stu_class, tea_id) ";
$strSQL .= " values ";
$strSQL .= " ('".$_POST["atxstu_id"]."','".$_POST["atxstu_pname"]."' ";
$strSQL .= " ,'".$_POST["atxstu_name"]."' ";
$strSQL .= " ,'".$_POST["atxstu_sex"]."','".$_POST["atxstu_password"]."' ";
$strSQL .= " ,'".$_POST["atxstu_address"]."','".$_POST["atxstu_tel"]."' ";
$strSQL .= " ,'".$_POST["atxstu_parent"]."','".$_POST["atxstu_class"]."' ";
$strSQL .= " ,'".$_POST["atxtea_id"]."') ";
$objQuery = mysql_query($strSQL);
if(!$objQuery)
{
echo " error save [".mysql_error()."] ";
}
}
//update
if($_POST["hdnCmd"] == "Update")
{
$strSQL = " UPDATE student SET ";
$strSQL .= " stu_id = '".trim($_POST["etxstu_id"])."' ";
$strSQL .= " ,stu_pname = '".trim($_POST["etxstu_pname"])."' ";
$strSQL .= " ,stu_name = '".trim($_POST["etxstu_name"])."' ";
$strSQL .= " ,stu_sex = '".trim($_POST["etxstu_sex"])."' ";
$strSQL .= " ,stu_password = '".trim($_POST["etxstu_password"])."' ";
$strSQL .= " ,stu_address = '".trim($_POST["etxstu_address"])."' ";
$strSQL .= " ,stu_tel = '".trim($_POST["etxstu_tel"])."' ";
$strSQL .= " ,stu_parent = '".trim($_POST["etxstu_parent"])."' ";
$strSQL .= " ,stu_class = '".trim($_POST["etxstu_class"])." '";
$strSQL .= " ,tea_id = '".trim($_POST["etxtea_id"])." '";
$strSQL .= " WHERE stu_id = '".$_POST["hdnEditstu_id"]."' ";
$objQuery = mysql_query($strSQL);
if(!$objQuery)
{
echo " error update [".mysql_error()."] ";
}
}
//delete
if($_GET["Action"]== "Del")
{
$strSQL = " DELETE FROM student ";
$strSQL .= " WHERE stu_id = '".$_GET["stuID"]."' ";
$objQuery = mysql_query($strSQL);
if(!$objQuery)
{
echo " error delete [".mysql_error()."] ";
}
}
$strSQL = " select * from student ";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
?>
<?
$strSQL .= " order by stu_id ASC LIMIT $Page_Start , $Per_Page ";
$objQuery = mysql_query($strSQL);
?>
<form name="frmMain" method="post" action="<?=$_SERVER["PHP_SELF"];?>">
<input type="hidden" name="hdnCmd" value="">
<table width="600" border="1">
<tr>
<th width="91"> <div align="center">รหัสนักเรียน</div></th>
<th width="98"> <div align="center">คำนำหน้าชื่อ</div></th>
<th width="198"> <div align="center">ชื่อ-สกุล</div></th>
<th width="91"> <div align="center">เพศ</div></th>
<th width="97"> <div align="center">Password</div></th>
<th width="91"> <div align="center">ที่อยู่ปัจจุบัน</div></th>
<th width="91"> <div align="center">เบอร์โทรศัพท์</div></th>
<th width="91"> <div align="center">ชื่อผู้ปกครอง</div></th>
<th width="91"> <div align="center">ห้องเรียน</div></th>
<th width="91"> <div align="center">รหัสครูผู้ดูแล</div></th>
<th width="91"> <div align="center">ชื่อครูผู้ดูแล</div></th>
<th width="91"> <div align="center">Edit</div></th>
<th width="91"> <div align="center">Delete</div></th>
</tr>
<?
while($objResult = mysql_fetch_array($objQuery))
{
?>
<?
if($objResult["stu_id"] == $_GET["stuID"] and $_GET["Action"] == "Edit")
{
?>
<tr>
<td><div align="center">
<input name = "etxstu_id" type = "text" value = " <?=$objResult["stu_id"];?> " size="10" maxlength="5" >
<input type = "hidden" name = "hdnEditstu_id" value =" <?=$objResult["stu_id"];?> " ></div></td>
<td><input name = "etxstu_pname" type = "text" value = " <?=$objResult["stu_pname"];?> " size="10"></td>
<td><input name = "etxstu_name" type = "text" value = " <?=$objResult["stu_name"];?> " size="25"> </td>
<td><div align="center"> <input name = "etxstu_sex" type = "text" value = " <?=$objResult["stu_sex"];?> " size="10"></div></td>
<td><input name = "etxstu_password" type = "text" value = " <?=$objResult["stu_password"];?> " size="20" maxlength="13"></td>
<td><textarea name="etxstu_address"> <?=$objResult["stu_address"];?> </textarea></td>
<td> <input name = "etxstu_tel" type = "text" value = " <?=$objResult["stu_tel"];?> " size="20" maxlength="10"></td>
<td> <input name = "etxstu_parent" type = "text" value = " <?=$objResult["stu_parent"];?> " size="20"></td>
<td> <input name = "etxstu_class" type = "text" value = " <?=$objResult["stu_class"];?> " size="7"></td>
<td> <input name = "etxtea_id" type = "text" value = " <?=$objResult["tea_id"];?> " size="10"></td>
<td> <input name = "etxtea_name" type = "text" value = " <?=$objResult["tea_name"];?> " size="20"></td>
<td colspan="2" align="right"><div align="center">
<input name = "btnAdd" type = "button" id = "btnUpdate" value = "Update" onClick = " frmMain.hdnCmd.value='Update';frmMain.submit(); " >
<input name = "btnAdd" type = "button" id = "btnCancel" value = "Cancel" onClick = " window.location='<?=$_SERVER["PHP_SELF"];?>'; " ></div></td>
</tr>
<?
}
else
{
?>
<tr>
<td><div align="center"><?=$objResult["stu_id"];?></div></td>
<td><?=$objResult["stu_pname"];?></td>
<td><?=$objResult["stu_name"];?></td>
<td><div align="center"><?=$objResult["stu_sex"];?></div></td>
<td align="left"><?=$objResult["stu_password"];?></td>
<td align="left"><?=$objResult["stu_address"];?></td>
<td align="left"><?=$objResult["stu_tel"];?></td>
<td align="left"><?=$objResult["stu_parent"];?></td>
<td align="center"><?=$objResult["stu_class"];?></td>
<td align="center"><?=$objResult["tea_id"];?></td>
<td><?=$objResult["tea_name"];?></td>
<td align="center"><a href="<?=$_SERVER["PHP_SELF"];?>?Action=Edit&stuID=<?=$objResult["stu_id"];?>">Edit</a></td>
<td align="center"><a href="JavaScript:if(confirm('Confirm Delete?')==true){window.location='<?=$_SERVER["PHP_SELF"];?>?Action=Del&stuID=<?=$objResult["stu_id"];?>';}">Delete</a></td></tr>
<?
}
?>
<?
}
?>
<?
}
?>
<tr>
<td><div align="center"><input name="atxstu_id" type="text" size="10" maxlength="5"></div></td>
<td><input name="atxstu_pname" type="text" size="10"></td>
<td><input name="atxstu_name" type="text" size="25"></td>
<td><input name="atxstu_sex" type="text" size="10"></td>
<td><input name="atxstu_password" type="text" size="20" maxlength="13"></td>
<td><textarea name="atxstu_address"></textarea></td>
<td><input name="atxstu_tel" type="text" size="20" maxlength="10"></td>
<td><input name="atxstu_parent" type="text" size="20"></td>
<td><input name="atxstu_class" type="text" size="7"></td>
<td><input name="atxtea_id" type="text" size="10" maxlength="5"></td>
<td><input name="atxtea_name" type="text" size="20"></td>
<td colspan="2" align="right"><div align="center">
<input name="btnAdd" type="button" id="btnAdd" value="Add" onClick="frmMain.hdnCmd.value='Add';frmMain.submit();"></div></td></tr>
</table>
<br />
<center>Total <?=$Num_Rows;?> Record : <?=$Num_Pages;?> Page : </center>
<p>
<?
if($Prev_Page)
{
echo "<center><a href='$_SERVER[SCRIPT_NAME]?Page=$Prev_Page&txkeyword=$_GET[txkeyword]'> <<Back </a></center>";
}
for($i=1; $i<=$Num_Pages; $i++){
if($i != $Page)
{
echo "[ <a href=$_SERVER[SCRIPT_NAME]?Page=$i&txkeyword=$_GET[txkeyword]'>$i </a>]";
}
else
{
echo "<b> $i </b>";
}
}
if($Page != $Num_Pages)
{
echo "<center><a href='$_SERVER[SCRIPT_NAME]?Page=$Next_Page&txkeyword=$_GET[txkeyword]'> Next>> </a></center>";
}
//mysql_close($objConnect);
// }
?>
</form>
<?
mysql_close($link);
?>
</body>
</html>
|
|
|
|
|
Date :
2013-02-05 13:37:10 |
By :
นศ.ปี4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ตอบความคิดเห็นที่ : 2 เขียนโดย : mr.win เมื่อวันที่ 2013-02-05 13:57:16
รายละเอียดของการตอบ ::
รบกวนช่วยดูโค๊ด หน่อยน่ะค่ะ จากที่สามารถ จัดการข้อมูลได้ แต่พอเพิ่ม form Search มามันกลับ Add update Delete ไม่ได้สักกะอย่างเลยค่ะ
Code (PHP)
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>จัดการข้อมูลนักเรียน</title>
</head>
<body>
<?
$host = "localhost";
$username = "root";
$password = "1234";
$dbname = "tonkla_db";
$tblname = "student";
$link = mysql_connect($host,$username,$password);
mysql_query("SET NAMES UTF8");
mysql_select_db($dbname,$link);
$result = mysql_query($query,$link);
//$num_rows = mysql_num_rows($result);
//$objQuery = mysql_query($query);
//$objResult = mysql_fetch_array($objQuery);
?>
<form name="frmSearch" method="get" action="<?=$_SERVER['SCRIPT_NAME'];?>">
<table width="599" border="1" align="center">
<tr>
<th>Keyword
<input name="txkeyword" type="text" id="txkeyword" value="<?=$_GET["txkeyword"];?>" />
<input type="submit" value="Search" /></th>
</tr>
</table><br />
</form>
<?
if($_GET["txkeyword"] != "")
{
$objConnect = mysql_connect("localhost","root","1234") or die ("Error Connect to Database");
$objDB = mysql_select_db("tonkla_db");
$strSQL = " SELECT * FROM student WHERE (stu_name LIKE '%".$_GET["txkeyword"]. "%' )";
mysql_query("SET NAMES UTF8");
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
$Num_Rows = mysql_num_rows($objQuery);
$Per_Page = 5;
$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)
{
$Next_Page = 1;
}
else if(($Num_Rows % $Per_Page)==0)
{
$Num_Page = ($Num_Rows/$Per_Page);
}
else
{
$Num_Page = ($Num_Rows/$Per_Page)+1;
$Num_Page = (int)$Num_Page;
}
?>
<?
//add
if($_POST["hdnCmd"]=="Add")
{
$strSQL = " INSERT INTO student ";
$strSQL .= " (stu_id, stu_pname, stu_name, stu_sex, stu_password, stu_address, stu_tel, stu_parent, stu_class, tea_id) ";
$strSQL .= " values ";
$strSQL .= " ('".$_POST["atxstu_id"]."','".$_POST["atxstu_pname"]."' ";
$strSQL .= " ,'".$_POST["atxstu_name"]."' ";
$strSQL .= " ,'".$_POST["atxstu_sex"]."','".$_POST["atxstu_password"]."' ";
$strSQL .= " ,'".$_POST["atxstu_address"]."','".$_POST["atxstu_tel"]."' ";
$strSQL .= " ,'".$_POST["atxstu_parent"]."','".$_POST["atxstu_class"]."' ";
$strSQL .= " ,'".$_POST["atxtea_id"]."') ";
$objQuery = mysql_query($strSQL);
if(!$objQuery)
{
echo " error save [".mysql_error()."] ";
}
}
//update
if($_POST["hdnCmd"] == "Update")
{
$strSQL = " UPDATE student SET ";
$strSQL .= " stu_id = '".trim($_POST["etxstu_id"])."' ";
$strSQL .= " ,stu_pname = '".trim($_POST["etxstu_pname"])."' ";
$strSQL .= " ,stu_name = '".trim($_POST["etxstu_name"])."' ";
$strSQL .= " ,stu_sex = '".trim($_POST["etxstu_sex"])."' ";
$strSQL .= " ,stu_password = '".trim($_POST["etxstu_password"])."' ";
$strSQL .= " ,stu_address = '".trim($_POST["etxstu_address"])."' ";
$strSQL .= " ,stu_tel = '".trim($_POST["etxstu_tel"])."' ";
$strSQL .= " ,stu_parent = '".trim($_POST["etxstu_parent"])."' ";
$strSQL .= " ,stu_class = '".trim($_POST["etxstu_class"])." '";
$strSQL .= " ,tea_id = '".trim($_POST["etxtea_id"])." '";
$strSQL .= " WHERE stu_id = '".$_POST["hdnEditstu_id"]."' ";
$objQuery = mysql_query($strSQL);
if(!$objQuery)
{
echo " error update [".mysql_error()."] ";
}
}
//delete
if($_GET["Action"]== "Del")
{
$strSQL = " DELETE FROM student ";
$strSQL .= " WHERE stu_id = '".$_GET["stuID"]."' ";
$objQuery = mysql_query($strSQL);
if(!$objQuery)
{
echo " error delete [".mysql_error()."] ";
}
}
$strSQL .= " order by stu_id ASC LIMIT $Page_Start , $Per_Page ";
$objQuery = mysql_query($strSQL);
?>
<form name="frmMain" method="post" action="<?=$_SERVER["PHP_SELF"];?>">
<input type="hidden" name="hdnCmd" value="">
<table width="600" border="1">
<tr>
<th width="91"> <div align="center">รหัสนักเรียน</div></th>
<th width="98"> <div align="center">คำนำหน้าชื่อ</div></th>
<th width="198"> <div align="center">ชื่อ-สกุล</div></th>
<th width="91"> <div align="center">เพศ</div></th>
<th width="97"> <div align="center">Password</div></th>
<th width="91"> <div align="center">ที่อยู่ปัจจุบัน</div></th>
<th width="91"> <div align="center">เบอร์โทรศัพท์</div></th>
<th width="91"> <div align="center">ชื่อผู้ปกครอง</div></th>
<th width="91"> <div align="center">ห้องเรียน</div></th>
<th width="91"> <div align="center">รหัสครูผู้ดูแล</div></th>
<th width="91"> <div align="center">ชื่อครูผู้ดูแล</div></th>
<th width="91"> <div align="center">Edit</div></th>
<th width="91"> <div align="center">Delete</div></th>
</tr>
<?
while($objResult = mysql_fetch_array($objQuery))
{
?>
<?
if($objResult["stu_id"] == $_GET["stuID"] and $_GET["Action"] == "Edit")
{
?>
<tr>
<td><div align="center">
<input name = "etxstu_id" type = "text" value = " <?=$objResult["stu_id"];?> " size="10" maxlength="5" >
<input type = "hidden" name = "hdnEditstu_id" value =" <?=$objResult["stu_id"];?> " ></div></td>
<td><input name = "etxstu_pname" type = "text" value = " <?=$objResult["stu_pname"];?> " size="10"></td>
<td><input name = "etxstu_name" type = "text" value = " <?=$objResult["stu_name"];?> " size="25"> </td>
<td><div align="center"> <input name = "etxstu_sex" type = "text" value = " <?=$objResult["stu_sex"];?> " size="10"></div></td>
<td><input name = "etxstu_password" type = "text" value = " <?=$objResult["stu_password"];?> " size="20" maxlength="13"></td>
<td><textarea name="etxstu_address"> <?=$objResult["stu_address"];?> </textarea></td>
<td> <input name = "etxstu_tel" type = "text" value = " <?=$objResult["stu_tel"];?> " size="20" maxlength="10"></td>
<td> <input name = "etxstu_parent" type = "text" value = " <?=$objResult["stu_parent"];?> " size="20"></td>
<td> <input name = "etxstu_class" type = "text" value = " <?=$objResult["stu_class"];?> " size="7"></td>
<td> <input name = "etxtea_id" type = "text" value = " <?=$objResult["tea_id"];?> " size="10"></td>
<td> <input name = "etxtea_name" type = "text" value = " <?=$objResult["tea_name"];?> " size="20"></td>
<td colspan="2" align="right"><div align="center">
<input name = "btnAdd" type = "button" id = "btnUpdate" value = "Update" onClick = " frmMain.hdnCmd.value='Update';frmMain.submit(); " >
<input name = "btnAdd" type = "button" id = "btnCancel" value = "Cancel" onClick = " window.location='<?=$_SERVER["PHP_SELF"];?>'; " ></div></td>
</tr>
<?
}
else
{
?>
<tr>
<td><div align="center"><?=$objResult["stu_id"];?></div></td>
<td><?=$objResult["stu_pname"];?></td>
<td><?=$objResult["stu_name"];?></td>
<td><div align="center"><?=$objResult["stu_sex"];?></div></td>
<td align="left"><?=$objResult["stu_password"];?></td>
<td align="left"><?=$objResult["stu_address"];?></td>
<td align="left"><?=$objResult["stu_tel"];?></td>
<td align="left"><?=$objResult["stu_parent"];?></td>
<td align="center"><?=$objResult["stu_class"];?></td>
<td align="center"><?=$objResult["tea_id"];?></td>
<td><?=$objResult["tea_name"];?></td>
<td align="center"><a href="<?=$_SERVER["PHP_SELF"];?>?Action=Edit&stuID=<?=$objResult["stu_id"];?>">Edit</a></td>
<td align="center"><a href="JavaScript:if(confirm('Confirm Delete?')==true){window.location='<?=$_SERVER["PHP_SELF"];?>?Action=Del&stuID=<?=$objResult["stu_id"];?>';}">Delete</a></td></tr>
<?
}
}
}
?>
<tr>
<td><div align="center"><input name="atxstu_id" type="text" size="10" maxlength="5"></div></td>
<td><input name="atxstu_pname" type="text" size="10"></td>
<td><input name="atxstu_name" type="text" size="25"></td>
<td><input name="atxstu_sex" type="text" size="10"></td>
<td><input name="atxstu_password" type="text" size="20" maxlength="13"></td>
<td><textarea name="atxstu_address"></textarea></td>
<td><input name="atxstu_tel" type="text" size="20" maxlength="10"></td>
<td><input name="atxstu_parent" type="text" size="20"></td>
<td><input name="atxstu_class" type="text" size="7"></td>
<td><input name="atxtea_id" type="text" size="10" maxlength="5"></td>
<td><input name="atxtea_name" type="text" size="20"></td>
<td colspan="2" align="right"><div align="center">
<input name="btnAdd" type="button" id="btnAdd" value="Add" onClick="frmMain.hdnCmd.value='Add';frmMain.submit();"></div></td></tr>
</table>
<br />
<center>Total <?=$Num_Rows;?> Record : <?=$Num_Pages;?> Page : </center>
<p>
<?
if($Prev_Page)
{
echo "<center><a href='$_SERVER[SCRIPT_NAME]?Page=$Prev_Page&txkeyword=$_GET[txkeyword]'> <<Back </a></center>";
}
for($i=1; $i<=$Num_Pages; $i++){
if($i != $Page)
{
echo "[ <a href=$_SERVER[SCRIPT_NAME]?Page=$i&txkeyword=$_GET[txkeyword]'>$i </a>]";
}
else
{
echo "<b> $i </b>";
}
}
if($Page != $Num_Pages)
{
echo "<center><a href='$_SERVER[SCRIPT_NAME]?Page=$Next_Page&txkeyword=$_GET[txkeyword]'> Next>> </a></center>";
}
//mysql_close($objConnect);
// }
?>
</form>
<?
mysql_close($link);
?>
</body>
</html>
|
|
|
|
|
Date :
2013-02-05 14:04:30 |
By :
นศ.ปี4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 00
|