简体   繁体   English

我的代码有什么问题? 不会从数据库中删除数据

[英]What is wrong with my code? Data is not deleted from database

I've updated the code but keep getting new errors. 我已经更新了代码,但是不断收到新的错误。 I'm really hoping that someone can help me and look at my code to see what is wrong. 我真的希望有人能帮助我,看看我的代码,看看有什么问题。 I have a database table on a webpage and I have one edit button and one delete button on each table row. 我在网页上有一个数据库表,并且每个表行上都有一个编辑按钮和一个删除按钮。 At the moment I'm just trying to get the delete button to work and it will just not delete the row in the database even though I selected that ID. 目前,我只是想使Delete按钮起作用,即使我选择了该ID,它也不会删除数据库中的行。 It looks like it's picking up the correct ID. 看起来它正在获取正确的ID。

Can someone tell what is wrong? 有人能说出什么问题吗? Below is the code... 下面是代码...

<?php 

    require 'connect.inc.php';

    if (isset($_POST['delete']) && isset($_POST['id'])) {
        $id = get_post('id');
        $query = "DELETE FROM movies WHERE id='.$id.' LIMIT 1";

        if (!mysql_query($query, $db_server))
            echo "DELETE failed: $query<br>".
            mysql_error() . "<br><br>";
    }

    $query = "SELECT * FROM movies, categories WHERE movies.genre_id = categories.genre_id";
    $result = mysql_query($query);

    if (!$result) die ("Database access failed:" .mysql_error()) ;
    $rows = mysql_num_rows($result);

    echo '<table><tr><th>Title</th><th>Release year</th><th>Genre</th><th>Director</th><th>Update</th><th>Delete</th></tr>';

    for ($j = 0 ; $j < $rows ; ++$j) {
    $row = mysql_fetch_row($result);
    //$id = $row[0];
    echo '<tr><td>' .$row[1] . '</td>' ;
    echo '<td>' .$row[2] . '</td>' ;
    echo '<td>' .$row[3] . '</td>' ;
    echo '<td>' .$row[4] . '</td>' ;
    echo '<td>'."<a href='edit_movie.php?edit=" . $row[0] . "'>Edit</a>".'</td>';
    echo '<td><form action="index.php" method="POST">
                <input type="hidden" name="delete" value="yes" />
                <input type="hidden" name="id" value="'. $row[0] .'" /> 
                <input type="submit" value="Delete" /></form>
                </td></tr>' ;
    }
    echo '</table>'; 

    include 'add_movie.php';

?>

You forget to close action attribute. 您忘记关闭动作属性。

You have echo '<td><form action="index.php method="POST"> change it to 您已echo '<td><form action="index.php method="POST">更改为

echo '<td><form action="index.php" method="POST">

Just to be clear: 'mysql_query' and accompanying commands is deprecated and should really not be used. 只是要清楚:'mysql_query'及其附带的命令已被弃用,实际上不应该使用。 The OP however stated that it was required for an assignment. 但是,OP表示需要进行分配。 The easiest way to replace them is to use 'mysqli_*' instead. 替换它们的最简单方法是使用“ mysqli_ *”代替。 For an example using parameter binding to avoid sql-injection: http://www.php.net/manual/en/mysqli-stmt.bind-param.php 有关使用参数绑定避免sql注入的示例: http : //www.php.net/manual/zh/mysqli-stmt.bind-param.php

Shouldn't it be: 不应该是:

if (isset($_POST['delete']) && isset($_POST['id'])) {
        $id = mysql_real_escape_string($_POST['id']);
        ...

See this link for some info on 'get_post': PHP: Having a problem with get_post 有关“ get_post”的一些信息,请参见此链接: PHP:get_post有问题

The problem there was that the function 'get_post' was defined on the next page of the course literature, wich the asker hadn't noticed. 问题在于,函数“ get_post”是在课程文献的下一页上定义的,直到问询者都没有注意到。

The variable $_POST['id'] contains the id-value sent from a form via an HTTP POST-request. 变量$ _POST ['id']包含通过HTTP POST请求从表单发送的id值。 You check if that value is set, and then you should assign it to '$id' like i wrote. 您检查该值是否已设置,然后应像我写的那样将其分配给“ $ id”。

Your delete sql has wrong quotes 您的删除SQL引号错误

$query = "DELETE FROM movies WHERE id='.$id.' LIMIT 1";

change to either 更改为

$query = "DELETE FROM movies WHERE id=".$id." LIMIT 1";

or 要么

$query = "DELETE FROM movies WHERE id=$id LIMIT 1";

Try changing the form action 尝试更改表单操作

'<td><form action="index.php" method="POST">

Also check your database connection is properly established 还要检查您的数据库连接是否正确建立

Perhaps this might help for get_post 也许这可能对get_post有帮助

PHP: Having a problem with get_post PHP:get_post有问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM