2017-07-15 81 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或表單提交。