Skip to main content

PHP#21MySQL#17 - tin_them.php, process.php - Thêm tin mới, Xử lý tin mới

 //tin_them.php

<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript" src="ckeditor/ckeditor.js"></script>
</head>

<body>
<?php
include("../connect.php");
if(isset($_GET['lang'])) $lang=$_GET['lang'];
else $lang='vi';
?>
<form id="form1" name="form1" method="get" action="">
<p>
<label for="lang">Ngon ngu:</label>
<select name="lang" id="lang" onchange="form1.submit();">
<option value="vi">Viet</option>
<option value="en" <?php if($lang=='en') echo "selected='selected'";?>>Anh</option>
</select>
</p>
<p>
<label for="idTL">The loai:</label>
<select name="idTL" id="idTL" onchange="form1.submit();">
<?php
$kq=mysqli_query($link,"select * from theloai where lang='$lang' order by ThuTu");
$idTL=0;
while($d=mysqli_fetch_array($kq)){
if($idTL==0) $idTL=$d['idTL'];
?>
<option value="<?php echo $d['idTL'];?>" <?php if(isset($_GET['idTL'])&&$d['idTL']==$_GET['idTL']){ echo "selected='selected'"; $idTL=$_GET['idTL'];}?>><?php echo $d['TenTL'];?></option>
<?php }?>
</select>
</p>

</form>


<form action="process.php" method="post" enctype="multipart/form-data" name="form2" id="form2">
<p>
<label for="idLT">Loai tin:</label>
<select name="idLT" id="idLT" >
<?php
$kqlt=mysqli_query($link,"select * from loaitin where idTL=$idTL order by ThuTu");

while($dlt=mysqli_fetch_array($kqlt)){
?>
<option value="<?php echo $dlt['idLT'];?>"><?php echo $dlt['Ten'];?></option>
<?php }?>
</select>
</p>
<p>
<label for="TieuDe">Tieu de:</label>
<input type="text" name="TieuDe" id="TieuDe" />
</p>
<p>
<label for="TieuDe_KhongDau">Tieu de KD:</label>
<input type="text" name="TieuDe_KhongDau" id="TieuDe_KhongDau" />
</p>
<p>
<label for="TomTat">Tom tat:</label><br/>
<textarea name="TomTat" id="TomTat" cols="45" rows="5"></textarea>
</p>
<p>
<label for="ufile">Chon hinh:</label>
<input type="file" name="ufile" id="ufile" />
</p>
<p>
<label for="idSK">Su kien:</label>
<select name="idSK" id="idSK">
<?php
$kqsk=mysqli_query($link,"select * from sukien where lang='$lang' order by ThuTu");
while($dsk=mysqli_fetch_array($kqsk)){
?>
<option value="<?php echo $dsk['idSK'];?>"><?php echo $dsk['MoTa'];?></option>
<?php }?>
</select>
</p>
<p>
<label for="Content">Noi dung:</label>
<textarea name="Content" id="Content" cols="45" rows="5" class="ckeditor"></textarea>
</p>
<p>
<input type="checkbox" name="TinNoiBat" id="TinNoiBat" />
<label for="TinNoiBat">Noi bat</label>
</p>
<p><label>Trang thai:</label>
<select name="AnHien">
<option value="0">An</option>
<option value="1">Hien</option>
</select>
</p>
<p>
<input type="hidden" name="lang" value="<?php echo $lang;?>"/>
<input type="hidden" name="idTL" value="<?php echo $idTL;?>"/>
<input type="submit" name="themtin" id="themtin" value="Them tin" />
<br />
</p>
</form>
</body>
</html>

//process.php
<?php
include("../connect.php");
date_default_timezone_set("Asia/Ho_Chi_Minh");
//Xử lý thêm 1 thể loại mới:
if(isset($_POST['themtl']))
{
$sl="insert into theloai values(NULL, '{$_POST['lang']}', '{$_POST['TenTL']}', '{$_POST['TenTL_KhongDau']}', {$_POST['ThuTu']}, {$_POST['AnHien']})";
if(mysqli_query($link,$sl))
header("location:theloai.php?lang=".$_POST['lang']);
else echo $sl;
}
//Xử lý cập nhật 1 thể loại:
if(isset($_POST['suatl']))
{
$sl="update theloai set lang='{$_POST['lang']}', TenTL='{$_POST['TenTL']}', TenTL_KhongDau='{$_POST['TenTL_KhongDau']}', ThuTu={$_POST['ThuTu']}, AnHien={$_POST['AnHien']} where idTL={$_POST['idTL']}";
if(mysqli_query($link,$sl))
header("location:theloai.php?lang=".$_POST['lang']);
else echo $sl;
}

// Xử lý xóa 1 thể loai
if(isset($_GET['xoatl']))
{
$sl="delete from theloai where idTL=".$_GET['xoatl'];
if(mysqli_query($link,$sl))
header("location:theloai.php?lang=".$_GET['lang']);
else echo $sl;
}

//Xử lý thêm 1 loại tin mới:
if(isset($_POST['them_lt']))
{
$sl="insert into loaitin values(NULL, '{$_POST['lang']}', '{$_POST['Ten']}', '{$_POST['Ten_KhongDau']}', {$_POST['ThuTu']}, {$_POST['AnHien']}, {$_POST['idTL']})";
if(mysqli_query($link,$sl))
header("location:loaitin.php?lang=".$_POST['lang']."&theloai=".$_POST['idTL']);
else echo $sl;
}

//Xử lý cập nhật 1 loại tin:
if(isset($_POST['sua_lt']))
{
$sl="update loaitin set lang='{$_POST['lang']}', Ten='{$_POST['Ten']}', Ten_KhongDau='{$_POST['Ten_KhongDau']}', ThuTu={$_POST['ThuTu']}, AnHien={$_POST['AnHien']}, idTL={$_POST['idTL']} where idLT={$_POST['idLT']}";
if(mysqli_query($link,$sl))
header("location:loaitin.php?lang=".$_POST['lang']."&theloai=".$_POST['idTL']);
else echo $sl;
}

//Xử lý xóa 1 loại tin:
if(isset($_GET['xoalt']))
{
$sl="delete from loaitin where idLT=".$_GET['xoalt'];
if(mysqli_query($link,$sl))
header("location:loaitin.php?lang=".$_GET['lang']."&theloai=".$_GET['idTL']);
else echo $sl;
}
//Xử lý thêm 1 tin mới:
if(isset($_POST['themtin']))
{
//Xử lý upfile:
if(isset($_FILES['ufile']))
{
$target="../dataupload/images/";
$filename=basename($_FILES['ufile']['name']);
$target.=$filename;
if(move_uploaded_file($_FILES['ufile']['tmp_name'],$target))
{
$url="/tintuc/dataupload/images/".$filename;
$ngay=date("Y-m-d h:i:s",time());
if(isset($_POST['TinNoiBat'])&&$_POST['TinNoiBat']=="on")
$tnb=1;
else $tnb=0;

//thêm dữ liệu vào bảng tin:
$sl="insert into tin values(NULL, '{$_POST['lang']}', '{$_POST['TieuDe']}', '{$_POST['TieuDe_KhongDau']}', '{$_POST['TomTat']}', '$url', '$ngay', 1, {$_POST['idSK']}, '{$_POST['Content']}', {$_POST['idLT']}, 0, $tnb, {$_POST['AnHien']})";
if(mysqli_query($link,$sl))
header("location:tin.php?lang=".$_POST['lang']."&idTL=".$_POST['idTL']."&idLT=".$_POST['idLT']);
else
{echo $sl; unlink($target);}
}
else echo "upload thất bại!";
}
}
?>

Comments

Popular posts from this blog

PHP#33MySQL#29 - Đếm lượt truy cập (KẾT THÚC KHÓA HỌC)

//hitcounter.php <?php session_start();  ?> <!doctype html> <html> <head> <meta charset="utf-8"> <title>Untitled Document</title> </head> <body> <?php  include("../connect.php"); //cập nhật số luowhtj truy cập: $sl="update counter set cnt=cnt+1"; mysqli_query($link,$sl); //Lấy dữ liệu số lượt truy cập để hiển thị ra web: $sl2="select * from counter"; $kq=mysqli_query($link,$sl2); $d=mysqli_fetch_array($kq); //Định dạng: $chuoi=str_pad($d['cnt'],6,"0",STR_PAD_LEFT); $luottrycap=""; for($i=0;$i<strlen($chuoi);$i++) { $tam=substr($chuoi,$i,1); $luottrycap.="<img src='images/$tam.png'/>"; } /*-Khi user truy cập vào website: +lần đầu tuy cập: Lưu thông tin của user lên csdl +Sau lần đầu tiên: cập nhtaaj lastvisit - Xóa các dòng dữ liệu quá 1 phút (Quy ước thời gian để khẳng định online hay offline: 1 phút) - Thống kê số liệu người dùng o...

Bài 2 - SOME OTHER TIME - Vào dịp khác

PHP#6 - MySQL#2 - Thêm, Sửa, Xóa trong SQL, tìm kiếm

  1. Tạo bảng khachhang với các thuộc tính sau: 2. Dùng lệnh SQL : - Thêm dữ liệu cho bảng khachhang: => INSERT INTO khachhang VALUES(1, "nguyen VAN thanh", "aa", "123", "Q1", "a") INSERT INTO khachhang VALUES(2, "NGUYEN thi Linh", "bb", "123456", "q1", "123456", "b") INSERT INTO khachhang VALUES(3, "Nguyen Van Teo", "teovn", "123456", "aa", "88", "lo@a.com") Sửa Địa chỉ của user aa thành Q10 =>UPDATE khachhang SET DiaChi="Q10" WHERE UserName="aa" Sửa số điện thoại của user teonv thanh 093445212 =>UPDATE khachhang SET DienThoai="093445212" WHERE UserName="teovn" Xóa tất cả user nào có email la b =>DELETE FROM khachhang WHERE Email="b" //KẾT QUẢ //Bài tiếp theo: Câu lệnh Select đơn giản Chỉ có phần select from. Có thể có thêm where. Ví dụ: 1.   SELECT ...