Sie sind auf Seite 1von 12

Bai 1 Demo.

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>Học PHP</title>
</head>
<body>
<form name="frm" action="demo1.php" method="post">
<input type="text" name="demo" />
<input type="submit" name="submit" value="Submit" />
<label>
Nam
<input name="gioitinh" type="radio" value="1" />
Nu
<input name="gioitinh" type="radio" value="0" />
</label>
</form>
<a href="demo1.php?name=Vũ Hồng Chinh 1&them=Them 1 cai moi">Link</a>
</body>
</html>
Bai2 Cach gui giu lieu theo Post va Get
<!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>Hiển thị dữ liệu</title>
</head>
<body>
<?php
if(isset($HTTP_POST_VARS['demo'])) {
$demo = $HTTP_POST_VARS['demo'];// $_POST['demo'];
if($demo != '') {
echo "Gửi theo kiểu POST: ".$demo;
} else {
echo "Chua co du lieu";
}
$gioitinh = $HTTP_POST_VARS['gioitinh'];// $_POST['demo'];
if($gioitinh == '0') {
$temp_gt = "Nu";
//echo "Nu";
} else if($gioitinh == '1') {
$temp_gt = "Nam";
//echo "Nam";
} else {
//echo "Khong chon";
$temp_gt = "Khong chon";
}

} else if($HTTP_GET_VARS['name']) {
$demo = $HTTP_GET_VARS['name'];// $_POST['demo'];
echo "Gửi theo kiểu GET: ".$demo;
$them = $HTTP_GET_VARS['them'];// $_POST['demo'];
echo "<br>".$them;
}
?>
<table width="100%" border="1" cellspacing="0" cellpadding="0">
<tr>
<td width="10%">&nbsp;</td>
<td width="90%">&nbsp;</td>
</tr>
<tr>
<td>Gioi tinh </td>
<td>
<?=$temp_gt?>
</td>
</tr>
</table>
</body>
</html>
Bai 3 Insert du lieu
<!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>Insert Database</title>
</head>

<body>
<?php
$link = mysql_connect("localhost", "root", "vertrigo");
mysql_select_db("hoang", $link);

$sql = "select * from lop";


$ketqua = mysql_query($sql, $link);

if(isset($HTTP_POST_VARS['Submit'])) {
$masv = $HTTP_POST_VARS['masv'];
$hoten = $HTTP_POST_VARS['hoten'];
$gioitinh = $HTTP_POST_VARS['gioitinh'];
$quequan = $HTTP_POST_VARS['quequan'];
$lop_id = $HTTP_POST_VARS['lop_id'];

$sql = "insert into sinhvien(masv, hoten, gioitinh, quequan, lop_id) value


('$masv', '$hoten', '$gioitinh', '$quequan', $lop_id)";
mysql_query($sql, $link);
echo "Thanh cong";
}

?>
<form name="frm" method="post" action="insert.php">
<table width="100%" border="1" cellspacing="0" cellpadding="0">
<tr>
<td width="12%">Lop </td>
<td width="88%"><label>
<select name="lop_id">
<?php
while($row = mysql_fetch_array($ketqua)) {
?>
<option value="<?=$row['lop_id'] ?>"><?=$row['lop_name'] ?></option>
<?php
}
?>
</select>
</label></td>
</tr>
<tr>
<td width="12%">Ma SV </td>
<td width="88%"><label>
<input name="masv" type="text" id="masv" />
</label></td>
</tr>
<tr>
<td>Ho va ten </td>
<td><label>
<input name="hoten" type="text" id="hoten" />
</label></td>
</tr>
<tr>
<td>Gioi tinh </td>
<td>Nam
<label>
<input name="gioitinh" type="radio" value="1" />
Nu
<input name="gioitinh" type="radio" value="0" />
</label></td>
</tr>
<tr>
<td>Que quan </td>
<td><label>
<input name="quequan" type="text" id="quequan" />
</label></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><label>
<input type="submit" name="Submit" value="Submit" />
</label></td>
</tr>
</table>
</form>
</body>
</html>
Bai 4 Nhan Du Lieu
<!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>Nhận Dữ Liệu</title>
</head>
<body>
<?php
$masinhvien=$HTTP_POST_VARS['masinhvien'];
$hotensinhvien=$HTTP_POST_VARS['hotensinhvien'];
$ngaysinh=$HTTP_POST_VARS['ngaysinh'];
$gioitinh=$HTTP_POST_VARS['gioitinh'];
$quequan=$HTTP_POST_VARS['quequan'];
?>
<table width="100%" border="1" cellspacing="0" cellpadding="0">
<tr>
<td width="18%">Mã sinh viên</td>
<td width="82%">
<?php
if ($masinhvien=='') {
echo "Chưa nhập mã sinh viên";
} else {
echo $masinhvien;
}
?>
</td>
</tr>
<tr>
<td>Họ tên sinh viên</td>
<td>
<?php
if($hotensinhvien==''){
echo "Chưa nhập nhọ tên sinh viên";
} else{
echo $hotensinhvien;
}

?>
</td>
</tr>
<tr>
<td>Ngày Sinh</td>
<td>
<?php
if($ngaysinh==''){
echo "Chưa nhập ngày sinh";
} else{
echo $ngaysinh;
}

?>
</td>
</tr>
<tr>
<td>Giới Tính </td>
<td>
<?php
if ($gioitinh=='0'){
echo "Nữ";
} else if($gioitinh=='1'){
echo "Nam";
} else {
echo "Chưa chọn giới tính";
}
?>
</td>
</tr>
<tr>
<td>Quê Quán </td>
<td><?php
if($quequan==''){
echo "Chưa nhập quê quán";
} else{
echo $quequan;
}
?>
</td>
</tr>
</table>

</body>
</html>
Bai 5 Nhap Du Lieu
<!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>Nhập Dữ Liệu</title>
</head>

<body>
<form name="frm" method="post" action="Nhandulieu.php" onsubmit="return
check();">
<table width="100%" border="1" cellspacing="0" cellpadding="0">
<tr>
<td width="13%">Mã sinh viên</td>
<td width="87%"><input type="text" name="masinhvien" /></td>
</tr>
<tr>
<td>Họ tên sinh viên</td>
<td><input type="text" name="hotensinhvien" /></td>
</tr>
<tr>
<td>Ngày Sinh</td>
<td><input type="text" name="ngaysinh" /></td>
</tr>
<tr>
<td>Giới Tính </td>
<td>
<label>
Nam
<input name="gioitinh" type="radio" value="1" />
Nu
<input name="gioitinh" type="radio" value="0" />
</label>
</td>
</tr>
<tr>
<td>Quê Quán </td>
<td><input type="text" name="quequan" /></td>
</tr>
</table>
<input type="submit" name="submit" value="submit"/>
</form>
<script language="javascript">
function check() {
if(document.frm.masinhvien.value == "") {
alert("Ban hay nhap ma sinh vien");
document.frm.masinhvien.focus();
return false;
}
if (document.frm.hotensinhvien.value==""){
alert("Ban hay nhap ho ten");
document.frm.hotensinhvien.focus();
return false;
}
if (document.frm.ngaysinh.value==""){
alert("Ban hay nhap ngay sinh");
document.frm.ngaysinh.focus();
return false;
}

var result = "";


for(var i = 0; i < document.frm.gioitinh.length; i++) {
if(document.frm.gioitinh[i].checked == true) {
result = document.frm.gioitinh[i].value;
break;
}
}
if (result == "") {
alert("Ban hay chon gioi tinh");
return false;
}

if (document.frm.quequan.value == "") {
alert("Ban chua nhap que quan");
document.frm.quequan.focus();
return false;
}

return true;
}
</script>
</body>
</html>
Bai 6 Select Du Lieu
<!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>Select Database</title>
</head>

<body>
<?php
$link = mysql_connect("localhost", "root", "vertrigo");
mysql_select_db("hoang", $link);

$sql = "select * from sinhvien,lop where sinhvien.lop_id = lop.lop_id order by ";


$sql.= "lop.lop_name asc,sinhvien.hoten asc";
$ketqua = mysql_query($sql, $link);
?>
<table width="100%" border="1" cellspacing="0" cellpadding="0">
<tr>
<td align="center"><strong>TT</strong></td>
<td><strong> Ma Sinh vien </strong></td>
<td><strong>Ho ten </strong></td>
<td><strong>Gioi tinh </strong></td>
<td><strong>Que quan </strong></td>
<td><strong>Lop </strong></td>
<td align="center"><strong>Update</strong></td>

</tr>
<?php
$i = 0;
while($row = mysql_fetch_array($ketqua)) {
$i++;
?>
<tr>
<td align="center">
<b><?=$i ?></b> </td>
<td>
<a href="update.php?id=<?=$row['id'] ?>">
<?=$row['masv'] ?> </td>
<td>
<a href="update.php?id=<?=$row['id'] ?>">
<?=$row['hoten'] ?>
</a> </td>
<td>
<?php
if($row['gioitinh'] == 1) {
echo "Nam";
} else {
echo "Nu";
}
?> </td>
<td><?=$row['quequan'] ?></td>

<td align="center">
<?=$row['lop_name'] ?>
</td>

<td align="center">
<a href="update.php?id=<?=$row['id'] ?>">
Update
</a>
</td>

</tr>
<?php
}
?>
</table>
<br />
<a href="insert.php">
Insert
</a>
</body>
</html>
Bai 7 Update Du Lieu
<?php
$link = mysql_connect("localhost", "root", "root");
mysql_select_db("hoang", $link);

if(isset($_POST['Submit'])) {
$sql = "update sinhvien set ";
$sql.= "masv = '".$_POST['masv']."',";
$sql.= "hoten= '".$_POST['hoten']."',";
$sql.= "gioitinh = ".$_POST['gioitinh'].",";
$sql.= "lop_id = ".$_POST['lop_id'].",";
$sql.= "quequan = '".$_POST['quequan']."' where id =".$_GET['id']."";
mysql_query($sql, $link);

$sql_lop = "select * from lop";


$ketqua_lop = mysql_query($sql_lop, $link);

$sql = "select * from sinhvien where id = ".$_GET['id']."";


$ketqua = mysql_query($sql, $link);
$row = mysql_fetch_array($ketqua);
$nam = "";
$nu = "";
if($row['gioitinh'] == 1) {
$nam = "checked";
}
if($row['gioitinh'] == 0) {
$nu = "checked";
}

echo "Day la Id cu lop trong bang ma sinh vien: ".$row['lop_id']."<br>";

?>

<!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>Update</title>
</head>

<body>
<form name="frm" method="post" action="update.php?id=<?=$_GET['id'] ?>">
<table width="100%" border="1" cellspacing="0" cellpadding="0">
<tr>
<td width="12%">Lop </td>
<td width="88%"><label>
<select name="lop_id">
<?php
while($row_lop = mysql_fetch_array($ketqua_lop)) {
if($row['lop_id'] == $row_lop['lop_id']) {
$selected = "selected";
} else {
$selected = "";
}

?>
<option <?=$selected ?> value="<?=$row_lop['lop_id'] ?>">
<?=$row_lop['lop_name'] ?> -- <?=$row_lop['lop_id'] ?>
</option>
<?php
}
?>
</select>
</label></td>
</tr>
<tr>
<td width="12%">Ma SV </td>
<td width="88%"><label>
<input name="masv" type="text" id="masv" value="<?=$row['masv'] ?>" />
</label></td>
</tr>
<tr>
<td>Ho va ten </td>
<td><label>
<input name="hoten" type="text" id="hoten" value="<?=$row['hoten'] ?>" />
</label></td>
</tr>
<tr>
<td>Gioi tinh </td>
<td>Nam
<label>
<input name="gioitinh" type="radio" value="1" <?=$nam ?>/>
Nu
<input name="gioitinh" type="radio" value="0" <?=$nu ?> />
</label></td>
</tr>
<tr>
<td>Que quan </td>
<td><label>
<input name="quequan" type="text" id="quequan" value="<?=$row['quequan'] ?
>" />
</label></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><label>
<input type="submit" name="Submit" value="Submit" />
</label></td>
</tr>
</table>
</form>
</body>
</html>

Das könnte Ihnen auch gefallen