删除并重定向到php中的页面

时间:2016-11-03 09:54:17

标签: javascript php

我试图删除php中的条目并在php中重定向页面。 在删除页面未被重定向到header("Location: report2.php?region=$region");后,我需要根据条件将页面重定向为header("Location: report2.php?region=$region"); 它也不会删除。它只重定向到report2.php?delete_id = 103 这是代码

  ?php
//Give your mysql username password and database name
include_once "db.php";
if(isset($_REQUEST['region']))
{
$region=$_REQUEST['region'];

//connection to the database


$qresult = mysql_query("SELECT * FROM registration where region='".$region."'");

    echo "<h1>Registration Details of $region Region.</h1>";
if (mysql_num_rows($qresult) == 0) { 
echo "<h3 style=color:#0000cc;text-align:center;>No Registrations Done..!</h3>"; 
} else { 


        echo "<table border='1' cellpadding='0' cellspacing='0' class='reports'>";

        echo "<tr>" .
                    "<td style='background: #f1f1f1; font-weight: bold;text-align:center;'>Registration Number</td>" .
                    "<td style='background: #f1f1f1; font-weight: bold;text-align:center;'>Name</td>" . 
"</tr>";

        while($row = mysql_fetch_assoc($qresult)) { 
                       echo "<tr>" .
                    "<td style=text-align:center;>" . $row['id'] . "</td>" . 

                    "<td style=text-align:center;>".  $row['name']  . "</td>"; 


                ?>

              <td><a href="javascript:delete_id(<?php echo $row['id']; ?>)"><img src="images/delete21.png" alt="Delete" /></a></td>


                    </tr>
        <?php   
        }
        }}
        ?>
        </table>
 <?php
 if(isset($_GET['delete_id']))
  {
    $sql_query="DELETE FROM registration WHERE id=".$_GET['delete_id'];
    mysql_query($sql_query);

    header("Location: report2.php?region=$region");
    exit;
  }

?>  

1 个答案:

答案 0 :(得分:0)

我认为您的问题是基于错误的格式化,因此您没有注意到您在下一个if语句之前没有关闭,我认为这并不是您想要的。

以下是您的固定和格式化代码:

<?php

  include_once "db.php";
  if(isset($_REQUEST['region'])) 
  {
    $region=$_REQUEST['region'];

    $sql_query="SELECT * FROM registration where region='".$region."' ";
    $result_set=mysql_query($sql_query);
  }
  if(isset($_GET['delete_id']))
  {

    $sql_query="SELECT * FROM registration where id='".$_GET['delete_id']."' ";
    $result=mysql_query($sql_query);
    while($row = mysql_fetch_assoc($result)) { 
      $region = $row['region'];
    }

    $sql_query="DELETE FROM registration WHERE id=".$_GET['delete_id'];
    mysql_query($sql_query);

    header("Location: report2.php?region=".$region);
    exit;
  }

?>
<script type="text/javascript">
    function delete_id(id) {
      if(confirm('Sure To Remove This Record ?')) {
          window.location.href='report2.php?delete_id='+id;
        }
    }
</script>