Jump to content

Delete Table-Row By Pressing The Delete-Button


jepperask

Recommended Posts

Hello!

 

I'm making a blog and I'm almost done with it. I'm at the point, where i have a page and I've created a while-loop, that shows every post on the page. I even asigned the delete-button, but for some reason it doesn't work. Could you take a look at my code and help me out? Thanks in advance.

 

<?php
session_start();
if(!isset($_SESSION['user_id'])){
header('location: index.php');
exit();
}
if (isset($_POST['post_id'])) {
$post_id = $_POST['post_id'];
if (isset($_POST['delete_id'])) {
mysql_query("DELETE FROM posts WHERE post_id = " . $post_id);
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style>
#delete-post {
width: 100%;
margin: auto;
background-color: #999;
}
</style>
<script src="http://code.jquery.com/jquery-1.5.min.js"></script>
</head>
<body>
<div>
<table border cellpadding="3">
<?php
mysql_connect("localhost", "root", "hejmeddig") or die(mysql_error());
mysql_select_db("pentakill_dk_db") or die(mysql_error());
$data = mysql_query("SELECT * FROM posts") or die(mysql_error());
while($info = mysql_fetch_array($data)){ ?>
<tr>
<th>
<form method="post" action="">
<input type="hidden" name="post_id" value="<?php echo $info['post_id']; ?>" />
<input type="submit" name="delete_id" value="Delete" />
</form>
</th>
<th>
Post-Title:
</th>
<td>
<?php echo $info['title']; ?>
</td>
<th>
Body:
</th>
<td>
<?php echo $info['body']; ?>
</td>
</tr>
<?php } ?>
</table>
</div>
</body>
</html>

Edited by jepperask
Link to comment
Share on other sites

try this as a basic debugging step :

if (isset($_POST['delete_id'])) {
$qry = "DELETE FROM posts WHERE post_id = " . $post_id;
die ($qry);
//mysql_query($qry);
} 

make sure the query looks right, if it does, run it directly in the database as a test, if that works then there is nothing wrong with the query, and there is an issue with some other part of the code.

 

p.s. when developing you should always have an or die attached to every query :

mysql_query($qry) or die (mysql_error(). "<br /> Error occured during :<br /> $sql");

These should be removed from the code before it goes live and be replaces with generic error displays, while the actual helpfull stuff is store securely in a log file or even emailed to yourself.

Link to comment
Share on other sites

try this as a basic debugging step :

if (isset($_POST['delete_id'])) {
$qry = "DELETE FROM posts WHERE post_id = " . $post_id;
die ($qry);
//mysql_query($qry);
} 

make sure the query looks right, if it does, run it directly in the database as a test, if that works then there is nothing wrong with the query, and there is an issue with some other part of the code.

 

p.s. when developing you should always have an or die attached to every query :

mysql_query($qry) or die (mysql_error(). "<br /> Error occured during :<br /> $sql");

These should be removed from the code before it goes live and be replaces with generic error displays, while the actual helpfull stuff is store securely in a log file or even emailed to yourself.

 

Hi, thanks for the reply. But i actually got another answer from a friend, who showed me that it could be the alignment of all my code. So he changed it a bit, and this worked:


<?php
    session_start();
    mysql_connect("localhost", "root", "hejmeddig") or die(mysql_error());          
    mysql_select_db("pentakill_dk_db") or die(mysql_error());
    include 'config.php';
    if (isset($_POST['post_id'])) {
        $post_id = $_POST['post_id'];
        if (isset($_POST['delete_id'])) {
            mysql_query("DELETE FROM posts WHERE post_id = " . $post_id);
        }
    }
?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8"/>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <style>
            #delete-post {
                width: 100%;
                margin: auto;
                background-color: #999;
            }
        </style>
        <script src="http://code.jquery.com/jquery-1.5.min.js"></script>
    </head>
    <body>
        <div>
            <table border cellpadding="3">
                <?php
                $data = mysql_query("SELECT * FROM posts") or die(mysql_error());
                while($info = mysql_fetch_array($data)){ ?>
                    <tr>
                        <th>
                            <form method="post" action="">
                                <input type="hidden" name="post_id" value="<?php echo $info['post_id']; ?>" />
                                <input type="submit" name="delete_id" value="Delete" />
                            </form>
                        </th>
                        <th>
                            Post-Title:
                        </th>
                        <td>
                            <?php echo $info['title']; ?>
                        </td>
                        <th>
                            Body:
                        </th>
                        <td>
                            <?php echo $info['body']; ?>
                        </td>
                   </tr>
                <?php }    ?>
            </table>
        </div>
    </body>
</html>

 

The thing that changed it all, was moving the database connection, to right after the session-start.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.