|
 |
|
อยากเพิ่ม แก้ไข กับ ลบ ให้ไปอยู่ในตาราง (PHP) |
|
 |
|
|
 |
 |
|
อยากจะเพิ่มคำสั่งลบ และ แก้ไข ให้ไปอยู่ในตาราง (ตอนนี้ผมทำเป็นปุ่มเอาไว้) พอกดลบ ก็จะถามเลยว่า ต้องการลบจริงหรือไม่ แต่ถ้ากดแก้ไข ก็ให้เอาข้อมูลของแถวนั้นมาไว้ textbox ครับ
แบบนี้ครับ (ยืมรูปเขามา ขอบคุณรูปจาก : .basic-skill.com)
ส่วนด้านล่างนี่คือ code ที่ทำไว้ครับ
Code
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<?php
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_USER', 'root');
DEFINE ('DB_PASSWORD', 'Admin');
DEFINE ('DB_NAME', 'test');
$conn = mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
mysqli_set_charset($conn, 'utf8');
if(isset($_GET['id'])){
$sql = "select * from department where DNUMBER={$_GET['id']}";
echo $sql;
$result = mysqli_query($conn,$sql);
$dataLoad = mysqli_fetch_array($result);
}
else if(isset($_POST['delete'])){
$sql = "delete from department where DNUMBER={$_POST['dnumber']}";
echo $sql;
mysqli_query($conn,$sql);
}
else if(isset($_POST['update'])){
$sql = "update department set DNAME='{$_POST['Dname']}', MGRSSN='{$_POST['Mgrssn']}', MGRSTARTDATE='{$_POST['Mgrs']}' where DNUMBER={$_POST['Dnumber']}";
echo $sql;
mysqli_query($conn,$sql);
}
else if(isset($_POST['add'])){
$sql = 'select max(Dnumber)+1 as nextssn from department';
$result = mysqli_query($conn,$sql);
$array = mysqli_fetch_array($result);
if($array['nextDnumber']==null){
$newDnumber = '1';
}
else{
$newDnumber = sprintf('%09s',$array['nextDnumber']);
}
$sql = "insert into department (DNUMBER, DNAME, MGRSSN, MGRSTARTDATE) values ('$newDnumber','{$_POST['Dname']}','{$_POST['Mgrssn']}',{$_POST['Mgrs']})";
echo $sql;
mysqli_query($conn,$sql) or die(mysqli_error($conn));
}
$sql = 'SELECT d.*,DATE_FORMAT(d.MGRSTARTDATE,\'%d/%m/%Y\') AS DSTART, e.FNAME AS \'cname\' FROM DEPARTMENT d, EMPLOYEE e WHERE e.SSN = d.MGRSSN';
$result = mysqli_query($conn, $sql);
$html = '<table border=1>';
$html .= '<tr><th>รหัสแผนก</th><th>ชื่อแผนก</th><th>รหัสหัวหน้าแผนก</th><th>วันเริ่มงาน</th><th>แก้ไข</th></tr>';
while($array=mysqli_fetch_array($result)){
$html = $html ."<tr><td>{$array['DNUMBER']}</td>";
$html = $html ."<td>{$array['DNAME']}</td>";
$html = $html ."<td>{$array['cname']}</td>";
$html = $html ."<td>{$array['DSTART']}</td></tr>";
}
$html = $html . '</table>';
if(mysqli_num_rows($result)>0)
echo $html;
?>
<form action="department.php" method="POST">
<table>
<tr><td>รหัสแผนก *</td> <td> <input type="hidden" name="Dnumber" maxlength="11" value='<?php if(isset($dataLoad)) echo $dataLoad['DNUMBER'];?>'/><?php if(isset($dataLoad)) echo $dataLoad['DNUMBER'];?></td></tr>
<tr><td>ชื่อแผนก *</td><td><input type="text" name="Dname" maxlength="15" value='<?php if(isset($dataLoad)) echo $dataLoad['DNAME'];?>'/></td></tr>
<tr> <td>แผนก *</td>
<td>
<?php
$sql = 'select * from employee';
$result = mysqli_query($conn, $sql);
$html = '<select name="Mgrssn">';
while($array=mysqli_fetch_array($result)){
if(isset($dataLoad) && $dataLoad['MGRSSN']==$array['SSN'])
$select = 'selected';
else
$select = '';
$html .= "<option value=\"{$array['SSN']}\" $select>{$array['FNAME']}</option>";
}
$html .= '</select>';
echo $html;
?>
<tr><td>วันเริ่มงาน *</td><td><input type="text" name="Mgrs" maxlength="20" value='<?php if(isset($dataLoad)) echo $dataLoad['MGRSTARTDATE'];?>'/></td></tr>
</td></tr>
<tr><td colspan="2"><input type="submit" name="add" value="เพิ่ม"/><input type="submit" name="update" value="แก้ไข"/><input type="submit" name="delete" value="ลบ"/></td></tr>
</table>
<?php
mysqli_close($conn);
?>
</form>
</body>
</html>
Tag : PHP, MySQL

|
|
 |
 |
 |
 |
Date :
2012-04-04 07:17:06 |
By :
ไร้วรยุทธ |
View :
3822 |
Reply :
1 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
|
|

|
Load balance : Server 01
|