|
|
|
การเขียน Function เพื่อ insert delete edit update #Database มันขึ้นหน้าขาวววววอะคับ |
|
|
|
|
|
|
|
ตอนแรกเขียน Insert ข้อมูลให้เข้าฐาน
ก็ใช้ CustomerForm.php
ก็สามารถ Insert ข้อมูลเข้าแล้วนะครับ
ทีนี้จะเขียนให้มัน edit ข้อมูลของลูกค้าได้
พอกดคลิ๊กเข้าไป edit มันก็ขึ้นหน้าขาวเลยอะครับ
UpdateCustomer.php
Code (PHP)
<html>
<head>
<meta http-equiv="Content-Type"content="text/html" charset="utf-8">
<title> UpdateCustomer </title>
</head>
<?php
include_once("connect.php");
include_once("CustomerClass.php");
if(isset($_POST['update']))
{
$objCustomer = new Customer();
$objCustomer->CustNo = $_POST['CustNo'];
$objCustomer->CustFirstName = $_POST['CustFirstName'];
$objCustomer->CustLastName = $_POST['CustLastName'];
$objCustomer->CustAddress = $_POST['CustAddress'];
$objCustomer->CustSubDistrict = $_POST['CustSubDistrict'];
$objCustomer->CustDistrict = $_POST['CustDistrict'];
$objCustomer->CustProvince = $_POST['CustProvince'];
$objCustomer->CustZipCode = $_POST['CustZipCode'];
$objCustomer->update();
$objCustomer->close();
}
elseif($_GET['Action']=='Delete')
{
$objCustomer = new Customer();
$objCustomer->connect();
$objCustomer->delete($_GET['CustNo']);
$objCustomer->close();
}
elseif($_GET['Action']=="Edit")
{
$objCustomer = new Customer();
$objCustomer->connect();
$sql = "SELECT * FROM Customer WHERE CustNo ='".$_GET['CustNo']."'";
$objCustomer->query($sql);
$row = mysql_fetch_array($objCustomer->results,MYSQL_ASSOC);
?>
<body bgcolor='CCFF66'> <br><br><br>
<center>
<font>แก้ไข Customer</font>
<form method="post" name="Customer" action="UpdateCustomer.php">
CustNo: <input type="text" name="CustNo" value=" <?php .$row['CustNo']. ?> "><br>
CustFirstName: <input type="text" name="CustFirstName" value=" <?php .$row['CustFirstName']. ?> "><br>
CustLastName: <input type="text" name="CustLastName" value=" <?php .$row['CustLastName']. ?> "><br>
CustAddress: <input type="text" name="CustAddress" value=" <?php .$row['CustAddress']. ?> "><br>
CustSubDistinct: <input type="text" name="CustSubDistinct" value=" <?php .$row['CustSubDistinct']. ?> "><br>
CustDistinct: <input type="text" name="CustDistinct" value=" <?php .$row['CustDistinct']. ?> "><br>
CustProvince: <input type="text" name="CustProvince" value=" <?php .$row['CustProvince']. ?> "><br>
CustZipCode: <input type="text" name="CustZipCode" value=" <?php .$row['CustZipCode']. ?> "><br>
<input type="submit" name="update" value="ok">
<input type="reset" value="reset">
</form>
<?php
}
?>
</center>
</body>
</html>
CustomerClass.php
Code (PHP)
<?php
include_once("connect.php");
class Customer extends db {
var $CustNo;
var $CustFirstName;
var $CustLastName;
var $CustAddress;
var $CustSubDistrict;
var $CustDistrict;
var $Custprovince;
var $CustZipCode;
function insert() {
$sql = "INSERT INTO Customer VALUES ('" .$this->CustNo ."','"
.$this->CustFirstName ."','"
.$this->CustLastName ."','"
.$this->CustAddress ."','"
.$this->CustSubDistrict ."','"
.$this->CustDistrict ."','"
.$this->Custprovince ."','"
.$this->CustZipCode ."')";
if ($this->results = mysql_query($sql)){
return true;
} else {
echo "Could not insert into Customer";
return false;
}
}
function update(){
$sql = "UPDATE Customer SET
CustNo = '" .$this->CustNo."',"
."CustFirstName='" .$this->CustFirstName. "',"
."CustAddress='" .$this->CustAddress. "',"
."CustSubDistrict='" .$this->CustSubDistrict. "',"
."CustDistrict='" .$this->CustDistrict. "',"
."Custprovince='" .$this->Custprovince. "',"
."CustZipCode='" .$this->CustZipCode. "',"
." WHERE CustNo='" .$this->CustNo."'";
if ($this->results = mysql_query($sql)){
return true;
} else {
echo "Could not delete customer" . $this->$CustNo . "\n" ;
return false;
}
}
}
function delete($CustNo){
$sql = " DELETE FROM Customer WHERE CustNo=' " .$CustNo. " ' ";
if ($this->results = mysql_query($sql)){
return true;
} else {
echo "Could not delete customer" . $this->$CustNo . "\n" ;
return false;
}
}
?>
Customer.php
Code (PHP)
<?php
include("connect.php");
$Connection = new db();
$Connection->connect();
$sql = "SELECT * FROM Customer";
$Connection->query($sql);
$numrows = mysql_num_rows($Connection->results);
echo "<html>";
echo "<head>";
echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">";
echo "</head>";
echo "<center>";
echo "<body bgcolor=\"FFCC33\">";
echo "<br>";
echo "ค้นหาตามรหัส : <INPUT TYPE=text NAME=\"SearchCustNo\" WIDTH=100>"." ".
"ค้นหาตามชื่อ : <INPUT TYPE=text NAME=\"SearchCustName\" WIDTH=100><INPUT TYPE=submit NAME=\"SearchInfo\" VALUE=\"ค้นหา\">"."<br><br>";
echo "<table border=\"1\" style=\"margin: 0 auto\" width=\"90%\" height=\"400\">";
echo "<tr align=center>";
echo "<td> CustNo </td>";
echo "<td> CustFirstName </td>";
echo "<td> CustLastName </td>";
echo "<td> CustAddress </td>";
echo "<td> CustSubDistinct </td>";
echo "<td> CustDistinct </td>";
echo "<td> CustProvince </td>";
echo "<td> CustZipCode </td>";
echo "<td> Consonle </td>";
echo "</tr>";
for($i=0 ; $i<$numrows ; $i++)
{
$row = mysql_fetch_array($Connection->results,MYSQL_ASSOC);
echo "<tr align=center>";
echo "<td>" . $row['CustNo'] . "</td>";
echo "<td>" . $row['CustFirstName'] . "</td>";
echo "<td>" . $row['CustLastName'] . "</td>";
echo "<td>" . $row['CustAddress'] . "</td>";
echo "<td>" . $row['CustSubDistinct'] . "</td>";
echo "<td>" . $row['CustDistinct'] . "</td>";
echo "<td>" . $row['CustProvince'] . "</td>";
echo "<td>" . $row['CustZipCode'] . "</td>";
echo "<td>" . "<a href=\"UpdateCustomer.php\"><input type=\"image\" src=\"edit.png\" name=\"Edit\" width=\"30px\" height=\"30px\">" . " " .
"<a href=\"UpdateCustomer.php\"><input type=\"image\" src=\"delete.png\" name=\"Delete\" width=\"30px\" height=\"30px\">" . "</td>";
}
echo "</tr>";
echo "</table>";
echo "<tr align=center>";
echo "<br>";
echo "<td colspan=10>" . "<a href=\"CustomerForm.php\"><input type=\"image\" src=\"insert.png\" name=\"insert\" width=\"30px\" height=\"30px\">" . "</td>";
echo "</tr>";
echo "</center>";
$Connection->close();
?>
Tag : PHP, HTML/CSS
|
ประวัติการแก้ไข 2014-11-17 19:40:25
|
|
|
|
|
Date :
2014-11-17 19:38:32 |
By :
xgabpyz |
View :
1258 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ไม่มีอะไรครับ แค่ code php error
Code (PHP)
CustNo: <input type="text" name="CustNo" value=" <?php .$row['CustNo']. ?> "><br>
CustFirstName: <input type="text" name="CustFirstName" value=" <?php .$row['CustFirstName']. ?> "><br>
CustLastName: <input type="text" name="CustLastName" value=" <?php .$row['CustLastName']. ?> "><br>
CustAddress: <input type="text" name="CustAddress" value=" <?php .$row['CustAddress']. ?> "><br>
CustSubDistinct: <input type="text" name="CustSubDistinct" value=" <?php .$row['CustSubDistinct']. ?> "><br>
CustDistinct: <input type="text" name="CustDistinct" value=" <?php .$row['CustDistinct']. ?> "><br>
CustProvince: <input type="text" name="CustProvince" value=" <?php .$row['CustProvince']. ?> "><br>
CustZipCode: <input type="text" name="CustZipCode" value=" <?php .$row['CustZipCode']. ?> "><br>
.$row ใส่ จุดข้างหน้า $row ทำไมครับ
ปล. อ้อ ข้างหลัง ]. ด้วย
Code (PHP)
<?php echo $row['CustZipCode']; ?>
|
ประวัติการแก้ไข 2014-11-18 09:08:33 2014-11-18 09:09:19
|
|
|
|
Date :
2014-11-18 09:07:50 |
By :
Chaidhanan |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 02
|