2017-07-15 133 views
-1

以下代码适用于$ _GET,但不适用于$ _POST。POST不工作,而不是GET

<?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值并查询以发布而不是get。但是当数据表的数据删除按钮不起作用时。在isset中更改get方法并查询并刷新页面后,行将被删除。它的原因是什么?

回答

0

这是因为这些线路

window.location.href='edit_data.php?edit_id='+id; 

会生成在其传递给PHP的$_GET阵列中,而不是$_POST数组中的URL查询字符串。

如果您想使用POST,您将不得不在HTML中使用<form>

0

如果您想使用方法POST, 您必须使用ajax post或使用<form method="post">

0
window.location.href='index.php?delete_id='+id; 

entewindow.location.href='edit_data.php?edit_id='+id; 

都$ _GET,做$ _ POST,使用<form action="#" method="post">与他们的内部的按钮。然后,这应该做的伎俩。

0
window.location.href='index.php?delete_id='+id; 

此行总是通过$ _GET数组传递值。如果你想使用$ _POST方法,你应该使用ajax或表单提交。