从数据库字符串输入中删除信息

时间:2014-02-11 15:31:45

标签: php mysql mysqli

我希望使用下面的代码从数据库中删除特定的行。下面的代码位于一个名为“delete.php”的文件中,它从一个输入框中获取输入,该输入框位于另一个名为“yourReports.php”的php文件中。

当表单在“yourReports.php”上提交时,它应该从数据库中删除该行,但它似乎不起作用。

delete.php

<?php 

        $mysqli = mysqli_connect("localhost", "root", "DBPASS","DBNAME") or die ('Could not connect to database!');   
        $_POST['delete'];
        $deletereport = mysqli_real_escape_string($mysqli, $_POST['delete']);

        mysqli_query($mysqli,"DELETE FROM reports WHERE reportName = '".$deletereport."'");

        header('Location: yourreports.php');

?>

yourReports.php

<form action="delete.php" method="post" name="form1">
            <label><strong>Enter Report Name To Delete:</strong></label>
            <input name="delete"  id="delete" type="text">
            <input value="Delete Report" name="delete" class='myButton' type="submit">
</form>

任何帮助将不胜感激。

由于

2 个答案:

答案 0 :(得分:1)

将表单的方法和名称属性设置为post

<form action="delete.php" method="post" name="form1">

检查查询是否失败:

if(! mysqli_query($mysqli,"DELETE FROM reports WHERE reportName = '".$deletereport."'")){
  echo mysqli_error();
}

[更新]

您不能有两个具有相同名称的输入。您的输入和删除按钮都具有名称delete。因此,请尝试考虑输入的不同名称。

答案 1 :(得分:0)

更改

 mysqli_query($mysqli,"DELETE FROM reports WHERE reportName = '".$deletereport."'");

  mysqli_query($mysqli,"DELETE FROM reports WHERE reportName = '{$deletereport}'");