BT Giai SQL Web

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 41

Thng tin khch hng ( Sa, Xa)

/-----------Connect----------/
<?php
$db = mysql_connect("localhost","root","");
if(!$db)
{
echo "Loi SQL";
exit();
}
$db = mysql_select_db("ql_ban_hang");
if(!$db)
{
echo "Loi DB";
exit();
}
mysql_query("set names utf8");
?>
/------------ Main-----------/
<html>
<body>
<script language="javascript">
function xac_nhan()
{
var xn = confirm("Bn c mun xa hay khng?");
if(xn == true)
return true;
else
return false;
}
</script>
<?php
include(connect.php);
$sql = "SELECT * FROM khach_hang";
$r1 = mysql_query($sql);
$n = mysql_fetch_assoc($r1);
$str= ' ';
Page 1
Jacky + Cupid+Kit

$str .='<tr>
<td>'.$n["Ma_khach_hang"].'</td>
<td>'.$n["Ten_khach_hang"].'</td>
.
<td><a href="test_sua.php?ma='.$n["Ma_khach_hang"].'">Sua</a></td>
<td><a onclick="return xac_nhan();"
href="test_xoa.php?ma='.$n["Ma_khach_hang"].'">Xoa</a></td>
</tr>';
}
?>
<table align="center" border="1" cellpadding="0" cellspacing="0" width="800">
<tr>
<td>Ma khach hang</td>
<td>Ten khach hang</td>
.
<td>Sua</td>
<td>Xoa</td>
</tr>
<?php
echo $str;
mysql_close();
?>
</table>
</body>
</html>
/------------- Sa ----------/

<body>
<?php
include(connect.php);
if(isset($_POST["bt_Sua"]))
Page 2
Jacky + Cupid+Kit

{
$ma_kh = $_POST["txt_ma"];
$ten_kh= $_POST["txt_ten"];
$phai = $_POST["rb_phai"];
$dia_chi= $_POST["txt_dc"];
$dien_thoai= $_POST["txt_dt"];
$email = $_POST["txt_email"];
$sql1= "UPDATE khach_hang
SET Ten_khach_hang ='{$ten_kh}', Phai = '{$phai}', Dia_chi ='{$dia_chi}',
Dien_thoai='{$dien_thoai}', Email='{$email}'
WHERE Ma_khach_hang = '{$ma_kh}' ";
$query1 = mysql_query($sql1);
echo '<meta http-equiv="refresh" content="1;url=test_2_12_thongtinkh.php"/>';
}
elseif(isset($_GET["ma"]))
{
$ma = $_GET["ma"];
$sql ="SELECT * FROM khach_hang WHERE Ma_khach_hang='$ma'";
$query = mysql_query($sql);
$n = mysql_fetch_assoc($query);
$ma_kh = $n["Ma_khach_hang"];
$ten_kh= $n["Ten_khach_hang"];
$phai = $n["Phai"];
$dia_chi= $n["Dia_chi"];
$dien_thoai= $n["Dien_thoai"];
$email = $n["Email"];
}
?>
<?php if(isset($_GET["ma"]) || isset($_POST["bt_Sua"])): ?>
<form id="form1" name="form1" action="" method="post">
<table align="center" border="1">
<tr>
<td align="center" colspan="2">Sua chi tiet</td>
</tr>
<tr>
<td>Ma khach hang</td>
<td><input type="text" id="txt_ma" name="txt_ma" readonly="readonly" value="<?php echo $ma_kh
?>"/></td>
</tr>
<tr>
<td>Ten khach hang</td>
<td><input name="txt_ten" type="text" id="txt_ten" value="<?php echo $ten_kh?>" /></td>
</tr>
<tr>
Page 3
Jacky + Cupid+Kit

<td>Phai</td>
<td><input type="radio" name="rb_phai" id="rb_phai_0" value="0" <?php if($phai==0) echo "checked"
?> />Nam<input type="radio" name="rb_phai" id="rb_phai_1" value="1" <?php if($phai==1) echo
"checked"; ?>/>Nu</td>
</tr>

<tr>
<td colspan="2" align="center">
<input type="submit" id="bt_Sua" name="bt_Sua" value="Sua"/>
</td>
</tr>
</table>
</form>
<?php endif; ?>
</body>
</html>
------------------Xa
<?php
Include(connect.php);
if(isset($_GET['ma']))
{
$ma_kh = $_GET['ma'];
$sql = "DELETE FROM khach_hang WHERE ma_khach_hang ='{$ma_kh}'";
$query1 = mysql_query($sql);
echo '<meta http-equiv="refresh" content="0;url=test_2_12_thongtinkh.php"/>';
}
?>

Page 4
Jacky + Cupid+Kit

-------------------

Tm kim theo combo list

Connect.php
<?php
$conn=mysql_connect('localhost','root','');
if(!$conn){
echo "loi";
exit;}
mysql_select_db('bookstore');
mysql_query("set names 'utf8'");
?>
/////////////////////////////////////////////////////
tk.php
<?php
include "connect.php";
$m=isset($_REQUEST['nhaxb'])?$_REQUEST['nhaxb']:"tatca";
$tu=isset($_REQUEST['tu'])?$_REQUEST['tu']:"";
$den=isset($_REQUEST['den'])?$_REQUEST['den']:"";?>
<body>
<?php
$query="select * from nhaxb";
$result=mysql_query($query,$conn);
?>
<form action="tk.php" method="get">
Nha xb <select name="nhaxb">
<option value="tatca">----</option>
<?php
while($row=mysql_fetch_array($result)){
$ma=$row[0];
$t=$row[1];
if ($ma==$m) $s=" selected "; else $s="";
Page 5
Jacky + Cupid+Kit

?>
<option value="<?php echo $ma;?>" <?php echo $s;?>><?php echo $t; ?></option>
<?php
}
?></select>
Gia tu: <input type="text" name="tu" value="<?php echo $tu;?>" />
Den: <input type="text" name="den" value="<?php echo $den;?>"/>
<input type="submit" name="tim" value="Tim" />
</form>
<table border="1">
<tr>
<td>
Ma sach
</td>
<td>
ten sach
</td>
<td>
Mo ta
</td>
</tr>
<?php
if(isset($_REQUEST['tim']))
{
$query="select * from sach where manxb = '$m'";
if($tu!='')
$query=$query." and gia >= ".$tu;
if($den!='')
$query=$query." and gia <= ".$den;
$result=mysql_query($query,$conn);
//$count=mysql_num_rows($result);
//$pagecount=ceil($);
while($row=mysql_fetch_array($result))
{
$masach=$row[0];
$tensach=$row[1];
$mota=$row[2];
echo "<tr><td>$masach</td><td>$tensach</td><td> $mota </td></tr>";
}
echo $m;
}
?>
</table>
</body>
</html>
Page 6
Jacky + Cupid+Kit

-----------------------------------------------

Tm kim theo phn loai gi

<?php
Include connect.php;
$sql="SELECT * FROM sach ";
$result=mysql_query($sql);
?>
<body>
<form action="" method="post" name="form_timkiem">
Ten sach : <input type="text" name="ten"><br />
Gia tu <input type="text" name="gia_tu">
Gia den <input type="text" name="gia_den"><br />
<input type="submit" name="submit" value="Tim kiem">
</form>
<table border="1">
<?php
if(isset($_REQUEST['submit'])){
$ten=$_REQUEST['ten'];
$sql .="WHERE tensach like '%$ten%' ";
Page 7
Jacky + Cupid+Kit

if($_REQUEST['gia_tu']!="")
$sql.=" and gia >=".$_REQUEST['gia_tu'];
if($_REQUEST['gia_tu']!="")
$sql.=" and gia <=".$_REQUEST['gia_den'];

$res = mysql_query($sql);
while($row=mysql_fetch_array($res)){
?>
<tr>
<td><?php echo $row['tensach'];?></td>
<td><?php echo "<a href='chitiet.php?masach=".$row['masach']."'/><img
src='image_data/".$row['hinh']."'></a>"?></td>
<td><?php echo $row['gia'];?></td>
</tr>
<?php
}
echo $sql;
}
?>
</table>
</body>
</html>
-----------------------

Page 8
Jacky + Cupid+Kit

Chitiet.php

<body>
<?php
Include connect.php;
$ma=$_REQUEST["masach"];
$sql="SELECT tennxb,tenloai, sach . *
FROM sach
INNER JOIN nhaxb ON sach.manxb = nhaxb.manxb
INNER JOIN loai ON sach.maloai=loai.maloai
WHERE sach.masach = '$ma'";
$query = mysql_query($sql);
$tb = mysql_fetch_assoc($query);
$str ='<tr>
<td>
'.$tb["masach"].'
</td>
<td>
'.$tb["tensach"].'
</td>
<td><img src="image_data/'.$tb["hinh"].'" /></td>
<td>'.$tb["tennxb"].'</td>
<td>'.$tb["tenloai"].'</td>
<td>'.$tb["gia"].'</td>
</tr>';
?>
<table border="1">
<tr><td>M sch</td>
<td>Tn sch</td>
<td>hnh</td>
<td>tn nh xut bn</td>
<td>tn loi sch</td>
<td>Gi</td>
</tr>
<?php
Page 9
Jacky + Cupid+Kit

echo $str;
?>
</table>
</body>
</html>
------------------

Page 10
Jacky + Cupid+Kit

Tm kim thong tin (c phn trang) Lm theo hng i tng

//////////////////////////
db.inc
<?php
$hostname='localhost';
$dbname='ql_ban_hang';
$dbuser='root';
$dbpass='';
?>
//////////////////////////
database.php
<?php
class database
{
var $sql="";
var $connection="";
var $cursor=null;
function database()
{
include('db.inc');
$this->connection =@mysql_connect($hostname,$dbuser,$dbpass);
if(!$this->connection)
die("Trang web ang bo tr, vui lng quay li sau(1.1)");
Page 11
Jacky + Cupid+Kit

if($dbname=="" || !mysql_select_db($dbname,$this->connection))
die("Trang web ang bo tr, vui lng quay li sau(1.2)");
}
function SetQuery($sql) //Gn cau lenh sql cho thuoc tinh $sql
{
$this->sql=$sql;
}
function Query()
//Gn dl cho thuoc tinh $cursor
{
mysql_query("set names utf8");
$this->cursor = mysql_query($this->sql,$this->connection);
return $this->cursor;
}
function LoadAllRow()
{
if(!($cur=$this->query()))
{
return null;
}
$array=array();
while($row=mysql_fetch_assoc($cur))
{
$array[]=$row;
}
mysql_free_result($cur);
return $array;
}
function LoadOneRow()
{
if(!($cur=$this->query()))
{
return null;
}
$row=mysql_fetch_assoc($cur);
mysql_free_result($cur);
return $row;
}
function disconnect()
{
mysql_close($this->connection);
}
}
?>
Page 12
Jacky + Cupid+Kit

//////////////////////////
Main.php
<?php
include("includes/database.php");
if(!empty($_GET['txt_ten']))
{
$ten_sua = $_GET['txt_ten'];
$db = new database();
$rows = 2; // moi trang se co 2 san pham
//---- Phan trang ----------$sql1 = "SELECT count(*) as tong
FROM sua s,hang_sua h
WHERE s.ma_hang_sua = h.ma_hang_sua AND ten_sua like'%".$ten_sua."%'";
$db->SetQuery($sql1);
$row = $db->LoadOneRow();
//print_r($row);
$tong = $row['tong'];
$pages = ceil($tong/$rows);
$pageslist ='';
for($i = 1 ; $i <= $pages ; $i++)
{
$pagelist .='<a
href="bai2_9_pages.php?page='.$i.'&txt_ten='.$ten_sua.'">'.$i.'</a>&nbsp;';
}
//============= xu ly tham so start =========
if(!isset($_GET['page']))
$start = 0;
else
$start = ($_GET['page'] - 1) * $rows;
//-----------------------------if($tong > 0 )
{
$thong_bao ="C $tong sn phm c tm thy";
}
else
$thong_bao ="Khng tm thy sn phm no";
//------------------------------------------------$sql2 = "SELECT s.*,ten_hang_sua
FROM sua s,hang_sua h
WHERE s.ma_hang_sua = h.ma_hang_sua AND ten_sua
like'%".$ten_sua."%'
LIMIT $start,$rows";
Page 13
Jacky + Cupid+Kit

$db->SetQuery($sql2);
$table = $db->LoadAllRow();
//print_r($table);
$ket_qua='';
foreach($table as $row)
{
$ket_qua .='<table width="100%" border="1" align="center" style="border-collapse:collapse">
<tr>
<td colspan="2" style="text-align:center;background:#FFCCCC"><font size="+1"
color="#FF0000">'.$row['Ten_sua'].' - '.$row['ten_hang_sua'].'</font></td>
</tr>
<tr>
<td valign="top" width="150px"><img src="hinh_sua/'.$row['Hinh'].'" width="150px"/></td>
<td><strong>Thnh phn dinh dng:</strong><br />
'.$row['TP_Dinh_Duong'].' <br />
<strong>Li ch:</strong><br />
'.$row['Loi_ich'].' <br />
<strong>Trng lng:</strong><font color="#FF0000">'.$row['Trong_luong'].' gr</font> - <strong>n
gi:</strong><font color="#FF0000">'. number_format($row['Don_gia']) .' VN</font> </td>
</tr>
</table>';
}
}
?>
<form id="form1" name="form1" method="get" action="">
<table width="700" border="1" align="center" >
<tr>
<td ><div align="center" class="style1">TM KIM THNG TIN SA</div></td>
</tr>
<tr>
<td>Tn sa:
<label>
<input type="text" name="txt_ten" id="txt_ten" value="<?php echo $ten_sua ?>" />
<input type="submit" name="bt_tim" id="bt_tim" value="Tm kim" />
</label></td>
</tr>
<?php if(!empty($_GET['txt_ten'])): ?>
<tr bgcolor="#FFFFCC">
<td ><div align="center"><?php echo $thong_bao ?></div>
<table width="90%" align="center" border="2" style="border-color:#FF0000">
<tr>
<td><?php echo $ket_qua ?></td>
</tr>
Page 14
Jacky + Cupid+Kit

</table>
</td>
</tr>
<tr>
<td style="text-align:center"><?php echo $pagelist; ?></td>
</tr>
<?php endif; ?>
</table>
</form>
</body>
</html>

Page 15
Jacky + Cupid+Kit

---------------------------

Thng tin hin th dng List (hng i tng include database.php pha trn vo)

<body>
<?php
include("includes/database.php");
$db = new database();
$sql ="Select ma_sua, ten_sua,trong_luong,don_gia,hinh from sua";
$db->SetQuery($sql);
$table = $db->LoadAllRow();
$n = count($table);
$dem =0;
$cot = 5;
$str ='';
for($i = 0 ;$i < $n ; $i++)
{
$row = $table[$i];
if($dem % $cot == 0 )
{
$str .='<tr>';
}
$str .='<td style="text-align:center"><strong><a
href="bai2_7_detail.php?ma_sua='.$row['ma_sua'].'">'.$row['ten_sua'].'</a></strong><br />
'.$row['trong_luong'].' gr - '. number_format($row['don_gia']) .' VN <br
/>

Page 16
Jacky + Cupid+Kit

<img src="hinh_sua/'.$row['hinh'].'" width="150px" height="170px"


border="0"/>
</td>';
if($dem % $cot == ($cot - 1))
{
$str .='</tr>';
}
$dem++;
}
?>
<table width="1000px" height="141" border="0" align="center">
<tr>
<td height="69" colspan="5"><div align="center" class="style1">THNG TIN CC SN
PHM</div></td>
</tr>
<?php echo $str ?>
</table>
</body>
</html>

Page 17
Jacky + Cupid+Kit

Chi tit sn phm (khi click vo hnh)

<body>
<?php
if(!isset($_GET['ma_sua']))
{
exit();
}
else
{
include("includes/database.php");
$ma_sua = $_GET['ma_sua'];
$db = new database();
$sql ="select ten_sua,ten_hang_sua,tp_dinh_duong,loi_ich,don_gia,hinh,trong_luong
from sua s, hang_sua h
where s.ma_hang_sua = h.ma_hang_sua AND ma_sua='{$ma_sua}'";
$db->SetQuery($sql);
$row = $db->LoadOneRow();
//print_r($row);
}
?>
<table width="609" height="303" border="1" align="center">
<tr bgcolor="#FFFFCC">
<td height="61" colspan="2"><div align="center" class="style1"><?php echo $row['ten_sua'].' '.$row['ten_hang_sua'] ?></div></td>
</tr>
<tr>
Page 18
Jacky + Cupid+Kit

<td width="200" height="209" valign="top"><img src="hinh_sua/<?php echo $row['hinh'] ?>"


width="200px" height="220px" border="0"/></td>
<td width="393" valign="top" style="padding-left:10px; padding-right:10px">
<strong>Thnh phn dinh dng:</strong><br />
<?php echo $row['tp_dinh_duong'] ?> <br />
<strong>Li ch:</strong><br />
<?php echo $row['loi_ich'] ?>
<div align="right"><strong>Trng lng:</strong><?php echo $row['trong_luong'] ?> gr <strong>n gi:</strong> <?php echo number_format($row['don_gia']) ?> VN</div>
</td>
</tr>
<tr>
<td align="right" style="padding-right:5px"><a href="bai2_7_list.php">Quay v</a></td>
<td>&nbsp;</td>
</tr>
</table>
</body>
--------------------------------

Page 19
Jacky + Cupid+Kit

Thm chi tit

<body>
<?php
include("includes/database.php");
// tao menu hang_sua
$db = new database();
$sql = "select ma_hang_sua,ten_hang_sua from hang_sua";
$db->SetQuery($sql);
$tb_hs = $db->LoadAllRow();
//print_r($tb_hs);
$mn_hang = '';
foreach($tb_hs as $row)
{
if(isset($_POST['mn_hang_sua']) && $_POST['mn_hang_sua'] ==
$row['ma_hang_sua'])
$mn_hang .=' <option value="'.$row['ma_hang_sua'].'"
selected>'.$row['ten_hang_sua'].'</option>';
else
$mn_hang .=' <option
value="'.$row['ma_hang_sua'].'">'.$row['ten_hang_sua'].'</option>';
}
// tao menu loai sua
Page 20
Jacky + Cupid+Kit

$sql = "select ma_loai_sua,ten_loai from loai_sua";


$db->SetQuery($sql);
$tb_ls = $db->LoadAllRow();
//print_r($tb_hs);
$mn_loai = '';
foreach($tb_ls as $row)
{
if(isset($_POST['mn_loai_sua']) && $_POST['mn_loai_sua'] == $row['ma_loai_sua'])
$mn_loai .=' <option value="'.$row['ma_loai_sua'].'"
selected>'.$row['ten_loai'].'</option>';
else
$mn_loai .=' <option
value="'.$row['ma_loai_sua'].'">'.$row['ten_loai'].'</option>';
}
//---------------- XU LY ----------if(!empty($_POST['txt_ma_sua']) && !empty($_POST['txt_ten_sua']) &&
!empty($_POST['txt_trong_luong']) && !empty($_POST['txt_tp_dinh_duong']) &&
!empty($_POST['txt_don_gia']) && !empty($_POST['txt_loi_ich']) && $_FILES['f_hinh']['name'] != "")
{
$ma_sua = $_POST['txt_ma_sua'];
$ten_sua = $_POST['txt_ten_sua'];
$ma_hang_sua = $_POST['mn_hang_sua'];
$ma_loai_sua = $_POST['mn_loai_sua'];
$trong_luong = $_POST['txt_trong_luong'];
$don_gia = $_POST['txt_don_gia'];
$tp_dinh_duong = $_POST['txt_tp_dinh_duong'];
$loi_ich = $_POST['txt_loi_ich'];
// lay du lieu doi tuong f_hinh
$f_hinh = $_FILES['f_hinh'];
$ten_hinh = $f_hinh['name'] ;
$type = $f_hinh['type'];
$size = $f_hinh['size'];
$tmp = $f_hinh['tmp_name'];
if(($type =='image/jpeg' || $type =='image/bmp' || $type =='image/gif') && $size < 8000)
{
move_uploaded_file($tmp,"hinh_sua/".$ten_hinh);
$sql = "INSERT INTO sua
VALUES('{$ma_sua}','{$ten_sua}','{$ma_hang_sua}','{$ma_loai_sua}',{$trong_luong},{$don_gia},'{$t
p_dinh_duong}','{$loi_ich}','{$ten_hinh}')";
$db = new database();
Page 21
Jacky + Cupid+Kit

$db->SetQuery($sql);
$db->Query();
}
}
?>
<form action="" method="post" enctype="multipart/form-data">
<table width="536" border="2" align="center">
<tr>
<td colspan="2"><div align="center"><strong>THM SA MI</strong></div></td>
</tr>
<tr>
<td width="150">M sa:</td>
<td width="368"><label>
<input type="text" name="txt_ma_sua" id="txt_ma_sua" />
</label></td>
</tr>
<tr>
<td>Tn sa:</td>
<td><label>
<input type="text" name="txt_ten_sua" id="txt_ten_sua" />
</label></td>
</tr>
<tr>
<td>Hng sa;</td>
<td><label>
<select name="mn_hang_sua" id="mn_hang_sua">
<?php echo $mn_hang ?>
</select>
</label></td>
</tr>
<tr>
<td>Loi sa:</td>
<td><select name="mn_loai_sua" id="mn_loai_sua">
<?php echo $mn_loai ?>

</select>
</td>
</tr>
<tr>
<td>Trng lng:</td>
<td><label>
<input type="text" name="txt_trong_luong" id="txt_trong_luong" />
(gr hoc ml) </label></td>
Page 22
Jacky + Cupid+Kit

</tr>
<tr>
<td>n gi:</td>
<td><label>
<input type="text" name="txt_don_gia" id="txt_don_gia" />
(VN)</label></td>
</tr>
<tr>
<td>Thnh phn dinh dng:</td>
<td><label>
<textarea name="txt_tp_dinh_duong" cols="40" id="txt_tp_dinh_duong"></textarea>
</label></td>
</tr>
<tr>
<td>Li ch:</td>
<td><label>
<textarea name="txt_loi_ich" cols="40" id="txt_loi_ich"></textarea>
</label></td>
</tr>
<tr>
<td>Hnh nh</td>
<td><label>
<input name="f_hinh" type="file" id="f_hinh" size="40" />
</label></td>
</tr>
<tr>
<td colspan="2"><div align="center">
<input type="submit" name="Btn_them_moi" id="button" value="Thm mi" />
</div></td>
</tr>
<tr>
<td colspan="2">&nbsp;</td>
</tr>
</table>
</form>
</body>

--------------------------

Page 23
Jacky + Cupid+Kit

Tm kim cp nht gi theo %

<body>
<?php

$con = mysql_connect("localhost","root","") or die("loi ket noi");


$db = mysql_select_db("bookstore") or die("loi database");
mysql_query("set names 'utf8'");

?>
<form action="" method="post">
Chn Nh Xut Bn :
<select name="nxb">
<?php
$sql_nxb = "SELECT * FROM nhaxb";
$result = mysql_query($sql_nxb);
$giatri = $_POST['giatri'];
while($row=mysql_fetch_array($result)){
$manxb = $row[0];
$tennxb = $row[1];
echo "<option value='".$manxb."'>".$tennxb."</option>";
}
?>
Page 24
Jacky + Cupid+Kit

</select>
Gi tr thay i % <input type="text" name="giatri" value="<?php echo $giatri; ?>"/> <br />
<input type="radio" checked="check" value="tang" name="dieu_chinh" /> Tng
<input type="radio" value="giam" name="dieu_chinh" /> Gim <br />
<input type="submit" name="submit" value="Thc hin" />
</form>
<hr />
<?php
if(isset($_POST['submit'])){
$nxb = $_POST['nxb'];
$giatri = $_POST['giatri'];
$dieuchinh = $_POST['dieu_chinh'];
echo "<hr /><p align=left>Kt qu khi gim gi $giatri %</p>";
if($dieuchinh=='tang')
$sql = "SELECT s. * ,ceiling(s.gia + $giatri/100 * s.gia) AS giamoi FROM sach s WHERE
manxb like '$nxb'";
else $sql = "SELECT s. * ,ceiling(s.gia - $giatri/100 * s.gia) AS giamoi FROM sach s WHERE
manxb like '$nxb'";
$result = mysql_query($sql);
echo "<table border=1 align=left>";
echo "<td><b>Tn sch</b></td><td><b>Gi mi</b></td>";
while($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td>".$row['tensach']."</td>";
echo "<td>".$row['giamoi']."</td>";
echo "</tr>";
}
echo "</table>";
}
?>
</body>
</html>

Page 25
Jacky + Cupid+Kit

-------------------

ng nhp (hng i tng)

Database.php
<?php
class database {
var $_sql = '';
var $_connection = '';
var $_cursor = null;
function database()
{
$this->_connection = @mysql_connect('localhost', 'root', '');
if (!$this->_connection )
{
die("Khong the ket noi MySQL");
}
$db='ql_sv';
if ($db != '' && !mysql_select_db( $db, $this->_connection ))
{
die ("Khong the mo CSDL $db: ".mysql_error());
}
}
function setQuery( $sql)
{
$this->_sql = $sql;
}
function query()
{
$this->_cursor = mysql_query( $this->_sql, $this->_connection );
return $this->_cursor;
}
function loadRow() {
if (!($cur = $this->query())) {
Page 26
Jacky + Cupid+Kit

return null;
}
$ret = null;
if ($row = mysql_fetch_row( $cur )) {
$ret = $row[0];
}
mysql_free_result( $cur );
return $ret;
}
function loadArrayField($numinarray = 0) {
if (!($cur = $this->query())) {
return null;
}
$array = array();
while ($row = mysql_fetch_row( $cur )) {
$array[] = $row[$numinarray];
}
mysql_free_result( $cur );
return $array;
}
function loadAllRow() {
if (!($cur = $this->query())) {
return null;
}
$array = array();
while ($row = mysql_fetch_assoc( $cur ))
{
// ketqua = mang ket qua
$array[] = $row;
}
mysql_free_result( $cur );
return $array;
}
function loadRowList( $key='' )
{
if (!($cur = $this->query())) {
return null;
Page 27
Jacky + Cupid+Kit

}
$array = array();
while ($row = mysql_fetch_array( $cur )) {
if ($key) {
$array[$row[$key]] = $row;
} else {
$array[] = $row;
}
}
mysql_free_result( $cur );
return $array;
}
function disconnect()
{
mysql_close( $this->_connection );
}
function escape_data($data)
{
if(ini_get('magic_quotes_gpc'))
{
$data = stripslashes($data);
}
return mysql_real_escape_string($data);
}
}
?>
-------------------------Class xy dng ngi dng hdt
<?php
include_once('database.php');
class xl_nguoi_dung
{
var $db=null;
function danh_sach_nguoi_dung()
{
$data = new database();
$data->setQuery('Select * From sinhvien');
Page 28
Jacky + Cupid+Kit

$result = $data->LoadAllRow();
$data->disconnect();
return $result;
}
function them_nguoi_dung($ho_ten, $ten_dang_nhap, $mat_khau,
$email,$dia_chi, $ngay_dang_ky)
{
$data = new database();
$data->setQuery("INSERT INTO nguoidung VALUES ('NULL','2',
'$ho_ten', '$ten_dang_nhap', '$mat_khau','$email', '$ngay_dang_ky','NULL')");
$result = $data->query();
$data->disconnect();
return $result;
}

function hien_thi_nguoi_dung_theo_sql($strsql)
{
$data = new database();
$data->setQuery($strsql);
mysql_query("set names utf8");
$result = $data->LoadAllRow();
$data->disconnect();
return $result;
}
function get_item($username,$pass)
{
$data = new database();
$data->setQuery("SELECT * FROM nguoidung WHERE username
='{$username}' AND password ='{$pass}'");
$row = $data->loadRow();
return $row;
}
}
?>
---------------------------Page 29
Jacky + Cupid+Kit

Main ng nhp
<?php ob_start();
session_start()?>
<body>
<form id="form1" name="form1" method="post" action="">
<table width="441" border="1" cellpadding="0" cellspacing="0">
<tr bgcolor="#D7DDDD">
<?php
if(isset($_POST["Tendn"]))
{
include_once("database/xl_nguoi_dung.php");
$xl_nguoi_dung= new xl_nguoi_dung();
$Bang_nguoi_dung=$xl_nguoi_dung->hien_thi_nguoi_dung_theo_sql("select
*from user where username='".$_POST["Tendn"]."' and
password='".$_POST["Matkhau"]."'");
if(count($Bang_nguoi_dung)>0)
foreach($Bang_nguoi_dung as $nd)
{
//$_SESSION["Mnd"]=$nd["user"];
$_SESSION['Hoten']= $nd['ho_ten'];
$_SESSION['dang_nhap'] = 'OK';
header("refresh:1;url=../../20110425/sanpham.php");
$hien_thi="";
if(isset($_SESSION["Hoten"]))
{
$hien_thi= 'Cho bn: '.$_SESSION["Hoten"].'<br><a
href="../20110425/trangchu.php">Vo trang lm vic.</a>';
}
else
{ $hien_thi="ng nhp sai.";}
}
}?>
<td colspan="2"><div align="center"><span
class="style1"><strong>Login</strong></span></div></td>
</tr>
<tr>
<td width="135" bgcolor="#D7DDDD">Username:</td>
Page 30
Jacky + Cupid+Kit

<td width="300" bgcolor="#D7DDDD"><input name="Tendn" type="text"


id="Tendn" /></td>
</tr>
<tr>
<td bgcolor="#D7DDDD">Password:</td>
<td bgcolor="#D7DDDD"><input name="Matkhau" type="password" id="Matkhau"
/></td>
</tr>
<tr>
<td height="26" colspan="2" valign="top" bgcolor="#D7DDDD"><div
align="center">
" />
</div></td>
</tr>
<tr>
<td height="21" colspan="2" valign="top" bgcolor="#D7DDDD">
<div align="center">
!"; else echo $hien_thi;?>
</div></td>
</tr>
</table>
</form>
</body>
</html>
--------------

Page 31
Jacky + Cupid+Kit

ng k

<body>
<?php
if(isset($_REQUEST['bt_dangky']))
{
if(!empty($_REQUEST['txt_ten']) && !empty($_REQUEST['txt_user']) &&
!empty($_REQUEST['txt_pass']))
{
$ten = $_REQUEST['txt_ten'];
$user = $_REQUEST['txt_user'];
$pass = $_REQUEST['txt_pass'];
$sdt = $_REQUEST['txt_sdt'];
$email = $_REQUEST['txt_email'];
$diachi =$_REQUEST['txt_diachi'];
$str='';
include_once("database/database.php");
$db = new database();
$sql = "INSERT INTO user VALUES
('$user','$pass','$ten','$diachi','$sdt','$email')";
//echo $sql;
$db -> setQuery($sql);
$db -> query();
$str ='ng k thnh cng!<a class="style8" href="dangnhap.php">Xin mi ng
nhp!</a>';}
elseif(!isset($_REQUEST['$user']) && !isset($_REQUEST["$email"]))
Page 32
Jacky + Cupid+Kit

{
! Th

!";

}
}
?>
<script language="javascript">
function check()
{
if(document.form1.txt_ten.value=="")
{
alert("Hy nh
n vo");
document.form1.txt_ten.focus();
return false;
}
if(document.form1.txt_user.value=="")
{
alert("Hy nh
n.");
document.form1.txt_user.focus();
return false;
}
if(document.form1.txt_pass.value=="")
{
alert("Hy nhp password");
document.form1.txt_pass.focus();
return false;
}
if(document.form1.txt_repass.value != document.form1.txt_repass.value)
{
alert("Bn nhp li password k ng");
document.form1.txt_pass.focus();
return false;
}
if(document.form1.txt_email.value=="")
{
alert("Hy nhp Email");
document.form1.txt_email.focus();
return false;
Page 33
Jacky + Cupid+Kit

}
if(document.form1.txt_diachi.value=="")
{
alert("Hy nh
");
document.form1.txt_diachi.focus();
return false;
}
if(document.form1.txt_sdt.value=="")
{
alert("Hy nh
");
document.form1.txt_sdt.focus();
return false;
}
return true;
}
</script>
<form id="form1" name="form1" method="post" action="">
<table width="441" border="1" cellpadding="0" cellspacing="0">
<tr bgcolor="#D7DDDD">
<td colspan="2"><div align="center"><span
class="style1"><strong>Registration</strong> <strong>Form</strong></span>
</div></td>
</tr>
<tr>
<td width="135" bgcolor="#D7DDDD">Username:</td>
<td width="300" bgcolor="#D7DDDD"><input name="txt_user" type="text"
id="txt_user" value="" /></td>
</tr>
<tr>
<td bgcolor="#D7DDDD">Password:</td>
<td bgcolor="#D7DDDD"><input name="txt_pass" type="password" id="txt_pass"
/></td>
</tr>
<tr>
Page 34
Jacky + Cupid+Kit

<td bgcolor="#D7DDDD">Re-Password:</td>
<td bgcolor="#D7DDDD"><input name="txt_repass" type="password"
id="txt_repass" /></td>
</tr>
<tr>
<td bgcolor="#D7DDDD">Full name: </td>
<td bgcolor="#D7DDDD"><label>
<input name="txt_ten" type="text" id="txt_ten" value="" />
</label></td>
</tr>
<tr>
: </td>
<td bgcolor="#D7DDDD"><input name="txt_diachi" type="text" id="txt_diachi"
size="50" /></td>
</tr>
<tr>
: </td>
<td bgcolor="#D7DDDD"><input name="txt_sdt" type="text" id="txt_sdt" value=""
/></td>
</tr>
<tr>
<td bgcolor="#D7DDDD">Email:</td>
<td bgcolor="#D7DDDD"><input name="txt_email" type="text" id="txt_email"
value="" /></td>
</tr>
<tr>
<td height="26" colspan="2" valign="top" bgcolor="#D7DDDD"><div
align="center">
<input name="bt_dangky" type="submit" id="bt_da
"
onclick="check();" />
</div></td>
</tr>
<tr>
<td height="21" colspan="2" valign="top" bgcolor="#D7DDDD">
<div align="center">
<?php if(isset($_REQUEST["bt_dangky"]))
echo $str; ?>
Page 35
Jacky + Cupid+Kit

</div> </td>
</tr>
</table>
</form>
</body>

Page 36
Jacky + Cupid+Kit

Sn phm (sau khi ng nhp thnh cng s thm sn phm)

----------------------------File connect.php

<?php
include "config.php";
@mysql_connect($host_db, $user_db, $pass_db) or die("Error Mysql");
@mysql_select_db($database) or die("Error DB");
mysql_query("set names 'utf8' ");
?>
-----------------------------File sn phm

<?php session_start() ?>


<?php
include "connect.php";
$sql ="select * from sach ";
$kq = mysql_query($sql);
?>
<body>
<?php
if(!isset($_SESSION["Hoten"]))
Page 37
Jacky + Cupid+Kit

{
$thongbao ='';
while ($r = mysql_fetch_array($kq))
{
$m = $r["masach"]; $t = $r["tensach"]; $g =$r["gia"];
$h = "image_data/".$r["hinh"];
echo "<div style='width:180px; height:150px; float:left;
margin:5px;background:#Caa;'>";
echo "$m - $t <br>$g<br>";
echo "<img src='$h' height=80>";
//echo "<a href='giohang.php?id=$m&action=add'>Mua</a>";
echo "</div>";
}
</a>';
}
else{
e
</a><br>';
while ($r = mysql_fetch_array($kq))
{
$m = $r["masach"]; $t = $r["tensach"]; $g =$r["gia"];
$h = "image_data/".$r["hinh"];
echo "<div style='width:180px; height:150px; float:left;
margin:5px;background:#Caa;'>";
echo "$m - $t <br>$g<br>";
echo "<img src='$h' height=80>";
echo "<a href='giohang.php?id=$m&action=add'>Mua</a>";
echo "</div>";
}
}
?>
</div>
<div>
</div><?php echo $thongbao; ?>
</div>
</body>
</html>
Page 38
Jacky + Cupid+Kit

------------------------------

Thm vo gi hng:

<?php
if (!isset($_SESSION)) session_start();
print_r($_REQUEST);
//unset($_SESSION["gh"]);
include "connect.php";
$ghtmp= array();
if (isset($_SESSION["gh"])) $ghtmp = $_SESSION["gh"];
$id= isset($_REQUEST["id"])?$_REQUEST["id"]:"";
$sl = isset($_REQUEST["sl"])?$_REQUEST["sl"]:1;
$sl = floor($sl *1);
$action = isset($_REQUEST["action"])?$_REQUEST["action"]:"";
if ($action=="add")
{
if ($id !="") themGH($id, $sl, $ghtmp);
}
if ($action=="update")
{
if ($id !="") CapnhatGH($id, $sl, $ghtmp);
}

function themGH($id, $sl, &$ghtmp)


{
if (!isset($ghtmp[$id]))
$ghtmp[$id]= $sl;
else $ghtmp[$id]= $ghtmp[$id]+$sl;
}

Page 39
Jacky + Cupid+Kit

function CapNhatGH($id, $sl, &$ghtmp)


{
if ($sl<=0) unset($ghtmp[$id]);
else
$ghtmp[$id]= $sl;
}
function showGH($gh)
{
if (Count($gh)==0) {echo "Gio hang rong"; return;}
?>
<table width="700" border="1">
<tr>
<td>Ma SP</td>
<td>Ten SP</td>
<td>Hinh</td>
<td>Gia</td>
<td>SL</td>
<td>Thanh Tien</td>
<td>Thay doi</td>
</tr>
<?php
$tong =0;
foreach($gh as $id=>$sl)
{
$sql ="select * from sach where masach='$id' ";
$kq = mysql_query($sql);
if (mysql_num_rows($kq)>0)
{
$r = mysql_fetch_array($kq);
$t = $r["tensach"]; $g= $r["gia"];
$h = "image_data/".$r["hinh"];
$tt = $g * $sl; $tong = $tong + $tt;
?>
<form action=giohang.php>
<input type="hidden" name="id" value="<?php echo $id;?>">
<input type="hidden" name="action" value="update">
<tr>
<td><?php echo $id;?></td>
<td><?php echo $t;?></td>
Page 40
Jacky + Cupid+Kit

<td><img src='<?php echo $h;?>' height="100"></td>


<td><?php echo $g;?></td>
<td><input type=text name=sl value="<?php echo $sl;?>"></td>
<td><?php echo $tt;?></td>
<td><input type="submit" value="Sua">
</tr></form>
<?php
}
}
?>
<tr>
<td colspan="5">Tong</td>
<td><?php echo $tong;?></td>
</tr>
</table>
<?php
}
$_SESSION["gh"]=$ghtmp;
?>
<body>Noi dung gio hang<hr />
<?php
//print_r($_SESSION["gh"]);
showGH($ghtmp);
?>
<hr />
<a href="sanpham.php">Tiep tuc mua hang</a>
</body>
</html>

Page 41
Jacky + Cupid+Kit

You might also like