 |
|
Code (PHP)
<?php
//start the session
session_start();
//check to make sure the session variable is registered
if(session_is_registered('user')){ ?>
<html>
<head>
<title>ThaiCreate.Com PHP & MySQL Tutorial</title>
</head>
<body>
<form action="<?=$_SERVER['SCRIPT_NAME'];?>" method="post">
<select name="select" onChange="this.form.submit()">
<option value="sos">Choose Database</option>
<option value="website">website</option>
<option value="b">b</option>
<option value="c">c</option>
</select>
</form>
<?
$objConnect = mysql_connect("localhost","root","9999") or die("Error Connect to Database");
$objDB = mysql_select_db("mydatabase");
$tablename = $_POST["select"];
echo $tablename;
//*** Add Condition ***//
if($_POST["hdnCmd"] == "Add")
{
$strSQL = "INSERT INTO ".$tablename." ";
$strSQL .="(WebID,LinkWeb,Description) ";
$strSQL .="VALUES ";
$strSQL .="('".$_POST["txtAddWebID"]."','".$_POST["txtAddLinkWeb"]."','".$_POST["txtAddDescription"]."')";
$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 ".$tablename." SET ";
$strSQL .="WebID = '".$_POST["txtEditWebID"]."' ";
$strSQL .=",LinkWeb = '".$_POST["txtEditLinkWeb"]."' ";
$strSQL .=",Description = '".$_POST["txtEditDescription"]."' ";
$strSQL .="WHERE WebID = '".$_POST["hdnEditCustomerID"]."' ";
$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 ".$tablename." ";
$strSQL .="WHERE WebID = '".$_GET["CusID"]."' ";
$objQuery = mysql_query($strSQL);
if(!$objQuery)
{
echo "Error Delete [".mysql_error()."]";
}
//header("location:$_SERVER[PHP_SELF]");
//exit();
}
$strSQL = "SELECT * FROM ".$tablename."";
$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="700" border="1">
<tr>
<th width="91"> <div align="center">WebID </div></th>
<th width="98"> <div align="center">LinkWeb </div></th>
<th width="458"> <div align="center">Description </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["WebID"] == $_GET["CusID"] and $_GET["Action"] == "Edit")
{
?>
<tr>
<td><div align="center">
<input type="text" name="txtEditWebID" size="5" value="<?=$objResult["WebID"];?>">
<input type="hidden" name="hdnEditCustomerID" size="5" value="<?=$objResult["WebID"];?>">
</div></td>
<td><input type="text" name="txtEditLinkWeb" size="40" value="<?=$objResult["LinkWeb"];?>"></td>
<td><input type="text" name="txtEditDescription" size="40" value="<?=$objResult["Description"];?>"></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["WebID"];?></div></td>
<td><?=$objResult["LinkWeb"];?></td>
<td><?=$objResult["Description"];?></td>
<td align="center"><a href="<?=$_SERVER["PHP_SELF"];?>?Action=Edit&CusID=<?=$objResult["WebID"];?>">Edit</a></td>
<td align="center"><a href="JavaScript:if(confirm('Confirm Delete?')==true){window.location='<?=$_SERVER["PHP_SELF"];?>?Action=Del&CusID=<?=$objResult["WebID"];?>';}">Delete</a></td>
</tr>
<?
}
?>
<?
}
?>
<tr>
<td><div align="center"><input type="text" name="txtAddWebID" size="5"></div></td>
<td><input type="text" name="txtAddLinkWeb" size="40"></td>
<td><input type="text" name="txtAddDescription" size="40"></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);
?>
<a href ="Search.php" >Search</a><br>
<a href ="Logout.php" >Logout</a>
</body>
</html>
<?php
}
else{
//the session variable isn't registered, send them back to the login page
echo "<script>window.location='login.html'</script>";
}
?>
ถ้าเลือกฐานข้อมูลมาดูเฉยๆมันแสดงผลได้ตามปกติ
แต่พอผมต้องการเพิ่ม แก้ไข ข้อมูลลงฐานข้อมูลมัน 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 '(WebID,LinkWeb,Description) VALUES ('10','s','s')' at line 1]Error Query [SELECT * FROM ]
Tag : - - - -
|
|
 |
 |
 |
 |
Date :
2010-03-19 09:37:12 |
By :
tehman |
View :
915 |
Reply :
15 |
|
 |
 |
 |
 |
|
|
|
 |