以下代码适用于$ _GET但不适用于$ _POST .
<?php
include_once 'dbconfig.php';
if(isset($_GET['delete_id']))
{
$sql_query="DELETE FROM users WHERE user_id=".$_GET['delete_id'];
mysqli_query($conn,$sql_query);
header("Location: $_SERVER[PHP_SELF]");
}
?>
<script type="text/javascript">
function edt_id(id)
{
if(confirm('Sure to edit ?'))
{
window.location.href='edit_data.php?edit_id='+id;
}
}
function delete_id(id)
{
if(confirm('Sure to Delete ?'))
{
window.location.href='index.php?delete_id='+id;
}
}
</script>
</head>
<body>
<center>
<div id="body">
<div id="content">
<table align="center">
<tr>
<th colspan="5"><a href="add_data.php">add data here.</a></th>
</tr>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>City Name</th>
<th colspan="2">Operations</th>
</tr>
<?php
$sql_query="SELECT * FROM users";
$result_set=mysqli_query($conn,$sql_query);
while($row=mysqli_fetch_row($result_set))
{ ?>
<tr>
<td><?php echo $row[1]; ?></td>
<td><?php echo $row[2]; ?></td>
<td><?php echo $row[3]; ?></td>
<td align="center"><a href="javascript:edt_id('<?php echo $row[0]; ?>')"><img src="b_edit.png" align="EDIT" /></a></td>
<td align="center"><a href="javascript:delete_id('<?php echo $row[0]; ?>')"><img src="b_drop.png" align="DELETE" /></a></td>
</tr>
<?php
} ?>
</table>
</div>
</div>
</center>
</body>
</html>
我已将isset值和查询更改为post而不是get . 但是那时数据表的数据删除按钮不起作用 . 在isset中更改get方法并查询并刷新页面后,该行将被删除 . 它的原因是什么?
4 回答
那是因为这些台词
将在URL上生成一个查询字符串,该查询字符串将传递给
$_GET
数组中的PHP而不是$_POST
数组中的PHP .如果要使用POST,则必须在HTML中使用
<form>
.如果要使用方法POST,则必须使用
ajax post
或使用<form method="post">
.和
是$ _GET,要做$ _POST,使用
<form action="#" method="post">
和它们内部的按钮 . 那应该是诀窍 .此行始终通过$ _GET数组传递值 . 如果你想使用$ _POST方法,你应该使用ajax或表单提交 .