我无法删除使用输入delete选择的行。我认为我必须在querydelete中设置其他参数,但我不知道到底是什么。
有人可以帮助我,因为我是初学者吗?还有另一种方法可以做类似的事情吗?
我的目的是推送删除输入,然后删除具有特定ID的客户(与删除输入相同)。
有人可以给我链接一个例子吗?
<!DOCTYPE html>
<html>
<head>
<title>Table with database</title>
<style>
table {
border-collapse: collapse;
width: 100%;
color: #588c7e;
font-family: monospace;
font-size: 25px;
text-align: left;
}
th {
background-color: #588c7e;
color: white;
}
tr:nth-child(even) {background-color: #f2f2f2}
</style>
</head>
<body>
<table>
<tr>
<th>ID</th>
<th>Room</th>
<th>Name</th>
<th>Check In</th>
<th>Check Out</th>
</tr>
<?php
include('db_connection.php');
$conn = OpenCon();
//SQL query
$query = "Select * from ergazomenos";
if(isset($_POST['delete'])){
$querydelete = "delete from ergazomenos where trim(ID)
='$_POST[hidden]'";
$queryexee = mysqli_query($conn, $querydelete);
}
$result = mysqli_query($conn, $query);
if (!$result){
echo("Error description: " . mysqli_error($conn));
}
//query database
while($rows = mysqli_fetch_array($result)){
$ID = $rows['ID'] ;
$Room = $rows['Room'] ;
$Name = $rows['Name'];
$CheckIn = $rows['Check In'] ;
$CheckOut = $rows['Check Out'] ;
//echo "</td><td>" . $ID. "</td><td>" "<input type=hidden ID=hidden
value=" . $rows['ID'] . $Room. "</td><td>". $Name. "</td><td>" . $CheckIn. "
</td><td>" . $CheckOut. "</td><td>";
echo "</td><td>" . $ID. "</td><td>" . $Room. "</td><td>". $Name.
"</td><td>" . $CheckIn. "</td><td>" . $CheckOut. "</td><td>";
</td>";
echo ("<form action=delete.php method=post>");
echo ("<tr><td><div align=\"center\"> $ID </div>" . "<input
type=hidden name=hidden value=".$rows['ID'] . "</td> <td><div
align=\"center\">
$Room </div></td> <td><div align=\"center\"> $Name </div></td> <td><div
align=\"center\"> $CheckIn </div></td> <td><div align=\"center\"> $CheckOut
</div></td> <td><div align=\"center\"> <td><div ");
echo ("<td>" . "<input type=submit name=delete value=delete" . "
</td>");
//
echo ($rows['ID']);
//echo '<td><input type="button" name="delete"
value="delete"></td>';
echo ("</tr>");
echo ($_POST['hidden']);
}
CloseCon($conn);
?>
</table>
<button type="button" onclick="alert('Hello world!')">Insert</button>
<button type="button" onclick="alert('Hello world!')">Update</button>
</body>
</html>
答案 0 :(得分:1)
您应该使用prepare语句来防止SQL注入
int main() {
testArray[1].letter = 'v';
return 0;
}
这样更安全。
答案 1 :(得分:0)
尝试从以下位置更改 $ querydelete :
$querydelete = "delete from ergazomenos where trim(ID)
='$_POST[hidden]'";
到
$querydelete = "delete from ergazomenos where trim(ID)=". $_POST['hidden'];
这应该有所帮助。是的,我没有测试代码。
答案 2 :(得分:0)
您必须在input
标记中使用(单引号或转义的双引号)属性值:
<input type='hidden' name='hidden' [etc.]
答案 3 :(得分:0)
分享我的风格。
沿着每个数据库驱动的记录,在名为Action
的网格中创建一列。
<td>
<a href="delete.php?id=<?php echo $rows['ID'];?>">Delete</a>
</td>
在delete.php
文件中:
include('connection.php');
$id = $_GET['id'];
$query= "delete from table where id = '$id'";
mysqli_query($conn, $query); // or $dbConn->query($query);
重定向到网格页面。