简体   繁体   中英

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. It looks like it's picking up the correct 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">

Just to be clear: 'mysql_query' and accompanying commands is deprecated and should really not be used. The OP however stated that it was required for an assignment. The easiest way to replace them is to use 'mysqli_*' instead. For an example using parameter binding to avoid sql-injection: http://www.php.net/manual/en/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

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.

The variable $_POST['id'] contains the id-value sent from a form via an HTTP POST-request. You check if that value is set, and then you should assign it to '$id' like i wrote.

Your delete sql has wrong quotes

$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

PHP: Having a problem with get_post

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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