Em đang học về php&mysql và thử tạo 1 bảng csdl:
phongban(phong,ngaytao)
Vấn đề là khi em tạo nút delete cho từng dòng thì khi ấn nút delete thì csdl ko xóa dòng đó mà xóa hết tất cả dữ liệu.
Ai biết cách tạo nút del thì giúp mình với. Thanks nhiều
code:
Code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<?php
$conn = mysql_connect('localhost','root','')or die ('can\'t connect to sever');
mysql_select_db('quanly',$conn) or die ('ko tim thay csdl');
//Check submit button
$phong=$_POST['dt_phong'];
$nv=$_POST['n_tao'];
$delete = del'.$delphong.';
//ADD
if(isset($_POST['add'])){
$add_db = mysql_query('INSERT INTO phongban (phong,ngaytao) VALUE ("'.$phong.'" ,"'.$nv.'")');
}
// DELETE
if(isset($_POST[$delete])){
if(mysql_num_rows($sql)>0){
$del_db = mysql_query('DELETE FROM phongban WHERE phong="'.$delphong.'"') or die ('can\'t delete');
}
}
$sql = mysql_query('SELECT phong,ngaytao FROM phongban');
?>
<form method="post" action="phongban.php">
<table width="331" border="1">
<tr>
<td width="167">Phong Ban </td>
<td width="148">Ngay Tao </td>
</tr>
<tr>
<td><input type="text" name="dt_phong" /></td>
<td><input type="text" name="n_tao" /></td>
<td><input type="submit" name="add" value="ADD" /></td>
</tr>
<tr>
<?php
if($sql){
if(mysql_num_rows($sql)>0){
while($row = mysql_fetch_array($sql)){
$delphong = $row['phong'];
echo '<tr>';
echo '<td>'.$row['phong'].'</td>';
echo '<td>'.$row['ngaytao'].'</td>';
echo '<td><input type="submit" name="del'.$delphong.'" value="DEL" /></td>';
}
}
}
?>
</tr>
</table>
</form>
</body>
</html>
Bookmarks