Tuesday, 18 January 2022

PHP#20MySQL#16 - Xóa LOẠI TIN

//loaitin.php (phần Xóa)

<!doctype html>

<html>

<head>

<meta charset="utf-8">

<title>Loại Tin</title>

</head>


<body>

<?php

include("../connect.php");

if(isset($_GET['lang'])) $lang=$_GET['lang'];

else $lang="vi";

?>

<form method="get" id="form1" name="form1">

  <p>

    <label for="loaitin">Chọn ngôn ngữ:</label>

    <select name="lang" id="lang" onChange="form1.submit();">

      <option value="vi">Việt</option>

      <option value="en"<?php if($lang=="en") echo "selected";?>>English</option>

    </select>

  </p>

  <p>

    <label for="theloai">Thể loại:</label>

    <select name="theloai" id="theloai" onChange="form1.submit();">

<?php 

$sl="select * from theloai where lang='$lang' order by ThuTu";

$kq=mysqli_query($link,$sl);

$idTL=0;

while($d=mysqli_fetch_array($kq))

{

if($idTL==0) $idTL=$d['idTL'];

?>

      <option value="<?php echo $d['idTL'];?>"<?php if(isset($_GET['theloai'])&&$_GET['theloai']==$d['idTL']) {echo "selected"; $idTL=$_GET['theloai'];}?>><?php echo $d['TenTL'];?></option>

      <?php }?>

    </select>

  </p>

</form>

<table width="700" border="1" cellspacing="0" cellpadding="0">

  <tbody>

    <tr>

      <th scope="col">Thứ tự</th>

      <th scope="col">Tên loại</th>

      <th scope="col">Tên không dấu</th>

      <th scope="col">Trạng thái</th>

      <th scope="col"><a href="loaitin_them.php?lang=<?php echo $lang;?>&idTL=<?php echo $idTL;?>">Thêm</a></th>

    </tr>

    <?php 

$sl="select * from loaitin where idTL=$idTL order by ThuTu";

$kq=mysqli_query($link,$sl);

while($d=mysqli_fetch_array($kq)){

?>

    <tr>

      <td><?php echo $d['ThuTu'];?></td>

      <td><?php echo $d['Ten'];?></td>

      <td><?php echo $d['Ten_KhongDau'];?></td>

      <td><?php if($d['AnHien']) echo "Hiện"; else echo "Ẩn";?></td>

      <td><a href="process.php?xoalt=<?php echo $d['idLT']?>&lang=<?php echo $lang;?>&theloai=<?php echo $idTL;?>" onClick="return confirm('Bạn có muốn xóa thể loại này không?')";>Xóa</a>/<a href="loaitin_sua.php?idLT=<?php echo $d['idLT']?>&lang=<?php echo $lang;?>">Sửa</a></td>

    </tr>

    <?php }?>

  </tbody>

</table>

</body>

</html>


//process.php (phần Xóa)

<?php 

include("../connect.php");

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ửa lý sửa 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($kq=mysqli_query($link,$sl))

header("location:theloai.php?lang=".$_POST['lang']);

else echo $sl;

}

//Xử lý xóa 1 thể loại:

if(isset($_GET['xoatl']))

{

$sl="delete from theloai where idTL=".$_GET['xoatl'];

if($kq=mysqli_query($link,$sl))

header("location:theloai.php?lang=".$_GET['lang']);

else echo $sl;

}

//Xửa lý thêm 1 loại tin:

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;

}

//sửa 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($kq=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['theloai']);

else echo $sl;

}

?>

1 comment: