แก้ error ในส่วนของโค้ดเพิ่มข้อมูล และค้นหา ให้หน่อยค่ะ รบกวนหน่อยนะๆๆๆๆๆๆๆๆๆๆๆ
โค้ดทั้งหมดในฟอร์มพนักงาน (employee.php)
Code (PHP)
<html>
<head>
<title>¢éÍÁÙÅÅÙ¡¤éÒ</title>
</head>
<body>
<?
$objConnect = mysql_connect("localhost","root","1234") or die("äÁèÊÒÁÒöµÔ´µèÍ°Ò¹¢éÍÁÙÅä´é");
$objDB = mysql_select_db("mydatabase");
?>
<form name="frmSearch" method="get" action="<?=$_SERVER['SCRIPT_NAME'];?>">
<table width="599" border="1">
<tr>
<th>Name
<input name="txtKeyword" type="text" id="txtKeyword" value="<?=$_GET["txtKeyword"];?>">
<input type="submit" value="Search"></th>
</tr>
</table>
</form>
<?
if($_GET["txtKeyword"] != "")
{
mysql_connect("localhost","root","1234") or die("Error Connect to Database");
$objDB = mysql_select_db("mydatabase");
// Search By name or id
$strSQL = "SELECT * FROM customer WHERE (name LIKE '%".$_GET["txtKeyword"]."%' or id LIKE '%".$_GET["txtKeyword"]."%' )";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
?>
<table width="600" border="1">
<tr>
<th width="91"> <div align="center">id </div></th>
<th width="98"> <div align="center">name </div></th>
<th width="198"> <div align="center">surname </div></th>
<th width="97"> <div align="center">address </div></th>
<th width="59"> <div align="center">phone </div></th>
<th width="71"> <div align="center">status</div></th>
</tr>
<?
while($objResult = mysql_fetch_array($objQuery))
{
?>
<tr>
<td><div align="center"><?=$objResult["id"];?></div></td>
<td><?=$objResult["name"];?></td>
<td><?=$objResult["surname"];?></td>
<td><div align="center"><?=$objResult["address"];?></div></td>
<td align="right"><?=$objResult["phone"];?></td>
<td align="right"><?=$objResult["status"];?></td>
</tr>
<?
}
?>
</table>
<?
mysql_close($objConnect);
}
?>
?>
<?
//*** Add Condition ***//
if($_POST["hdnCmd"] == "Add")
{
$strSQL = "INSERT INTO customer ";
$strSQL .="(id,name,surname,address,phone,status) ";
$strSQL .="VALUES ";
$strSQL .="('".$_POST["txtAddid"]."','".$_POST["txtAddname"]."' ";
$strSQL .=",'".$_POST["txtsurname"]."' ";
$strSQL .=",'".$_POST["txtAddaddress"]."','".$_POST["txtAddphone"]."' ";
$strSQL .=",'".$_POST["txtAddstatus"]."') ";
$objQuery = mysql_query($strSQL);
if(!$objQuery)
{
echo "Error Save [".mysql_error()."]";
}
//header("location:$_SERVER[PHP_SELF]");
//exit();
}
//*** Update Condition ***//
if($_POST["hdnCmd"] == "Update")
{
$strSQL = "UPDATE customer SET ";
$strSQL .="id= '".$_POST["txtEditid"]."' ";
$strSQL .=",name = '".$_POST["txtEditname"]."' ";
$strSQL .=",surname = '".$_POST["txtEditsurname"]."' ";
$strSQL .=",address = '".$_POST["txtEditaddress"]."' ";
$strSQL .=",phone = '".$_POST["txtEditphone"]."' ";
$strSQL .=",status= '".$_POST["txtEditstatus"]."' ";
$strSQL .="WHERE id = '".$_POST["hdnEditid"]."' ";
$objQuery = mysql_query($strSQL);
if(!$objQuery)
{
echo "Error Update [".mysql_error()."]";
}
//header("location:$_SERVER[PHP_SELF]");
//exit();
}
//*** Delete Condition ***//
if($_GET["Action"] == "Del")
{
$strSQL = "DELETE FROM customer ";
$strSQL .="WHERE id = '".$_GET["id"]."' ";
$objQuery = mysql_query($strSQL);
if(!$objQuery)
{
echo "Error Delete [".mysql_error()."]";
}
//header("location:$_SERVER[PHP_SELF]");
//exit();
}
$strSQL = "SELECT * FROM customer";
$objQuery = mysql_query($strSQL) or die ("Error 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">id </div></th>
<th width="98"> <div align="center">name </div></th>
<th width="198"> <div align="center">surname </div></th>
<th width="97"> <div align="center">address </div></th>
<th width="59"> <div align="center">phone </div></th>
<th width="71"> <div align="center">status</div></th>
<th width="30"> <div align="center">Edit </div></th>
<th width="30"> <div align="center">Delete </div></th>
</tr>
<?
while($objResult = mysql_fetch_array($objQuery))
{
?>
<?
if($objResult["id"] == $_GET["id"] and $_GET["Action"] == "Edit")
{
?>
<tr>
<td><div align="center">
<input type="text" name="txtEditid" size="5" value="<?=$objResult["id"];?>">
<input type="hidden" name="hdnEditid" size="5" value="<?=$objResult["id"];?>">
</div></td>
<td><input type="text" name="txtEditname" size="20" value="<?=$objResult["name"];?>"></td>
<td><input type="text" name="txtEditsurname" size="20" value="<?=$objResult["surname"];?>"></td>
<td><div align="center"><input type="text" name="txtEditaddress" size="2" value="<?=$objResult["address"];?>"></div></td>
<td align="right"><input type="text" name="txtEditphone" size="5" value="<?=$objResult["phone"];?>"></td>
<td align="right"><input type="text" name="txtEditstatus" size="5" value="<?=$objResult["status"];?>"></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["id"];?></div></td>
<td><?=$objResult["name"];?></td>
<td><?=$objResult["surname"];?></td>
<td><div align="center"><?=$objResult["address"];?></div></td>
<td align="right"><?=$objResult["phone"];?></td>
<td align="right"><?=$objResult["status"];?></td>
<td align="center"><a href="<?=$_SERVER["PHP_SELF"];?>?Action=Edit&id=<?=$objResult["id"];?>">Edit</a></td>
<td align="center"><a href="JavaScript:if(confirm('Confirm Delete?')==true){window.location='<?=$_SERVER["PHP_SELF"];?>?Action=Del&id=<?=$objResult["id"];?>';}">Delete</a></td>
</tr>
<?
}
?>
<?
}
?>
<tr>
<td><div align="center"><input type="text" name="txtAddid" size="5"></div></td>
<td><input type="text" name="txtAddname" size="20"></td>
<td><input type="text" name="txtAddsurname" size="20"></td>
<td><div align="center"><input type="text" name="txtAddaddress" size="50"></div></td>
<td align="right"><input type="text" name="txtAddphone" size="5"></td>
<td align="right"><input type="text" name="txtAddstatus" size="5"></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>
</form>
<?
mysql_close($objConnect);
?>
</body>
</html>
คือจะทำหน้าฟอร์มพนักงานที่สามารถเพิ่มลบแก้ไขและค้นหาได้อะค่ะ ในส่วนของการเพิ่มข้อมูล พอเพิ่มข้อมูลผ่านหน้าฟอร์มแร้ว error ว่า Error Save [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1] ช่วยหน่อยค่ะ งงเลยแก้ไม่ถูก
และพอค้นหาข้อมูลพนักงานสามารถค้นหาได้แต่จะมี error ว่า Error Query [SELECT * FROM employee] ขึ้นมาด้วยตลอด
รบกวนช่วยแก้ให้หน่อยค่ะTag : - - - -
Date :
2010-02-09 11:23:48
By :
sitorn2170
View :
897
Reply :
0
Load balance : Server 02