ใช้ php เพิ่ม ลบ แก้ไข

ตอบกระทู้

รูปแสดงอารมณ์
:icon_plusone: :like: :plusone: :gfb: :-D :) :( :-o 8O :? 8) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: :angry: :baa: :biggrin:
รูปแสดงอารมณ์อื่นๆ

BBCode เปิด
[img] เปิด
[url] เปิด
[Smile icon] เปิด

กระทู้แนะนำ
   

มุมมองที่ขยายได้ กระทู้แนะนำ: ใช้ php เพิ่ม ลบ แก้ไข

ใช้ php เพิ่ม ลบ แก้ไข

โดย rangsun6342 » 23/06/2014 11:47 pm


connect


<?php
$username = "root";
$password = "root";
$database = "new";
$host = "localhost";

$conn = mysql_connect($host,$username,$password);
mysql_select_db($database);
?>

show

<?php
include("connect.php");
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>


<body>
<a href="add.php">เพิ่มข้อมูล</a>


<table width="100%" border="1" cellspacing="0" cellpadding="0">
<tr>
<td>id</td>
<td>name</td>
<td>surname</td>
<td>salary</td>

</tr>

<?php
$sql_show = " select * from table1 as t1, table2 as t2 where t1.salary_id = t2.id";
#print $sql_show;
$res_show = mysql_db_query($database,$sql_show);
while($val_show = mysql_fetch_array($res_show)){
?>
<tr>
<td><a href="update.php?id=<?=$val_show["id"]?>"><?=$val_show["id"] ?></a></td>
<td><?=$val_show["name"] ?></td>
<td><?=$val_show["surname"] ?></td>
<td><?=$val_show["salary_name"] ?></td>
<td><a href="del.php?id=<?=$val_show["id"]?>">ลบ</a></td>
</tr>



<?php
}

?>
</table>
</body>


</html>

del

<?php
include("connect.php");


$id = $_GET["id"];




$sql_add = "DELETE FROM table1 where id='$id'";


#print $sql_add;


$res_add = mysql_db_query($database,$sql_add);
if($res_add){
print "OK<br>";
print "<a href=\"show.php\">BACK</a>";
}else{
print "FAIL !!!<br>";
print "<a href=\"show.php\">BACK</a>";
}


?>

add
<?php
include("connect.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form action="fn_add.php" method="post">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="32%">ชื่อ </td>
<td width="68%"><input type="text" name="name" /></td>
</tr>
<tr>
<td>นามสกุล</td>
<td><input type="text" name="surname" /></td>
</tr>
<tr>
<td>เงินเดือน</td>
<td>
<select name="salary">
<?php
$sql="select * from table2";
$res_show = mysql_db_query($database,$sql);
while($val_show = mysql_fetch_array($res_show)){
?>
<option value="<?=$val_show["id"]?>"><?=$val_show["salary_name"] ?></option>
<?php
}
?>
</select>
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>
<input type="submit" value="SAVE" />
<input type="reset" value="CLEAR" />
</td>
</tr>
</table>
</form>


</body>
</html>


fn_add
<?php
include("connect.php");

$name = $_POST["name"];
$surname = $_POST["surname"];
$salary = $_POST["salary"];
#print $name;

$sql_add = "insert into table1(name, surname, salary_id) values('$name', '$surname', '$salary')";

#print $sql_add;
$res_add = mysql_db_query($database,$sql_add);
if($res_add){
print "OK<br>";
print "<a href=\"show.php\">BACK</a>";
}else{
print "FAIL !!!<br>";
print "<a href=\"show.php\">BACK</a>";
}
?>

update

<?php
include("connect.php");
$id = $_GET["id"];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
$sql_show = "SELECT * FROM table1 WHERE id = '$id'";
$res_show = mysql_db_query($database,$sql_show);
$val_show = mysql_fetch_array($res_show)

?>
<form action="fn_update.php" method="post">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="32%">id </td>
<td width="68%"><input type="text" name="id" value="<?=$val_show["id"] ?>" readonly="readonly"/></td>
</tr>
<tr>
<td width="32%">ชื่อ </td>
<td width="68%"><input type="text" name="name" value="<?=$val_show["name"] ?>" /></td>
</tr>
<tr>
<td>นามสกุล</td>
<td><input type="text" name="surname" value="<?=$val_show["surname"] ?>" /></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>
<input type="submit" value="SAVE" />
<input type="reset" value="CLEAR" />
</td>
</tr>
</table>
</form>


</body>
</html>


fn_update
<?php
include("connect.php");

$id = $_POST["id"];
$name = $_POST["name"];
$surname = $_POST["surname"];
#print $name;

$sql_add = "update table1 set name='$name', surname='$surname' where id='$id'";

#print $sql_add;

$res_add = mysql_db_query($database,$sql_add);
if($res_add){
print "OK<br>";
print "<a href=\"show.php\">BACK</a>";
}else{
print "FAIL !!!<br>";
print "<a href=\"show.php\">BACK</a>";
}

?>

ข้างบน