如何从数据库中删除某一行数据

时间:2016-05-26 07:23:01

标签: php mysqli

我想删除数据库中的某些数据行。这是我的代码,它无法删除数据。我在错误的get id部分?我在数据库表中设置了自动增量ID。

<form name="form1" action="" method="post" enctype="multipart/form-data">
    <table>
        <tr>
            <td>select files</td>
            <td><input type="file" name="f1"></td>
        </tr>
        <td><input type="submit" name="submit1" value="upload"></td>
        <td><input type="submit" name="submit2" value="display"></td>
    </table>
</form>

<?php
$con = mysqli_connect("localhost","root","");
mysqli_select_db($con,"fyp")or die(mysqli_error($con));;
if(isset($_POST["submit2"]))
{
    $sql = "SELECT username, image FROM images where username = '$_SESSION[username]' ";
    $result = mysqli_query($con,$sql);
    echo"<table>";
    echo "<tr>";
    while ($row=mysqli_fetch_array($result))
    {
        echo "<td>";
        echo '<img src="data:image/jpeg;base64,'.base64_encode($row['image']).'" height="100" width="100"/>';
        echo "<br>";
        ?><a href = "delete.php?id=<?php echo $row['id'];?>" class = "delete">Delete</a><?php
        echo "</td>"; 


    }
    echo "</tr>";
}
下面的代码是我的delete.php

 <?php
$con = mysqli_connect('localhost', 'root', '');
mysqli_select_db($con,'images');
$id = $_GET["id"];
$sql = "DELETE FROM images where id =$id";
$result = mysqli_query($con,$sql);
?>


<script type="text/javascript">
window.location = "furprototype.php"
</script>

1 个答案:

答案 0 :(得分:1)

<?php
error_reporting(E_ALL);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$con = mysqli_connect('localhost', 'root', '','fyp');
$stmt = $con->prepare("DELETE FROM images where id =?");
$stmt->bind_param('i', $_GET["id"]);
$stmt->execute();

您应该使用预备语句进行数据库交互,并且应始终设置正确的错误报告,以警告您错误的数据库选择和各种错别字,就像字段列表中的错过列一样。