用php删除记录然后重新加载页面

时间:2014-10-30 01:33:03

标签: php mysql

我正在构建一个简单的网页,允许用户从数据库中删除记录,然后页面将重新加载并删除记录。无法找出用于实现此目的的代码。这是我的主页:

<html>
<head>
<title>Change Record form</title>

<style type="text/css">
td {font-family: tahoma, arial, verdana; font-size: .875em }
</style>


</head>
<body>
<a href="add_new_form.php">Add new record</a>
<br>
<br>
<?php
  $con=mysqli_connect("localhost", "root", "", "customers");
     if (mysqli_connect_errno()) {
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$strSQL = "SELECT * FROM music";

$rs = mysqli_query($con, $strSQL);

while($row = mysqli_fetch_array($rs, MYSQLI_BOTH)) {

   // Write the value of the column FirstName (which is now in the array $row)
  echo "<br>";
  echo "<br>";    
  echo $row['artist'] . "<br />";
  echo $row['title'] . "<br />";
  echo $row['format'] . "<br />";
  echo $row['notes'] . "<br />";

  echo '<FORM METHOD="LINK" ACTION="update_form.php">
        <INPUT TYPE="submit" VALUE="Update">
        </FORM>';

  echo '<FORM METHOD="LINK" ACTION="delete_process.php">
        <INPUT TYPE="submit" VALUE="Delete">
        </FORM>';
 }
?> 

然后这是delete_process页面:

<?php
$id = $_GET['id'];
$artist = $_GET['artist'];
$title = $_GET['title'];
$format = $_GET['format'];
$notes = $_GET['notes'];

//create connection to DB
$con=mysqli_connect("localhost", "root", "", "customers");

//check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$sql = "DELETE from 'MUSIC' WHERE 'id' = $id";

mysql_query($sql);
?>

基本上,当用户单击删除按钮时,将删除该特定记录,并在删除记录时重新加载主页面。不要真的知道代码,谢谢!

2 个答案:

答案 0 :(得分:0)

在delete_process.php

的末尾添加标题
<?php
//redirect to index page
header('Location:index.php'); //replace index.php with the page you want to redirect to
?>

应该有效

答案 1 :(得分:0)

您需要添加

'<input type="hidden" name="id" value="' . $row['id'] . '" />'

发布到delete_process.php的表单。

但是,您应该知道您发布的代码非常不安全,通过SQL注入攻击并删除整个数据库也是微不足道的。